Objects and RAF Streams

A

aegis

Is it possible to write Objects out with ObjectOutputStream
and then read them back with RandomAccessFile? If so,
how is this usually done? It seems like one would have
to read the Objects back as a byte array and then
convert that to the Object.

Thoughts?
 
A

Arne Vajhøj

aegis said:
Is it possible to write Objects out with ObjectOutputStream
and then read them back with RandomAccessFile? If so,
how is this usually done? It seems like one would have
to read the Objects back as a byte array and then
convert that to the Object.

Because RAF is not InputStream/OutputStream, then I think
you need to over over a byte array.

Have you considered a database instead of RAF?

Arne
 
L

Lew

Of course what you want is possible, but it's difficult and unwise. Point
one: your choice of RandomAccessFile implies that you'd want to dip into
random parts of the file. Doing that, how would you know if and when you're
at the top of an object graph?

Regardless, to do what you want, study carefully the format of a serialized
object stream and of each serialized type (which can change between versions).

Why do you not want to use ObjectInputStream?

Seriously, why don't you?
 
A

Arne Vajhøj

Lew said:
Of course what you want is possible, but it's difficult and unwise.
Point one: your choice of RandomAccessFile implies that you'd want to
dip into random parts of the file. Doing that, how would you know if
and when you're at the top of an object graph?

Regardless, to do what you want, study carefully the format of a
serialized object stream and of each serialized type (which can change
between versions).


Why do you not want to use ObjectInputStream?

Seriously, why don't you?

I read the question as if he would be using ObjectInputStream
but wanted to know whether he could avoid the byte array
(RandomAccessFile can not be wrapped in a ObjectInputStream
like FileInputStream can).

But after reading your post and rereading the original post,
then I am not so sure.

Arne
 
T

Tom Anderson

Is it possible to write Objects out with ObjectOutputStream and then
read them back with RandomAccessFile? If so, how is this usually done?
It seems like one would have to read the Objects back as a byte array
and then convert that to the Object.

It would be very easy to write an InputStream which wrapped a
RandomAccessFile - map the methods as follows:

InputStream RandomAccessFile

available length() - getFilePointer()
read (3 forms) read (3 forms)
skip seek(getFilePointer() + n)
close close (or ignore)
mark long mark = getFilePointer()
markSupported return true
reset seek(mark)

Wrap an ObjectInputStream round that.

This might not actually be what you want to do, though. Tell us more about
your actual goal and we might have more useful ideas.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top