Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How can i convert java.lang.Object to byte[]

Reply
Thread Tools

How can i convert java.lang.Object to byte[]

 
 
Neena
Guest
Posts: n/a
 
      09-28-2005
Hi,

How can i convert java.lang.Object to byte[].
cant cast directly. throws ClassCastException.

Regards

Neena

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      09-28-2005
Neena wrote:

> How can i convert java.lang.Object to byte[].


Serialise it.
 
Reply With Quote
 
 
 
 
megagurka
Guest
Posts: n/a
 
      09-28-2005
Neena skrev:
> How can i convert java.lang.Object to byte[].
> cant cast directly. throws ClassCastException.


First of all, why do you want to convert an object into a byte[]?
Second, there are an infinite number of formats for storing an object
in a byte[]. Which format do you want? If you don't care, you can
probably use the Java serialization mechanism, see
java.io.Serializable, java.io.ObjectOutputStream and
java.io.ByteArrayOutputStream for more information.

/JN

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-28-2005
On 28 Sep 2005 02:55:11 -0700, "Neena" <>
wrote or quoted :

>
>How can i convert java.lang.Object to byte[].
>cant cast directly. throws ClassCastException.

If the backing object is not REALLY a byte[] already then it is
impossible. An Object just has a few fields. You are asking the
machine to create a silk purse out of a sow's ear.

See http://mindprod.com/gotchas.html#ARRAY
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-28-2005
On 28 Sep 2005 02:55:11 -0700, "Neena" <>
wrote or quoted :

>How can i convert java.lang.Object to byte[].
>cant cast directly. throws ClassCastException.


If what you are trying to do is flatten an object for transport, then
you need to serialise it. See http://mindprod.com/applets/fileio.html
for how to write a serialized object into a byte array.

See http://mindprod.com/jgloss/serialization.html

Could we see your surrounding code to understand the context of your
question?
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-28-2005
On 28 Sep 2005 02:55:11 -0700, "Neena" <>
wrote or quoted :

>
>How can i convert java.lang.Object to byte[].
>cant cast directly. throws ClassCastException.


there is yet another way to interpret your question, how do I write
the fields of a Object (subclass of object really) in binary to a byte
array. For that you can use a DataOutputStream and a ByteArrayStream.
See http://mindprod.com/applets/fileio.html for how to hook them up.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Neena
Guest
Posts: n/a
 
      09-29-2005
I just wanted to pass abytearray to my JNI native code. i will have a
long, string or Calendar objects.
used the following method to create byte[]:

ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ObjectOutputStream oStream = new ObjectOutputStream( bStream );
oStream.writeObject ( obj );
byte[] byteVal = bStream. toByteArray();

thanks alot...

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2005
On 29 Sep 2005 04:53:33 -0700, "Neena" <>
wrote or quoted :

>I just wanted to pass abytearray to my JNI native code. i will have a
>long, string or Calendar objects.
> used the following method to create byte[]:


Perhaps you are trying to create something that looks like a C struct
in memory. Then once C has the address of it, it can go to town
without fooling around with JNI field by field access?

If that is true, if you are on a little-endian machine you can use
LEDatastream see http://mindprod.com/products1.html#LEDATASTREAM

Write to a byte array by combining it with a ByteArrayOutputStream.
See http://mindprod.com/applets/fileio.html for details. Tell it you
have little-endian binary data and want to write to a byte array.
For character data, write native platform-encoded bytes, null
terminated in fixed length fields.

If you don't have to worry about compatibility with older JVMs, you
can use nio instead of LEDataStream.

If your machine is big-endian, just tell the file i/o amanuensis you
have big endian binary data instead.

Similarly when you return, you can take the C struct apart again to
get the value in the fields using LEDataInputStream.

This technique should be very fast on the C side, with a fairly heavy
set up cost. The slightly ugly thing about it is if you implement it
on a variety of platforms, on the Java side you need a big and little
endian version, and if there are padding/alignment bytes in the C
struct, they too may be platform dependent. With normal JNI the Java
side is identical for all platforms. Only the C side changes.



--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Neena
Guest
Posts: n/a
 
      09-30-2005
thanks alot..
i am going to give it a try...)

 
Reply With Quote
 
vakhouri vakhouri is offline
Junior Member
Join Date: Jul 2007
Posts: 1
 
      07-28-2007
Thanks For the reply Neena, it was of great help..
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Qestion about convert Object to byte[] and convert it back davidxiongcn@gmail.com Java 5 11-04-2006 04:11 PM
IsNumeric: Convert.ToInt32 vs. Convert.ToInt64 sck10 ASP .Net 4 09-03-2006 09:40 PM
To convert to J2SE 6 or not to convert, that is the question... Jaap Java 4 07-10-2006 09:03 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Do I need to Convert with Convert.ToInt32(session("myNumber")) ? Andreas Klemt ASP .Net 1 07-23-2003 02:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57