/** * 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()); }
public static void clearOutput(Output output) { output.clear(); // 防止hold过大的内存块一直不释放 byte[] bytes = output.getBuffer(); if (bytes == null) { return; } if (bytes.length > MAX_CACHED_BUF_SIZE) { output.setBuffer(new byte[DEFAULT_BUF_SIZE], -1); } }
public static void clearOutput(Output output) { output.clear(); // 防止hold过大的内存块一直不释放 byte[] bytes = output.getBuffer(); if (bytes == null) { return; } if (bytes.length > MAX_CACHED_BUF_SIZE) { output.setBuffer(new byte[DEFAULT_BUF_SIZE], -1); } }
public static byte[] serialize(final Kryo kryo, final Object o) { if (o == null) { throw new NullPointerException("Can't serialize null"); } final Output output = new Output(4096); kryo.writeObject(output, o); output.flush(); return output.getBuffer(); }
/** * Reads a string encrypted by another instance with a shared key */ private void testEncryptsAndDecryptsMessage(Map<String, Object> topoConf) { String testText = "Tetraodontidae is a family of primarily marine and estuarine fish of the order" + " Tetraodontiformes. The family includes many familiar species, which are" + " variously called pufferfish, puffers, balloonfish, blowfish, bubblefish," + " globefish, swellfish, toadfish, toadies, honey toads, sugar toads, and sea" + " squab.[1] They are morphologically similar to the closely related" + " porcupinefish, which have large external spines (unlike the thinner, hidden" + " spines of Tetraodontidae, which are only visible when the fish has puffed up)." + " The scientific name refers to the four large teeth, fused into an upper and" + " lower plate, which are used for crushing the shells of crustaceans and" + " mollusks, their natural prey."; Kryo kryo = new Kryo(); BlowfishTupleSerializer writerBTS = new BlowfishTupleSerializer(kryo, topoConf); BlowfishTupleSerializer readerBTS = new BlowfishTupleSerializer(kryo, topoConf); int bufferSize = 1024; Output output = new Output(bufferSize, bufferSize); Input input = new Input(bufferSize); String[] stringList = testText.split(" "); ListDelegate delegate = new ListDelegate(); delegate.addAll(Arrays.asList(stringList)); writerBTS.write(kryo, output, delegate); input.setBuffer(output.getBuffer()); ListDelegate outDelegate = readerBTS.read(kryo, input, ListDelegate.class); Assert.assertEquals(testText, Joiner.on(" ").join(outDelegate.toArray())); } }
@Override public byte[] serialize(final Object modelObject) { try (final Output kryoOutput = new Output(32 /*bufferSize*/, -1 /*maxBufferSize*/)) { kryo.writeClassAndObject(kryoOutput, modelObject); return kryoOutput.getBuffer(); } } }
@Override public byte[] serialize(final Object modelObject) { Preconditions.checkArgument(modelObject instanceof MessageLite || modelObject instanceof GeneratedMessage, "Cannot serialize non Protobuf object %s", modelObject.getClass()); try (final Output kryoOutput = new Output(32 /*bufferSize*/, -1 /*maxBufferSize*/)) { kryo.writeClassAndObject(kryoOutput, modelObject); return kryoOutput.getBuffer(); } } }
/** * Serializes RyaSubGraph to bytes * @param bundle - RyaSubGraph to be serialized * @return - serialized bytes from RyaSubGraph */ public byte[] toBytes(RyaSubGraph bundle) { Output output = new Output(new ByteArrayOutputStream()); kryo.writeObject(output, bundle, new RyaSubGraphSerializer()); return output.getBuffer(); }
/** * Serializes RyaSubGraph to bytes * @param bundle - RyaSubGraph to be serialized * @return - serialized bytes from RyaSubGraph */ public byte[] toBytes(RyaSubGraph bundle) { Output output = new Output(new ByteArrayOutputStream()); kryo.writeObject(output, bundle, new RyaSubGraphSerializer()); return output.getBuffer(); }
public byte[] serialize(T object) { if (object == null) { return null; } Output output = new Output(1024, -1); kryo.writeClassAndObject(output, object); byte[] bytes = output.getBuffer(); output.close(); return bytes; }
/** * @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 writeOutput(WriteBuffer out, Output output) { byte[] array = output.getBuffer(); int limit = output.position(); for (int i=0;i<limit;i++) out.putByte(array[i]); }
public static void clearOutput(Output output) { output.clear(); // 防止hold过大的内存块一直不释放 byte[] bytes = output.getBuffer(); if (bytes == null) { return; } if (bytes.length > MAX_CACHED_BUF_SIZE) { output.setBuffer(new byte[DEFAULT_BUF_SIZE], -1); } }
public static void clearOutput(Output output) { output.clear(); // 防止hold过大的内存块一直不释放 byte[] bytes = output.getBuffer(); if (bytes == null) { return; } if (bytes.length > MAX_CACHED_BUF_SIZE) { output.setBuffer(new byte[DEFAULT_BUF_SIZE], -1); } }
@Override public byte[] toBytes(T item) { kryo.writeObject(output, item); output.clear(); return output.getBuffer(); }
/** * 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()); }
@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()); }
@Test public void test() { final Kryo kryo = new Kryo(); kryo.register(TestSingleton.class, SingletonSerializer.of("getInstance")); final Output output = new Output(1024); kryo.writeClassAndObject(output, TestSingleton.getInstance()); final Input input = new Input(output.getBuffer()); final TestSingleton deserialized = (TestSingleton) kryo.readClassAndObject(input); assertEquals(TestSingleton.getInstance(), deserialized); }
@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); }