aspose file tools*
The moose likes Beginning Java and the fly likes How I get byte[ ] from Object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How I get byte[ ] from Object?" Watch "How I get byte[ ] from Object?" New topic
Author

How I get byte[ ] from Object?

Nacho Espinosa
Ranch Hand

Joined: Jan 17, 2007
Posts: 30
Hi everybody:

Well, I try to get byte[] from Object.

Is There any Class that do it?

Or any possible solution for this issue.

ByteBuffer only works for primitive types.

Thanks


Greetings<br />Nacho Espinosa<br />SCJP 1.4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Well, a byte array is an object, so... if this object that is referred to is a byte array, then you can simply cast it back to a byte array reference before you use it.

However, if this object is not a byte array, then it would depend on what this object is. Is there a method that will return the underlying byte array? Or is there a meaning to converting to a byte array? etc.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nacho Espinosa
Ranch Hand

Joined: Jan 17, 2007
Posts: 30
My issue is next.

I have an Array Object (Bean[]), I want to get the size for this array object, I think that obtaining the byte[], I get the size, when I use the length attribute for this object.

That�s the reason about to know the byte[].

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Nacho Espinosa:
I have an Array Object (Bean[]), I want to get the size for this array object, I think that obtaining the byte[], I get the size, when I use the length attribute for this object.


The length field of the array will return the number of elements in the array. With Java, there is no way to determine the physical byte size of an object. (ie. there is no sizeof operator)

Henry
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Nacho Espinosa:
Well, I try to get byte[] from Object.



How do you get byte[] from Object ? Do you mean serializing the object to get a stream of bytes.


Rahul Bhattacharjee
LinkedIn - Blog
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Serialising will give you a byte array, providing the object is Serializable. However, the length of this byte array will not be the same as the size of the object in memory; the serialised version is usually bigger.

There is no proper way to obtain the size of a Java object in memory. But there are very few legitimate reasons to need to know it. Often, people who think they need to know the size are people coming from a C/C++ background, who are used to dealing with the raw memory in a way that Java programs just don't do.

If you really do need to know the size, reasonable estimates can be made. Search the forums. This has been discussed many times before.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
Ok ... Eat this ready to eat stuff. This is cooked with ObjectOutputStream.



Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I think in this case also you are seralizing the object obj as only the serializable objects can be written to the ObjectOutputStream.

I agree with Peter Chase , we cannot find the in memory size of the object.moreover how a jvm interprets the bytecode and makes objects in the heap is completely vendor specific and there is no exposed api's to interrogate this stuff.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Pradeep Kadambar:
Ok ... Eat this ready to eat stuff. This is cooked with ObjectOutputStream.


Although it's not 100% clear, I don't think the original poster wanted to serialise his object (well, an array, actually). He wanted to know the true size in memory of the object. As I and another have explained, this is not possible in Java - only an estimate can be made.

If serialisation were wanted, then the posted code basically works, but has several poor features.

The only one I'll mention is that the squashing of IOException and returning empty array could mask a programming error. No real I/O is happening - only in-memory pseudo-I/O - and hence an IOException should never happen. If an IOException did somehow happen, the last thing you'd want to do is squash it, because it would mean something seriously wrong.
Nacho Espinosa
Ranch Hand

Joined: Jan 17, 2007
Posts: 30
Thanks for answers:

You help me to understand that not a way for obtain the size for an object.

Thanks Peter, and other people.

I found another solution about this issue.

But I thought that it was possible.

Thanks a lot.
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
Well .... What you are talking about is the second part of the question.


Although it's not 100% clear, I don't think the original poster wanted to serialise his object (well, an array, actually). He wanted to know the true size in memory of the object.


That's exactly why your are not totally clear about what was asked. As far as the solution it's not about giving fool proof solutions. If just has to give the idea.

All in good sense
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This Java Specialist's Newsletter is about memory counts. As usual, it goes over my head after about 2 lines. See if it does what you need.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
wood burning stoves
 
subject: How I get byte[ ] from Object?