Serializing objects into a stream

B

blazingdragon

I'm trying to wrap an ObjectOutputStream around a PipedOutputStream...

It doesn't seem to mind as as the PipedOutputStream isn't connected,
however if I try to wrap the ObjectOutputStream around an already
connected PipedOutputStream, everything simply freezes.. no error
messages or anything.

If I try and connect the pipe afterwards, it doesn't seem to do
anything. -_-; I get a pipe not connected IOException if I try and
write an object into the ObjectOutputStream.

I need to serialize an object and have it be written into an array of
bytes. I'm probably going about this the wrong way, so any suggestions
would be great.
 
E

EJP

I'm trying to wrap an ObjectOutputStream around a PipedOutputStream...

It doesn't seem to mind as as the PipedOutputStream isn't connected,
however if I try to wrap the ObjectOutputStream around an already
connected PipedOutputStream, everything simply freezes.. no error
messages or anything.

Constructing an ObjectOutputStream writes to the nested stream. Is there
a reading thread for the PipedOutputStream? and is it reading? If not,
that would explain the freeze. (The reader should be attempting to
construct an ObjectInputStream around its PipedInputStream at the same
time as the above is happening: it will block until the
ObjectOutputStream is constructed.)
 
C

Chris Uppal

It doesn't seem to mind as as the PipedOutputStream isn't connected,
however if I try to wrap the ObjectOutputStream around an already
connected PipedOutputStream, everything simply freezes.. no error
messages or anything.

Your problem sounds like a known deadlock scenario. Search Google groups for

PipedOutputStream deadlock

to find the details.

-- chris
 
T

Thomas Hawtin

I need to serialize an object and have it be written into an array of
bytes. I'm probably going about this the wrong way, so any suggestions
would be great.

Use java.io.ByteArrayOutputStream:

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(...your object reference here...);
out.flush();
byte[] data = byteOut.toByteArray();

Tom Hawtin
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top