@Override public Measurement deserialize(String topic, byte[] bytes) { // System.out.println("topic" + topic); // System.out.println("bytes" + bytes); // System.out.println("bytes" + bytes.length); // System.out.println("bytse: " + Arrays.toString(bytes)); // System.out.println("kryos: " + kryos.get()); // if ((topic.equals("proteus-realtime") || // (topic.equals("proteus-flatness")) && bytes.length > 40)) { // System.out.println("Escape"); // return null; // } if (topic.equals(ProteusData.get("kafka.topicName")) && bytes.length < 40) { return kryos.get().readObject(new ByteBufferInput(bytes), SensorMeasurement.class); } else if (topic.equals(ProteusData.get("kafka.flatnessTopicName")) && bytes.length < 40) { return kryos.get().readObject(new ByteBufferInput(bytes), SensorMeasurement.class); } else if (topic.equals(ProteusData.get("kafka.hsmTopicName"))) { return kryos.get().readObject(new ByteBufferInput(bytes), HSMMeasurement.class); } else { throw new IllegalArgumentException("Illegal argument: " + topic); } }
/** * Deserializes given byte buffer to Object using Kryo instance in pool. * * @param buffer input with serialized bytes * @param <T> deserialized Object type * @return deserialized Object */ public <T> T deserialize(final ByteBuffer buffer) { ByteBufferInput in = new ByteBufferInput(buffer); Kryo kryo = borrow(); try { @SuppressWarnings("unchecked") T obj = (T) kryo.readClassAndObject(in); return obj; } finally { release(kryo); } }
/** * Deserializes given InputStream to an Object using Kryo instance in pool. * * @param stream input stream * @param <T> deserialized Object type * @return deserialized Object * @param bufferSize size of the buffer in front of the stream */ public <T> T deserialize(final InputStream stream, final int bufferSize) { ByteBufferInput in = new ByteBufferInput(stream, bufferSize); Kryo kryo = borrow(); try { @SuppressWarnings("unchecked") T obj = (T) kryo.readClassAndObject(in); return obj; } finally { release(kryo); } }
@Override public SensorReading deserialize(String s, byte[] bytes) { try { return kryos.get().readObject(new ByteBufferInput(bytes), SensorReading.class); } catch(Exception e) { throw new IllegalArgumentException("Error reading bytes",e); } }
public static String getStringFromBytes(final byte [] bytes){ Kryo kryo = KryoPool.getInstance().getKryo(); ByteBufferInput in = new ByteBufferInput(bytes.length+5); in.setBuffer(bytes); String str = kryo.readObject(in,String.class); in.close(); KryoPool.getInstance().returnToPool(kryo); return str; }
public KryoSerialization (Kryo kryo) { this.kryo = kryo; kryo.register(RegisterTCP.class); kryo.register(RegisterUDP.class); kryo.register(KeepAlive.class); kryo.register(DiscoverHost.class); kryo.register(Ping.class); input = new ByteBufferInput(); output = new ByteBufferOutput(); }
/** * Deserializes given InputStream to an Object using Kryo instance in pool. * * @param stream input stream * @param <T> deserialized Object type * @param bufferSize size of the buffer in front of the stream * @return deserialized Object */ public <T> T deserialize(final InputStream stream, final int bufferSize) { ByteBufferInput in = new ByteBufferInput(stream, bufferSize); Kryo kryo = borrow(); try { @SuppressWarnings("unchecked") T obj = (T) kryo.readClassAndObject(in); return obj; } finally { release(kryo); } }
public KryoSerialization(Kryo kryo) { this.kryo = kryo; kryo.register(RegisterTCP.class); kryo.register(RegisterUDP.class); kryo.register(KeepAlive.class); kryo.register(DiscoverHost.class); kryo.register(Ping.class); input = new ByteBufferInput(); output = new ByteBufferOutput(); }
public static void main(String [] args){ String str = "India,Search,Asia"; System.out.println("string is " + str.getBytes()); Kryo kryo =KryoPool.getInstance().getKryo(); ByteBufferOutput bufferOutput = new ByteBufferOutput(20); kryo.writeObject(bufferOutput, str); byte[] msgBytes = bufferOutput.toBytes(); System.out.println(msgBytes.length); ByteBufferInput input = new ByteBufferInput(msgBytes.length); input.setBuffer(msgBytes); String str2 = kryo.readObject(input,String.class); System.out.println(str2); KryoPool.getInstance().returnToPool(kryo); }
public static <T> T load (Class<T> type) { Input input = new ByteBufferInput(output.getBuffer()); return kryo.readObjectOrNull(input, type); }