serialize only a litte part?

P

Peter

Hi
For example, I have serialize a hugh object into file system. A
little part of the object change. How can i only serialize that little
part? or i have to serialize the whole object again?

thanks
from Peter ([email protected])
 
T

Tony Morris

You must serialize the entire object again.
The only other possible alternative is if you know the offset into the file
of the changed data and use a java.io.RandomAccessFile to change it, which,
in my opinion, is not worth it, since it is prone to error and
maintainability issues.
 
N

NOBODY

Note:
BAIS: byte array input stream
BAOS: byte array output stream

Yes you can serialize only a part. Here is the twist:

1-You have to write to a BAOS subclass (to avoid arraycopy and get access
to the buffer, for reuse, and the count)

2-you write 1 instance of all common classes you intend to use to the
object stream (this is the learn phase)
3-you flush, and count how many bytes were written.
4-you write your instance and flush
5-you grab only the byte between the mark and the end and save them.

when reading:

1-prepare a static readonly version of the byte[] prefix created earlier,
this is a working cached version of the data you didn't want to save.
remember that it must be exactly the same classes you wrote in phase 2
above.

2-you load your data in another byte[].
3-either you concat the 2 arrays and wrap the OIS over BAIS over byte[],
or better, you use a OIS over sequenceinputstream over 2 BAIS over the 2
byte[].

4-read all the "dummy" prefix objects
5-read your instance.


NOTE:
-the size is definitely smaller, but be warned that the r/w of dummies
are taking much more CPU now.
-String is natively supported like a primitive, but not arrays of it.
-arrays (of any dimensions) of a class are not learned when the class is
learned.
-write your own Externalizable classes and persist yourself, the
readExternal is about 60%-80% of typical readobjects. And it makes
smaller data.
-this trick make data versionning almost impossible.
-you can detect classes by overriding OOS.annotateClass() and loggin to
console for dev purpose.



(e-mail address removed) (Peter) wrote in @posting.google.com:
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top