Menu

ObjectStream

Developers
2007-05-16
2012-08-14
  • Moritz Hoffmann

    Moritz Hoffmann - 2007-05-16

    A small class that enhances the stream class with the methods ReadObject and WriteObject. Just a proof of concept, adapt it if you want!
    Below there is a small test case.
    Both contributed to the ooRexx Project and thus released under the CPL.


    /
    Enhanced stream class that is capable of reading and writing objects using
    the serializatation framework.
    /
    .environment~ObjectStream = .ObjectStream

    ::REQUIRES "Serializable.cls"
    ::CLASS ObjectStream SUBCLASS STREAM
    ::METHOD ReadObject
    -- Method to read an object from the stream
    lines = .array~new
    do i = 1 while line \= ""
    line = self~linein
    if line \= "" then
    lines[i] = line
    end
    return .SerializeFunctions~Deserialize(lines)

    ::METHOD WriteObject
    -- method to write an object to the stream
    use arg object
    do line over .SerializeFunctions~Serialize(object)
    self~lineout(line)
    end
    self~lineout("")
    return 0


    !/opt/ooRexx/bin/rexx

    parse arg modifier file .
    modifier = modifier~translate
    if modifier = "READ" then do
    s = .ObjectStream~new(file)
    r = s~ReadObject
    say r
    say s~ReadObject
    say s~ReadObject
    end
    else if modifier = "WRITE" then do
    s = .ObjectStream~new(file)
    s~open("WRITE REPLACE")
    s~WriteObject(.Test~new~~FillData)
    s~WriteObject(.Test2~new)
    end
    else say "Usage: ./teststream.rex (WRITE|READ) file"

    ::REQUIRES "ObjectStream.cls"
    ::CLASS test MIXINCLASS Serializable
    ::METHOD PersistentData ATTRIBUTE
    ::METHOD FillData
    stem. = "I'm a stem variable"
    stem.1 = "first level entry"
    stem.1.1 = "sub entry"
    d = .directory~new
    d~method = .method~new("my method","Say 'hello'")
    d~mb = .mutablebuffer~new("This is some string")
    d~stem = stem.
    string = "Test, occurring several times"
    d~array = .array~of(.bag~of(string,string),.list~of(string,string))
    d~object = .test2~new
    d~nil = .nil
    r = .relation~new
    r[string] = 1
    r[string] = 42
    r[42] = string
    d~relation = r
    t = .table~new
    t[string] = stem.
    d~table = t
    q = .queue~new
    q~queue(string)
    d~queue = q
    self~PersistentData = d

    ::CLASS Test2 MIXINCLASS Serializable
    ::METHOD ReadObject CLASS
    use arg data
    return self~new(data)
    ::METHOD WriteObject
    return 'a'
    ::METHOD Init
    use arg data

     
    • Rick McGuire

      Rick McGuire - 2007-05-16

      My preferred mode of operation for the serializable support would be to have it operate using streams alone. There are a number of performance efficiencies to be gained from streaming the output vs. building the persistence data up using lots of mutable buffers and concat operations. To support this, I've got a couple of "in memory" streams in the works that would allow the serialization data to be easily captured. Also, there's a socket stream implementation in the works which would allow RMI-like operations to be implemented using the serialization framework.

      Rick

       

Log in to post a comment.