UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Java
  • » how can we convert an object or input stream to array of bytes

#1 2006-07-22 10:08 AM

suji
Guest

Re: how can we convert an object or input stream to array of bytes

[color="Blue"]I'll be so thankful if anybody can help me in this plz........

How can we convert an object (I mean to say [COLOR="Red"]Object[/color] o;) and the inputstream ([color="Red"]InputStream[/color] is) to array of bytes....

If you can help me, guide me with an example then that will be so greatful...

Thanks in advance....[/COLOR]

#2 2006-07-22 11:00 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: how can we convert an object or input stream to array of bytes

Ok...

So there is no direct 'convert to byte array' type call in Java... but there is serialization...  there is a Serializable interface that can be implemented on objects you want to turn into arrays of bytes for transport down streams, storage in files and so forth but the Object class proper does NOT implement it...

Now working backwards...  input streams and serializing them...  that really doesn't make sense...  lets look at a silly example... YOU are an input stream... you go and get a newspaper and start reading it... the newspaper is your source...  now lets say you 'store' that state ( such that you are reading the day's newspaper )... the following day you are 'retrieved' and pick up the current newspaper... same as before but now its a new day... and its a new edition of the newspaper... and the information, as the stream, that you are retrieving is completely different from what was expected...

So the point is... you cannot truly 'store' a stream... instead you have to read the data on the stream completely... and store that... otherwise there is no telling what type of information you are going to end up with during retrieval...

Now for serializing Object class objects... as previously mentioned, Object class objects do not implement the Serializable interface, so there is no easy way to do it... but if you are REALLY need to do it...

Use the java reflection API on the object... the short version of doing this...

Define your own format to store the object's class type... then on that Class object call getDelcaredFields ( so you retrieve etc. public, protected and private Fields objects for the Class )... then on each of those them setAccessible( ) method ( with True ) so you can directly access them... then determine each field's type ( many ways to do this )... then retrieve the value of each using the appropriate Field.get...( ) method... then store them ( in whatever format you have decided upon )...

To reconstitute the object... read it all back in... reverse the process... and re-set the fields to the old values... 

Simple... huh?!  lol


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2009-01-27 08:22 AM

Seo Services
Guest

Re: how can we convert an object or input stream to array of bytes

Thanks for information. I also like this solution about convert object.

  • Index
  • » Java
  • » how can we convert an object or input stream to array of bytes

Board footer

Powered by FluxBB