Java help: assigning "this" in constructor

Discussion in 'OT Technology' started by Euclid, Apr 16, 2005.

  1. Euclid

    Euclid New Member

    Joined:
    Apr 19, 2004
    Messages:
    8,139
    Likes Received:
    0
    Location:
    Nashville, TN
    Java help: serializing complicated Objects

    The problem is with serializing complicated objects, see post #3

    I need to send and receive an object as a byte array, so I am using this article for motivation. Basically you make the object serializable, write it to an ObjectOutputStream, and send that through a ByteArrayOutputStream to getBytes() the datagram. The object can be reconstituted by doing the same steps in reverse with InputStreams.

    http://www.javaworld.com/javaworld/javatips/jw-javatip40.html

    My question is this:

    I would like to have a constructor for my class that takes a byte array and reconstitutes it, assigning the result to "this". Can I do that?

    Something like:

    public class CipherText implements Serializable
    {
    public CipherText( byte[] buffer )
    {
    ByteArrayInputStream baiStream = new ByteArrayInputStream( buffer );
    ObjectInputStream oiStream = new ObjectInputStream( new BufferedInputStream(baiStream) );
    this = oiStream.readObject();
    oiStream.close();
    }
    }


    If I can't do that, should I just make a static method that returns a CipherText object?
     
    Last edited: Apr 16, 2005
  2. Euclid

    Euclid New Member

    Joined:
    Apr 19, 2004
    Messages:
    8,139
    Likes Received:
    0
    Location:
    Nashville, TN
    cs555/CipherText.java:42: cannot assign a value to final variable this
    this = (CipherText) oiStream.readObject();
    ^
    1 error


    I think that answers my first question... Anyone have any suggestions for me?
     
  3. Euclid

    Euclid New Member

    Joined:
    Apr 19, 2004
    Messages:
    8,139
    Likes Received:
    0
    Location:
    Nashville, TN
    Okay, so I did the static method and I think it will be okay.

    Now the problem is that it doesn't seem to be serializing all of the data.

    public class CipherText implements Serializable
    {
    private BigInteger delta;
    private Hashtable gammas;


    This is pretty much it right here. The Hashtable has keys of type Integer that are mapped to Stacks of BigIntegers.

    I didn't do anything besides add "implements Serializable" on this thing. When I decode the byte array into a new CipherText object, I get the correct BigInteger delta on the other side, but I got an EmptyStackException when I tried to access the values stored in the Hashtable.

    I assume I need to write special writeObject() and readObject() methods for the serialization, can anyone confirm this? Any suggestions?
     
  4. rpark

    rpark New Member

    Joined:
    Apr 9, 2005
    Messages:
    1,082
    Likes Received:
    0
    Location:
    Isla Vista, California
    this is a keyword in Java and cannot be a varible name.
     
  5. Euclid

    Euclid New Member

    Joined:
    Apr 19, 2004
    Messages:
    8,139
    Likes Received:
    0
    Location:
    Nashville, TN
    Thanks, I resolved that issue.

    Any suggestions on how I can make my object Serializable properly?

    (see post 3)
     
  6. crotchfruit

    crotchfruit Guest

    what happens if you make those members protected instead of private?
     
  7. danison

    danison The Contrarian

    Joined:
    Mar 11, 2005
    Messages:
    426
    Likes Received:
    0
    Location:
    Portland, OR
    This seems curious. Stack, Integer, BigInteger, and Hashtable are all serializable so there should be no issue when you serialize or deserialize your object.

    I would not mark the method protected, that does nothing as far as serialization is concerned, the only modifer that has any impact on serialization is transient and I do not seee that here. If you want to post the entire class here or message me I will be glad to take a look.

    And FYI, HashMap is generally better to use than Hashtable. Hashtable is nice because its thread safe, but its expensive. And if threadsafety is an issue just pass the HashMap into Collections.synchronizedMap( map ).
     

Share This Page