Class: Java::ObjectOutputStream

Inherits:
Object
  • Object
show all
Includes:
ObjectStream
Defined in:
lib/javaobs.rb

Overview

A Ruby version of the Java ObjectOutputStream. The Ruby classes must have attached Java meta classes to attach UUID.

Constant Summary

Constants included from ObjectStream

Java::ObjectStream::PRIM_ARRAY, Java::ObjectStream::PRIM_BOOL, Java::ObjectStream::PRIM_BYTE, Java::ObjectStream::PRIM_CHAR, Java::ObjectStream::PRIM_DOUBLE, Java::ObjectStream::PRIM_FLOAT, Java::ObjectStream::PRIM_INT, Java::ObjectStream::PRIM_LONG, Java::ObjectStream::PRIM_OBJECT, Java::ObjectStream::PRIM_SHORT, Java::ObjectStream::SC_BLOCKDATA, Java::ObjectStream::SC_EXTERNALIZABLE, Java::ObjectStream::SC_SERIALIZABLE, Java::ObjectStream::SC_WRITE_METHOD, Java::ObjectStream::STREAM_MAGIC, Java::ObjectStream::STREAM_VERSION, Java::ObjectStream::TC_ARRAY, Java::ObjectStream::TC_BLOCKDATA, Java::ObjectStream::TC_BLOCKDATALONG, Java::ObjectStream::TC_CLASS, Java::ObjectStream::TC_CLASSDESC, Java::ObjectStream::TC_ENDBLOCKDATA, Java::ObjectStream::TC_EXCEPTION, Java::ObjectStream::TC_LONGSTRING, Java::ObjectStream::TC_NULL, Java::ObjectStream::TC_OBJECT, Java::ObjectStream::TC_PROXYCLASSDESC, Java::ObjectStream::TC_REFERENCE, Java::ObjectStream::TC_RESET, Java::ObjectStream::TC_STRING

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ ObjectOutputStream

Create an o writer on with a stream.



676
677
678
679
680
681
682
683
# File 'lib/javaobs.rb', line 676

def initialize(str)
  @str = str
  @handles = {}
  @nextHandle = 0

  writeUShort(STREAM_MAGIC)
  writeShort(STREAM_VERSION)
end

Instance Method Details

#defaultWriteObject(object) ⇒ Object



606
607
608
# File 'lib/javaobs.rb', line 606

def defaultWriteObject(object)
  writeObjectFields(object.class.javaClass, object)
end

#nextHandleObject

Creates object reference handles.



516
517
518
519
520
# File 'lib/javaobs.rb', line 516

def nextHandle
  h = @nextHandle
  @nextHandle += 1
  h
end

#writeArray(klass, v) ⇒ Object

Write an array of objects.



523
524
525
526
527
528
529
# File 'lib/javaobs.rb', line 523

def writeArray(klass, v)
  type = klass.arrayType
  writeInt(v.length)
  v.each do |e|
    writeType(type, e)
  end
end

#writeBlockData(data) ⇒ Object

Writes a block of data to the stream.



546
547
548
549
550
# File 'lib/javaobs.rb', line 546

def writeBlockData(data)
  writeBlockStart(data.length)
  @str.write data
  writeBlockEnd
end

#writeBlockEndObject



541
542
543
# File 'lib/javaobs.rb', line 541

def writeBlockEnd
  writeByte(TC_ENDBLOCKDATA)
end

#writeBlockStart(size) ⇒ Object



531
532
533
534
535
536
537
538
539
# File 'lib/javaobs.rb', line 531

def writeBlockStart(size)
  if (size <= 255)
    writeByte(TC_BLOCKDATA)
    writeByte(size)
  else
    writeByte(TC_BLOCKDATALONG)
    writeInt(size)
  end
end

#writeBool(b) ⇒ Object



511
# File 'lib/javaobs.rb', line 511

def writeBool(b); writeByte(b ? 1 : 0); end

#writeByte(b) ⇒ Object



504
# File 'lib/javaobs.rb', line 504

def writeByte(b); @str.putc b; end

#writeClassDesc(klass) ⇒ Object

Writes the class description to the stream.



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/javaobs.rb', line 588

def writeClassDesc(klass)
  @handles[klass] = nextHandle

  writeString klass.javaName
  writeUID klass.uid
  writeByte klass.flags

  writeShort(klass.fields.length)
  klass.fields.each do |f|
    writeByte(f.type)
    writeString(f.name)
    writeObject(f.subtype) if f.subtype
  end

  writeByte(TC_ENDBLOCKDATA) # Annotations
  writeObject(klass.superClass)
end

#writeDouble(d) ⇒ Object



508
# File 'lib/javaobs.rb', line 508

def writeDouble(d); @str.write [d].pack("G"); end

#writeFloat(f) ⇒ Object



509
# File 'lib/javaobs.rb', line 509

def writeFloat(f); @str.write [f].pack("g"); end

#writeInt(i) ⇒ Object



507
# File 'lib/javaobs.rb', line 507

def writeInt(i); @str.write [i].pack("i"); end

#writeLong(l) ⇒ Object



513
# File 'lib/javaobs.rb', line 513

def writeLong(l); @str.write [l].pack("Q"); end

#writeObject(obj) ⇒ Object

Writes the object and class data to the stream. Will write a reference if the object has already been written once.



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/javaobs.rb', line 631

def writeObject(obj)
 unless obj
   writeByte(TC_NULL)
 else
   handle = @handles[obj]
   if (handle)
     writeByte(TC_REFERENCE)
     writeShort(0x007E)
     writeShort(handle)
   else
     case obj
     when JavaClass
       writeByte(TC_CLASSDESC)
       writeClassDesc(obj)

     when JavaArray
       writeByte(TC_ARRAY)
       writeObject(obj.class.javaClass)
       writeArray(obj.class.javaClass, obj)
       @handles[obj] = nextHandle
   
     when String
       writeByte(TC_STRING)
       writeString(obj)
       @handles[obj] = nextHandle
   
     else
       writeByte(TC_OBJECT)
       klass = obj.class.javaClass
       writeObject(klass)
       @handles[obj] = nextHandle
       writeObjectData(klass, obj)
     end
   end
 end
end

#writeObjectData(klass, obj) ⇒ Object

Write the object and class to the stream.



618
619
620
621
622
623
624
625
626
# File 'lib/javaobs.rb', line 618

def writeObjectData(klass, obj)
  writeObjectData(klass.superClass, obj) if klass.superClass

  if klass.flags == SC_SERIALIZABLE
    writeObjectFields(klass, obj)
  else
    obj._writeJavaData(self)
  end
end

#writeObjectFields(klass, object) ⇒ Object



610
611
612
613
614
615
# File 'lib/javaobs.rb', line 610

def writeObjectFields(klass, object)
  klass.fields.each do |f|
    v = object.send(f.name.intern)
    writeType(f.type, v)
  end
end

#writeObjects(objs) ⇒ Object

Write an array of objects to the stream.



669
670
671
672
673
# File 'lib/javaobs.rb', line 669

def writeObjects(objs)
  objs.each do |o|
    writeObject o
  end
end

#writeShort(s) ⇒ Object



506
# File 'lib/javaobs.rb', line 506

def writeShort(s); @str.write [s].pack("s"); end

#writeString(s) ⇒ Object



510
# File 'lib/javaobs.rb', line 510

def writeString(s); writeShort(s.length); @str.write s; end

#writeType(type, v) ⇒ Object

Reads a Java primitive type.



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/javaobs.rb', line 553

def writeType(type, v)
  case type
  when PRIM_BYTE
    writeByte(v)

  when PRIM_CHAR
    writeByte(v)

  when PRIM_DOUBLE
    writeDouble(v)

  when PRIM_FLOAT
    writeFloat(v)

  when PRIM_INT
    writeInt(v)

  when PRIM_LONG
    writeLong(v)

  when PRIM_SHORT
    writeShort(v)

  when PRIM_BOOL
    writeBool(v)

  when PRIM_OBJECT, PRIM_ARRAY
    writeObject(v)

  else
    raise SerializationError, "Unknown type #{type}"
  end
end

#writeUID(u) ⇒ Object



512
# File 'lib/javaobs.rb', line 512

def writeUID(u); @str.write u; end

#writeUShort(s) ⇒ Object



505
# File 'lib/javaobs.rb', line 505

def writeUShort(s); @str.write [s].pack("n"); end