weird serialization issue with long

D

Daisy

I'd appreciate some help with a weird problem.

A long in an object that I serialize is read as zero every other
iteration. Since I'm creating new instances every time, I don't think
it has to do with reset() or flush(). Also, it seems inefficient to
keep recreating the Byte and ObjectOutput streams. I'm going to try
reset() and flush() rather than new instances once I close this long
issue. Is there a better approach?

// create event
MyEvent event = new MyEvent("test",123);

// serialize the event into a byte stream
ByteArrayOutputStream baos = new ByteArrayOutputStream( );
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( event );

// put the byte stream into our friendly ByteBuffer
ByteBuffer buffer = ByteBuffer.wrap( baos.toByteArray( ) );

public class MyEvent implements Serializable {

private long long1;

private final String string1;

public MyEvent( String stringArg , long longArg ) {
long1 = longArg;
string1=stringArg;
}
}


To read the event, I use:
ByteArrayInputStream bais = new ByteArrayInputStream( buffer.array( )
);

ObjectInputStream ois = new ObjectInputStream( bais );

MyEvent eventRead = ( MyEvent ) ois.readObject( );

Thanks for the help!
 
V

Venkatesh

Hi,

I'm unable to reproduce ur problem. Things work fine for me .... Where
are you reading the object back? Is it in the same method or in a
different method?

FYI, here is the code that I have written:

package com.trilogy.temp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io_ObjectInputStream;
import java.io_ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;

class MyEvent implements Serializable {

private long long1;

private final String string1;

public MyEvent( String stringArg , long longArg ) {
long1 = longArg;
string1=stringArg;
}

public String toString() {
return("MyEvent[string1=" + string1 + ", long1=" + long1 +
"]");
}

}

public class TestSerialization {

public void testSerialize() throws IOException,
ClassNotFoundException {

MyEvent event = new MyEvent("test", 123);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);

oos.writeObject(event);

ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray());

ByteArrayInputStream bais = new
ByteArrayInputStream(buffer.array());
ObjectInputStream ois = new ObjectInputStream(bais);

MyEvent eventRead = ((MyEvent)ois.readObject());

System.out.println("eventRead = " + eventRead);

}

/**
* @param args
*/
public static void main(String[] args) {

try {
(new TestSerialization()).testSerialize();
} catch(Exception e) {
System.err.println("Got exception: " + e.getMessage());
e.printStackTrace();
}

}

}


The output I get is:

eventRead = MyEvent[string1=test, long1=123]
 
M

Mike Schilling

Daisy said:
I'd appreciate some help with a weird problem.

A long in an object that I serialize is read as zero every other
iteration. Since I'm creating new instances every time, I don't think
it has to do with reset() or flush(). Also, it seems inefficient to
keep recreating the Byte and ObjectOutput streams. I'm going to try
reset() and flush() rather than new instances once I close this long
issue. Is there a better approach?

// create event
MyEvent event = new MyEvent("test",123);

// serialize the event into a byte stream
ByteArrayOutputStream baos = new ByteArrayOutputStream( );
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( event );

// put the byte stream into our friendly ByteBuffer
ByteBuffer buffer = ByteBuffer.wrap( baos.toByteArray( ) );

public class MyEvent implements Serializable {

private long long1;

private final String string1;

public MyEvent( String stringArg , long longArg ) {
long1 = longArg;
string1=stringArg;
}
}


To read the event, I use:
ByteArrayInputStream bais = new ByteArrayInputStream( buffer.array( )
);

ObjectInputStream ois = new ObjectInputStream( bais );

MyEvent eventRead = ( MyEvent ) ois.readObject( );

This shows the reading and writing of only one instance. Can you show us an
exmaple that doesn more than one and demonstrates the every-other-time
behavior?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top