• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Convert an Object to ByteArray

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- Peter
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
I AM MIGHTY! Especially when I hold this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic