This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert an Object to ByteArray

 
Kunal Aher
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I plan to write an application where the Object is stored to some secondary storage. Can anyone suggest how we can convert an Object to a array of Bytes , so that I can store the Objects ?
Has anyone come across a similar problem , and if yes , what was the solution that was adopted ?
All ideas welcome .
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kunal,
Why not just Serialize the Object onto an ObjectOutputStream? You can write your objects to a file like this:

You can then later read them back like this:

Any object that you write to the File must implement the Serializable interface which has no methods and for that reason is called a tagging interface. Members declared transient will not be written to the File and there are some other issues that you may want to read about in the javadocs on ObjectInputStream and ObjectOutputStream.
Hope this helps,
Michael Morris
 
Kunal Aher
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply .
But I am using the ATG Dynamo app server and their Repository API. It is a layer above the DB ( similar to JDO ) , and the method signature that they have provided to insert binary data takes a byte[]. There is no method like setObject that can be used. So the deadlock.

Regards
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- Peter
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kunal,
Well you can try something like this:
TestObject.java


ObjectByteArray.java


Hope this helps,
Michael Morris
 
Kunal Aher
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Thanks . Its a great solution. I shall be trying
it out soon.
Let me share with you what I need this for. I am creating a few object , which will be the outcome of xml parsing , some DB lookups etc. But the data in them wont change regularly. So I though about this idea of storing those fully constructed objects in a secondary storage. And the next time I need those objects I will read them from the DB. Now the only thing left to do will be to compare the time to see if there are aany significant differences .The above soln. will allow me to go ahead with my idea.

Thanks once again .

Regards
[ March 04, 2003: Message edited by: Kunal Aher ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic