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.



686
687
688
689
690
691
692
693
# File 'lib/javaobs.rb', line 686

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

  writeUShort(STREAM_MAGIC)
  writeShort(STREAM_VERSION)
end

Instance Method Details

#defaultWriteObject(object) ⇒ Object

Ruby version of the default write object method.



615
616
617
# File 'lib/javaobs.rb', line 615

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

#nextHandleObject

Creates object reference handles.



522
523
524
525
526
# File 'lib/javaobs.rb', line 522

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

#writeArray(klass, v) ⇒ Object

Write an array of objects.



529
530
531
532
533
534
535
# File 'lib/javaobs.rb', line 529

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.



554
555
556
557
558
# File 'lib/javaobs.rb', line 554

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

#writeBlockEndObject

Write the block end tag.



549
550
551
# File 'lib/javaobs.rb', line 549

def writeBlockEnd
  writeByte(TC_ENDBLOCKDATA)
end

#writeBlockStart(size) ⇒ Object

Write the beginning of a block tag with the size of the block.



538
539
540
541
542
543
544
545
546
# File 'lib/javaobs.rb', line 538

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

#writeBool(b) ⇒ Object



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

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

#writeByte(b) ⇒ Object



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

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

#writeClassDesc(klass) ⇒ Object

Writes the class description to the stream.



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/javaobs.rb', line 596

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



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

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

#writeFloat(f) ⇒ Object



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

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

#writeInt(i) ⇒ Object



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

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

#writeLong(l) ⇒ Object



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

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.



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
667
668
669
670
671
672
673
674
675
676
# File 'lib/javaobs.rb', line 641

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.



628
629
630
631
632
633
634
635
636
# File 'lib/javaobs.rb', line 628

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

Internal method to write meta fields to stream.



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

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.



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

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

#writeShort(s) ⇒ Object



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

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

#writeString(s) ⇒ Object



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

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

#writeType(type, v) ⇒ Object

Reads a Java primitive type.



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
586
587
588
589
590
591
592
593
# File 'lib/javaobs.rb', line 561

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



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

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

#writeUShort(s) ⇒ Object



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

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