@Override public <T> T deSerialize(final byte[] param, final Class<T> clazz) throws TransactionException { T object; try (ByteArrayInputStream inputStream = new ByteArrayInputStream(param)) { Kryo kryo = new Kryo(); Input input = new Input(inputStream); object = kryo.readObject(input, clazz); input.close(); } catch (IOException e) { throw new TransactionException("kryo deSerialize error" + e.getMessage()); } return object; }
@Override public Object read(Kryo kryo, Input input, Class c) { int len = input.readInt(); byte[] ser = new byte[len]; input.readBytes(ser); ByteArrayInputStream bis = new ByteArrayInputStream(ser); try { ObjectInputStream ois = new ObjectInputStream(bis); return ois.readObject(); } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public TradeCustomer read(Kryo kryo, Input input, Class<TradeCustomer> arg2) { Pair custormer = kryo.readObject(input, Pair.class); Pair trade = kryo.readObject(input, Pair.class); long timeStamp = input.readLong(); String buffer = input.readString(); TradeCustomer inner = new TradeCustomer(timeStamp, trade, custormer, buffer); return inner; }
public List<Object> deserializeStrings(Input input) { List<Object> values = new ArrayList<Object>(); int size = input.readInt(true); for (int i = 0; i < size; i++) { values.add(input.readString()); } return values; }
public T execute(Kryo kryo) { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); Input input = new Input(byteArrayInputStream); return (T) kryo.readClassAndObject(input); } });
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 Object read(Kryo kryo, Input input, Class c) { int len = input.readInt(); byte[] ser = new byte[len]; input.readBytes(ser); ByteArrayInputStream bis = new ByteArrayInputStream(ser); try { ClassLoaderObjectInputStream ois = new ClassLoaderObjectInputStream(kryo.getClassLoader(), bis); return ois.readObject(); } catch (Exception e) { throw new RuntimeException(e); } } }
checkKryoInitialized(); try { return kryo.copy(from); 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());
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; }
@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); } } };
@Test public void testKyro() { Employee employee = new Employee("Rick", "Hightower"); Kryo kryo = new Kryo(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Output output = new Output(outputStream); kryo.writeClassAndObject(output, employee); output.close(); final byte[] bytes = outputStream.toByteArray(); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); Input input = new Input(inputStream); Object object = kryo.readClassAndObject(input); puts(object); }
/** * 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); }
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); }
registration = tryGetFromGlobal(globalIds.get(classID), classID + ""); if (registration == null) throw new KryoException("Encountered unregistered class ID: " + classID); return registration; throw new KryoException("register(registration) is not allowed. Use register(type, serializer)"); throw new KryoException("getRegistration(id) is not implemented"); Output output = new Output(baos); k.writeClassAndObject(output, obj); output.close(); DataInputStream serialized = new DataInputStream(new ByteArrayInputStream(mapOut.toByteArray())); ExternalizableClassResolver resolver2 = new ExternalizableClassResolver(serialized); write("second", k, baos, new A()); // 24 bytes Input input = new Input(new ByteArrayInputStream(baos.toByteArray())); Object read = k.readClassAndObject(input); System.err.println("output " + read); Kryo k = new Kryo(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); write("first", k, baos, new A()); // write takes 78 bytes
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); }
@Override public T deserialize(byte[] b) { Input input = new Input(b); return (T) kryo.readClassAndObject(input); } }
/** * 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())); } }