private byte[] getKryoSerializedBytes (final Object obj) { final Kryo kryo = new Kryo(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); final Output output = new Output(os); kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()); kryo.writeClassAndObject(output, obj); output.flush(); return os.toByteArray(); }
public T execute(Kryo kryo) { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); Input input = new Input(byteArrayInputStream); return (T) kryo.readClassAndObject(input); } });
/** * Constructs a {@link DefaultStateSerializer} instance with the given list * of classes registered in kryo. * * @param classesToRegister the classes to register. */ public DefaultStateSerializer(List<Class<?>> classesToRegister) { kryo = new Kryo(); output = new Output(2000, 2000000000); for (Class<?> klazz : classesToRegister) { kryo.register(klazz); } }
/** * Sub classes can customize the Kryo instance by overriding this method * * @return create Kryo instance */ protected Kryo createInstance() { Kryo kryo = new Kryo(); if (classLoader != null) { kryo.setClassLoader(classLoader); } kryo.setReferences(false); for (Class<?> clazz : classes) { kryo.register(clazz); } return kryo; }
public KryoSerialization () { this(new Kryo()); kryo.setReferences(false); kryo.setRegistrationRequired(true); }
public byte[] execute(Kryo kryo) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Output output = new Output(byteArrayOutputStream); kryo.writeClassAndObject(output, object); output.flush(); return byteArrayOutputStream.toByteArray(); } });
checkKryoInitialized(); try { return kryo.copy(from); ByteArrayOutputStream baout = new ByteArrayOutputStream(); Output output = new Output(baout); kryo.writeObject(output, from); output.close(); ByteArrayInputStream bain = new ByteArrayInputStream(baout.toByteArray()); Input input = new Input(bain); return (T)kryo.readObject(input, from.getClass());
protected void testKryoSerialization(final Object original) { final Kryo kryo = KryoUtil.getKryo(); kryo.reset(); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Output output = new Output(baos); kryo.writeClassAndObject(output, original); output.close(); kryo.reset(); final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); final Object deserialized = kryo.readClassAndObject(new Input(bais)); Assert.assertEquals(original, deserialized); }
private static byte[] serializeObjectToKryo(Serializable object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Output output = new Output(baos); Kryo kryo = borrowKryo(); try { kryo.writeObject(output, object); } finally { releaseKryo(kryo); } output.close(); return baos.toByteArray(); }
@Override public TestObject apply() { Input kryoIn = null; try { kryoIn = new Input(new ByteArrayInputStream(out.toByteArray())); return kryo.readObject(kryoIn, TestObject.class); } finally { U.close(kryoIn, log); } } };
/** * Ensure that the Profiler configuration can undergo Kryo serialization which * occurs when the Profiler is running in Storm. */ @Test public void testKryoSerialization() throws Exception { // setup a profiler config to serialize ProfilerConfig expected = ProfilerConfig.fromJSON(profilesToSerialize); assertNotNull(expected); Kryo kryo = new Kryo(); // serialize ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); Output output = new Output(byteStream); kryo.writeObject(output, expected); // validate serialization byte[] bits = output.toBytes(); assertNotNull(bits); // deserialize Input input = new Input(new ByteArrayInputStream(bits)); ProfilerConfig actual = kryo.readObject(input, ProfilerConfig.class); // validate deserialization assertNotNull(actual); assertEquals(expected, actual); }
private Object getKryoDeserializedObject (final byte[] ser) { final Kryo kryo = new Kryo(); final Input input = new Input(new ByteArrayInputStream(ser)); kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()); return kryo.readClassAndObject(input); }
@Override public byte[] serialize(Object obj, int bufferSize) { Kryo kryo = pool.borrow(); try (Output output = new Output(new ByteArrayOutputStream(), bufferSize)) { kryo.writeClassAndObject(output, obj); return output.toBytes(); } finally { pool.release(kryo); } }
public static <T> T deserialize(byte[] buffer, Class<T> clazz) { Kryo kryo = SerializationUtilities.borrowKryo(); kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); T result = null; try { result = kryo.readObject(new Input(new ByteArrayInputStream(buffer)), clazz); } finally { SerializationUtilities.releaseKryo(kryo); } return result; }
private static <T extends Serializable> T deserializeObjectFromKryo(byte[] bytes, Class<T> clazz) { Input inp = new Input(new ByteArrayInputStream(bytes)); Kryo kryo = borrowKryo(); T func = null; try { func = kryo.readObject(inp, clazz); } finally { releaseKryo(kryo); } inp.close(); return func; }
@Override public void apply(TestObject obj) { out.reset(); Output kryoOut = null; try { kryoOut = new Output(out); kryo.writeObject(kryoOut, obj); } finally { U.close(kryoOut, log); } } };
Kryo kryo = new Kryo(); // #### Store to disk... Output output = new Output(new FileOutputStream("file.bin")); SomeClass someObject = ... kryo.writeObject(output, someObject); output.close(); // ### Restore from disk... Input input = new Input(new FileInputStream("file.bin")); SomeClass someObject = kryo.readObject(input, SomeClass.class); input.close();
public KryoSerializer() { kryo = new Kryo(); kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); output = new Output(200, 2000000000); input = new Input(1); }
private void checkKryoInitialized() { if (this.kryo == null) { this.kryo = new Kryo(); Kryo.DefaultInstantiatorStrategy instantiatorStrategy = new Kryo.DefaultInstantiatorStrategy(); instantiatorStrategy.setFallbackInstantiatorStrategy(new StdInstantiatorStrategy()); kryo.setInstantiatorStrategy(instantiatorStrategy); this.kryo.setAsmEnabled(true); this.kryo.register(typeClass); } } // --------------------------------------------------------------------------------------------
/** * 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())); } }