• Post Reply Bookmark Topic Watch Topic
  • New Topic

object stream and byte array conversion  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,


I am wondeirng how to convert an ObjectInputStream to a byte array, then convert the array back to ObjectInputStream -- should I convert the array back to ObjectOutputStream other than ObjectInputStream?

Any sample codes?


thanks in advance,
George
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To "convert" an ObjectInputStream to a byte[], you'll just need to read all the contents using read() and store the data in a byte[]. A ByteArrayOutputStream provides a convenient way to do this: read the data and write to a ByteArrayOutputStream, then ask the ByteArrayOutputStream for the bytes.

To "convert" back to an ObjectInputStream, wrap a ByteArrayInputStream around the byte[], and wrap an ObjectInputStream around the ByteArrayInputStream.
 
George Lin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, Ernest!


Originally posted by Ernest Friedman-Hill:
To "convert" an ObjectInputStream to a byte[], you'll just need to read all the contents using read() and store the data in a byte[]. A ByteArrayOutputStream provides a convenient way to do this: read the data and write to a ByteArrayOutputStream, then ask the ByteArrayOutputStream for the bytes.

To "convert" back to an ObjectInputStream, wrap a ByteArrayInputStream around the byte[], and wrap an ObjectInputStream around the ByteArrayInputStream.



regards,
George
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!