Module: Java
- Defined in:
- lib/javaobs.rb
Overview
Java Objects namespace to read and write Java serialized objects to streams. Any Java serialized object can be read from a stream. To write a Java object, the meta class must be primed with a sample input serialized object. The is required because Java uses a UUID to identify classes and it is generated using a complex hashing scheme of data and method signatures. Since this system does not have access to that information, it needs to get it from a serialized object.
Objects that have custom serialization methods can be read and written by creating a class as we have for the Date class:
module Java
module Util
class Date < SimpleDelegator
extend JavaObject
def initialize
super(Time)
end
# Set the time with a Time object.
def time=(time)
__setobj__(time)
end
def _readJavaData(stream)
data = stream.readBlockData
t, = data.unpack("Q")
__setobj__(Time.at(t / 1000, (t % 1000) * 1000))
end
# Get the data in the form needed for the Java date serialization.
def _writeJavaData(stream)
t = __getobj__.tv_sec * 1000 + __getobj__.tv_usec / 1000
stream.writeBlockData([t].pack("Q"))
end
end
end
end
The important methods are the data method that is used for writing the the object to a stream.
All other classes will be auto-generated when the stream is read and persisted. A Java Meta Class is added to the Ruby Class that contains all the Java field information needed to serialize the objects.
Defined Under Namespace
Modules: JavaObject, ObjectStream, Util Classes: JavaArray, JavaClass, JavaField, ObjectInputStream, ObjectOutputStream, SerializationError