File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes can an outputstream be serialised? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can an outputstream be serialised?" Watch "can an outputstream be serialised?" New topic

can an outputstream be serialised?

Moin Khatri

Joined: Mar 18, 2007
Posts: 6
I am Moin.Please can anyone explain me whether an Output Stream be serialized or no and why.

Thanks in advance,
Amir Alagic
Ranch Hand

Joined: Mar 21, 2006
Posts: 65
no, it is not possible to serialize OutputStream.
Moin Khatri

Joined: Mar 18, 2007
Posts: 6
thanks for the reply Amir.any explaination this context would be really helpful for me

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
The short answer is, because no OutputStream (that I know of) implements the Serializable interface. Therefore, if you try to serialize it, you will get an exception. They didn't design these classes to be serializable because they didn't think it would be useful to anyone.

I think you probably need to serialize the data written to the OutputStream. Can you tell us anything about the OutputStream you're using? Where did it come from? Did you create it, or did it come from some other class? What sort of data is being written to it?

One possibly useful technique is to us a ByteArrayOutputStream for the OutputStream. When you are done writing to the stream, close() it, and then call toByteArray(). Now you have a byte[] which can be serialized.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: can an outputstream be serialised?
It's not a secret anymore!