aspose file tools*
The moose likes I/O and Streams and the fly likes object stream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "object stream" Watch "object stream" New topic
Author

object stream

dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello i would like to know if it is possible to work with objectInputStream and ObjectOutputStream without having to load and save to a file. I dont want to depend on any file at the moment, but i would like to work with a stream of object. For exemple a cat instance that i would like to serialize and reconstruct for testing without writing it to a file.
Thank you for helping.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

ObjectInputStream and ObjectOutputStream can wrap any InputStream / OutputStream, not just FileInputStream and FileOutputStream.

For example, you could use ByteArrayInputStream and ByteArrayOutputStream for temporary storage.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello and thank your for your answer.
How would i transform then my object and pass it to the ByteArrayInputStream ?
The constructor takes an array of bytes. How would i transform my object in a byte array ?
Thank you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

I think you get the order wrong.

You create a ByteArrayOutputStream (BAOS), wrap it in an ObjectOutputStream (OOS), write to that OOS and when you're done you retrieve the byte array from the BAOS using the toByteArray() method.

You can then later create a ByteArrayInputStream (BAIS) using that byte array, wrap it in an ObhjectInputStream (OIS) and read from that OIS.

If you want to read before you're done writing, either use multiple BAOS and BAIS instances, or create some other way of redirecting an output stream to an input stream. Perhaps the PipedOutputStream and PipedInputStream classes can help you. Check out their APIs for more info.
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello,
So here is what i did and it works for one instance of my class :


But now if i want to test with 2 or more instance of Database, how would i go for it ? The thing is that i want to serialize Database object each time one is created. Is it possible to serialise it in the same file ? Or if i have multiple file (one for each serialized Database) how could i read them all ?
Hope i m clear.
Thank you for any help on how to go for this kind of work.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

If your ObjectOutputStream is writing to the same file then you must not instantiate the ObjectOutputStream multiple times. Each time a new ObjectOutputStream writes to a file it writes a few headers. If you use multiple ObjectOutputStream to write to the same file, these headers will be written multiple times and hence the file will be corrupted.
So, if you are using the same file, then share the ObjectOutputStream also.


apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object stream
 
Similar Threads
Convertion of string object to BLOB
Uploading files from an applet to a servlet!?!?
HELP!!! Output stream closing prematurely
to find a string in a file
Serialization/Deserialization