Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Neena - 28 Sep 2005 10:55 GMT
Hi,

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

Regards

Neena
Andrew Thompson - 28 Sep 2005 11:25 GMT
> How can i convert java.lang.Object to byte[].

Serialise it.
megagurka - 28 Sep 2005 11:32 GMT
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
Roedy Green - 28 Sep 2005 21:00 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 28 Sep 2005 21:09 GMT
>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?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 28 Sep 2005 22:46 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Neena - 29 Sep 2005 12:53 GMT
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...
Roedy Green - 29 Sep 2005 14:51 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Neena - 30 Sep 2005 05:25 GMT
thanks alot..
i am going to give it a try...:-))


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.