/** * Serializes the given object into a {@link ByteBuffer} backed by the byte array returned by Kryo serialization. * * @param obj Object to be serialized. */ public ByteBuffer serializeToByteBuffer(Object obj) { output.clear(); kryo.writeClassAndObject(output, obj); return ByteBuffer.wrap(output.getBuffer(), 0, output.position()); }
if (output.position() != 0) { throw new IllegalStateException("The Kryo Output still contains data from a previous " + "serialize call. It has to be flushed or cleared at the end of the serialize call.");
public void writeOutputTo(OutputStream os) throws IOException { os.write(output.getBuffer(), 0, output.position()); }
/** * @see #serializeObject(Object, byte[], int) */ public static ByteBuffer serializeObject(Object o, int maxSize) { Kryo k = getKryoThreadLocalForObjects(); Output out = new Output(DEFAULT_BUFFER_SIZE_BYTES, maxSize); k.writeClassAndObject(out, o); return ByteBuffer.wrap(out.getBuffer(), 0, out.position()); }
private void after(Output output, Object object) { parent.serializingListener().after(output.position(), object); }
private void before(Output output, Object object) { parent.serializingListener().before(output.position(), object); }
private void writeOutput(WriteBuffer out, Output output) { byte[] array = output.getBuffer(); int limit = output.position(); for (int i=0;i<limit;i++) out.putByte(array[i]); }
/** * Write kryo. * * @param <T> the type parameter * @param obj the obj * @param file the file */ public static <T> void writeKryo(T obj, OutputStream file) { try { Output output = new Output(buffer.get()); new KryoReflectionFactorySupport().writeClassAndObject(output, obj); output.close(); IOUtils.write(Arrays.copyOf(output.getBuffer(), output.position()), file); file.close(); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Serializes the given object into a {@link ByteBuffer} backed by the byte array returned by Kryo serialization. * * @param obj Object to be serialized. */ public ByteBuffer serializeToByteBuffer(Object obj) { output.clear(); kryo.writeClassAndObject(output, obj); return ByteBuffer.wrap(output.getBuffer(), 0, output.position()); }
/** * Serializes an arbitrary object into a binary representation, using full * reference tracking and the object graph serializer. * Must be paired with {@code KryoSerializers#fromBytes(byte[], int, int)} or * {@code KryoSerializers#fromBytes(byte[])} */ public static int serializeObject(Object o, byte[] buffer, int position) { Kryo k = getKryoThreadLocalForObjects(); Output out = new Output(buffer, buffer.length); out.setPosition(position); k.writeClassAndObject(out, o); return out.position(); }
public static int toBytes(Object o, byte[] buffer, int position) { Kryo k = kryoForObjectPerThread.get(); Output out = new Output(buffer); out.setPosition(position); k.writeClassAndObject(out, o); return out.position(); }
public static int toBytes(ServiceDocument o, byte[] buffer, int position) { Kryo k = kryoForDocumentPerThread.get(); Output out = new Output(buffer); out.setPosition(position); k.writeClassAndObject(out, o); return out.position(); }
/** * Blocking write to an outputStream * * @param outputStream output stream to write to * @param object object to write * * @return Number of bytes written * * @throws IOException */ public int write(OutputStream outputStream, Object object) throws IOException { output.clear(); output.setPosition(4); kryo.writeClassAndObject(output, object); int length = output.position() - 4; writeIntToWriteBuffer(length); outputStream.write(writeBuffer, 0, output.position()); return length + 4; }
@Override public Slice toByteArray(T o) { final Output output = new Output(32, -1); try { kryo.writeClassAndObject(output, o); } finally { output.close(); } return new Slice(output.getBuffer(), 0, output.position()); }
@Override public void write(Kryo kryo, Output output, ThreadLocal<?> tl) { output.writeBoolean(tl instanceof InheritableThreadLocal); final Object val = tl.get(); final int pos = output.position(); try { kryo.writeClassAndObject(output, val); } catch (RuntimeException e) { if (PRINT_WARNINGS_ON_UNSERIALIZABLE_THREAD_LOCAL) System.err.println("WARNING: Cannot serialize ThreadLocal (" + tl + " = " + val + "), it will be restored as null."); output.setPosition(pos); kryo.writeObject(output, new DEFAULT()); } }
@Override public DataStatePair toDataStatePair(T o) { data.setPosition(0); writeClassAndObject(data, o); if (!pairs.isEmpty()) { state.setPosition(0); for (ClassIdPair cip : pairs) { writeClassAndObject(state, cip); } pairs.clear(); dataStatePair.state = new Slice(state.getBuffer(), 0, state.position()); } else { dataStatePair.state = null; } dataStatePair.data = new Slice(data.getBuffer(), 0, data.position()); return dataStatePair; }
@Override public <T> void serialize(final T object, final MessageBufferOutput buffer) throws IOException { try (Holder k = getKryoHolder()) { final Output output = k.output; // this will allow kryo to grow the buffer as needed. output.setBuffer(buffer.getBuffer(), Integer.MAX_VALUE); output.setPosition(buffer.getPosition()); // set the position to where we already are. kryoRunner.doSerialize(k, output, object); // if we resized then we need to adjust the message buffer if (output.getBuffer() != buffer.getBuffer()) buffer.replace(output.getBuffer()); buffer.setPosition(output.position()); } catch (final KryoException ke) { throw new IOException("Failed to serialize.", ke); } catch (final IllegalArgumentException e) { // this happens when requiring registration but serializing an unregistered class throw new IOException("Failed to serialize " + objectDescription(object) + " (did you require registration and attempt to serialize an unregistered class?)", e); } }
public QueryValidationServiceState serializedAndCompareDocuments( boolean useBinary, QueryValidationServiceState original) throws Throwable { QueryValidationServiceState originalDeserializedWithSig = null; if (useBinary) { Output o = KryoSerializers.serializeDocument(original, 4096); originalDeserializedWithSig = (QueryValidationServiceState) KryoSerializers .deserializeDocument( o.getBuffer(), 0, o.position()); } else { String serializedDocument = Utils.toJson(original); originalDeserializedWithSig = Utils.fromJson( serializedDocument, QueryValidationServiceState.class); } compareDocumentFields(original, originalDeserializedWithSig); return originalDeserializedWithSig; }
@Test public void serializeDocumentForIndexing() { ExampleServiceState state = new ExampleServiceState(); state.documentSelfLink = "selfLink"; state.documentKind = Utils.buildKind(ExampleServiceState.class); Output o = KryoSerializers.serializeDocumentForIndexing(state, 2048); ExampleServiceState deser = (ExampleServiceState) KryoSerializers.deserializeDocument( o.getBuffer(), 0, o.position()); assertNull(deser.documentSelfLink); assertNull(deser.documentKind); }
@Test public void serializeDocumentForIndexing() { ExampleServiceState state = new ExampleServiceState(); state.documentSelfLink = "selfLink"; state.documentKind = Utils.buildKind(ExampleServiceState.class); Output o = KryoSerializers.serializeDocumentForIndexing(state, 2048); ExampleServiceState deser = (ExampleServiceState) KryoSerializers.deserializeDocument( o.getBuffer(), 0, o.position()); assertNull(deser.documentSelfLink); assertNull(deser.documentKind); }