how to serialize and deserialize large objects

112 views
Skip to first unread message

shay hassidim

unread,
Mar 12, 2014, 11:05:34 AM3/12/14
to kryo-...@googlegroups.com
How to serialize and de-serialize large objects?

We see only 4K is serialized.

See below our existing code - should we use the OutputChunked instead?

public byte[] serialize(Object attribute) {

byte[] buffer = null;

synchronized (lock) {

init();

Output output = new Output(new ByteArrayOutputStream());

try {
kryo.writeClassAndObject(output, attribute);

buffer = output.getBuffer();
} finally {
output.close();
}
}

return buffer;
}

public Object deserialize(byte[] buffer) {

Object result = null;

synchronized (lock) {

init();

Input input = new Input(buffer);

try {
result = kryo.readClassAndObject(input);
} finally {
input.close();
}
}

return result;
}

Any help is appreciated.
Thanks
Shay

mongonix

unread,
Mar 12, 2014, 12:45:23 PM3/12/14
to kryo-...@googlegroups.com
Your line is wrong, because it returns only the current content of the buffer, not everything you've ever written to it:
buffer = output.getBuffer();

You should do:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
...
output.close();
buffer = baos.toByteArray();


-Leo

Nate

unread,
Mar 12, 2014, 12:52:25 PM3/12/14
to kryo-users
On Wed, Mar 12, 2014 at 5:45 PM, mongonix <romi...@gmail.com> wrote:
Your line is wrong, because it returns only the current content of the buffer, not everything you've ever written to it:
buffer = output.getBuffer();

You should do:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
...
output.close();
buffer = baos.toByteArray();

ByteArrayOutputStream can be avoided if using the Output settings to grow the buffer. Also see Output#toBytes().

-Nate

 

--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users
---
You received this message because you are subscribed to the Google Groups "kryo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kryo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shay Hassidim

unread,
Mar 12, 2014, 3:30:31 PM3/12/14
to kryo-...@googlegroups.com

Thanks! Is these better:

 

public byte[] serialize(Object attribute) {

                byte[] buffer = null;

 

                init();

 

                ByteArrayOutputStream bois = new ByteArrayOutputStream();

 

                Output output = new Output(bois);

 

                try {

 

                                try {

                                                Kryo kryo = ser.get();

 

                                                kryo.writeClassAndObject(output, attribute);

 

                                                output.flush();

                                                bois.flush();

 

                                                buffer = bois.toByteArray();

                                } finally {

                                                output.close();

                                                bois.close();

                                }

                } catch (IOException e) {

 

                                LOG.error("can not serialize class " + attribute, e);

 

                                throw new HttpSessionSerializationException(

                                                                "can not serialize class " + attribute, e);

                }

                return buffer;

}

 

public Object deserialize(byte[] buffer) {

 

                Object result = null;

 

                init();

 

                Input input = new Input(buffer);

 

                try {

                                Kryo kryo = ser.get();

 

                                result = kryo.readClassAndObject(input);

                } finally {

                                input.close();

                }

 

                return result;

}

Reply all
Reply to author
Forward
0 new messages