Java Code Examples for com.esotericsoftware.kryo.io.Output#toBytes()

The following examples show how to use com.esotericsoftware.kryo.io.Output#toBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActivationSerializerTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testCanCorrectlySerializeDataValueStorageInteger() throws Exception {
    SQLInteger dvd = new SQLInteger(12);
    ActivationSerializer.DataValueStorage storage = new ActivationSerializer.DataValueStorage(dvd);

    Kryo kryo = kp.get();
    Output out = new Output(4096,-1);
    KryoObjectOutput koo = new KryoObjectOutput(out,kryo);
    koo.writeObject(storage);

    byte[] data = out.toBytes();

    Input input = new Input(data);

    KryoObjectInput koi = new KryoObjectInput(input,kryo);
    ActivationSerializer.DataValueStorage dvs = (ActivationSerializer.DataValueStorage)koi.readObject();

    Assert.assertEquals("Incorrect ser/de", dvd.getString(), ((DataValueDescriptor) dvs.getValue(null)).getString());
}
 
Example 2
Source File: KryoTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testSQLDecimalInt() throws Exception {
    SQLDecimal in = new SQLDecimal();
    in.setValue(1234);

    Output output = new Output(new byte[20],20);
    KryoObjectOutput koo = new KryoObjectOutput(output,kryo);
    koo.writeObject(in);

    byte[] bytes = output.toBytes();
    assertNotNull(bytes);

    Input input = new Input(bytes);
    KryoObjectInput koi = new KryoObjectInput(input,kryo);
    SQLDecimal out = (SQLDecimal) koi.readObject();

    assertNotNull(out);
    assertEquals(in, out);
}
 
Example 3
Source File: EncoderDecoderKryo.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override public byte[] encode(T element) {
  logger.debug("Encoding using kryo.");
  Kryo kryo = kryos.get();
  int size = 0;
  Output output = new Output(0, 500000000);
  try {
    kryo.writeObject(output, element);
    //logger.info("Object size: " + output.toBytes().length);
    logger.debug("Encoding finished.");
    byte [] result = output.toBytes();
    size = result.length;
    return result;
  } finally {
    output.close();
    if(size < MAX_OBJECT_SIZE) { //Small objects will be put back into the pull, larger ones will be thrown away
      kryos.remove();
    }
  }

}
 
Example 4
Source File: ProfileMeasurementTest.java    From metron with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that the {@link ProfileMeasurement} can undergo Kryo serialization which
 * occurs when the Profiler is running in Storm.
 */
@Test
public void testKryoSerialization() {
  assertNotNull(measurement);
  Kryo kryo = new Kryo();

  // serialize
  ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  Output output = new Output(byteStream);
  kryo.writeObject(output, measurement);

  // validate serialization
  byte[] bits = output.toBytes();
  assertNotNull(bits);

  // deserialize
  Input input = new Input(new ByteArrayInputStream(bits));
  ProfileMeasurement actual = kryo.readObject(input, ProfileMeasurement.class);

  // validate deserialization
  assertNotNull(actual);
  assertEquals(measurement, actual);
}
 
Example 5
Source File: KryoTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testSQLRef() throws Exception {
    SQLRef sqlRef = new SQLRef(new HBaseRowLocation(new byte[] {0, 1, 2,3,4,5,6,7,8,9}));

    Output output = new Output(new byte[30],30);
    Serializer serializer = kryo.getSerializer(SQLRef.class);
    serializer.write(kryo, output, sqlRef);
    
    byte[] bytes = output.toBytes();
    assertNotNull(bytes);

    Input input = new Input(new ByteArrayInputStream(bytes), bytes.length);
    SQLRef out = (SQLRef) serializer.read(kryo, input, SQLRef.class);

    assertNotNull(out);
    assertEquals(sqlRef, out);
}
 
Example 6
Source File: KryoSerializer.java    From tx-lcn with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(Object obj) throws SerializerException {
    byte[] bytes;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        //获取kryo对象
        Kryo kryo = new Kryo();
        Output output = new Output(outputStream);
        kryo.writeObject(output, obj);
        bytes = output.toBytes();
        output.flush();
    } catch (Exception ex) {
        throw new SerializerException("kryo serialize error" + ex.getMessage());
    } finally {
        try {
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {

        }
    }
    return bytes;
}
 
Example 7
Source File: SerializerTest.java    From jim-framework with Apache License 2.0 6 votes vote down vote up
public static <T> byte[] serialize(T obj) {
    if (obj == null) {
        throw new RuntimeException("KroySerialize.serialize:obj is null");
    }

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Output ko = new Output(output);

    try {
        kryo.writeObject(ko, obj);
        return ko.toBytes();
    }
    finally {
        ko.close();
    }


}
 
Example 8
Source File: ProfilerConfigTest.java    From metron with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: ProfilePeriodTest.java    From metron with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that the ProfilePeriod can undergo Kryo serialization which
 * occurs when the Profiler is running in Storm.
 */
@Test
public void testKryoSerialization() {
  ProfilePeriod expected = ProfilePeriod.fromTimestamp(AUG2016, 1, TimeUnit.HOURS);
  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));
  ProfilePeriod actual = kryo.readObject(input, ProfilePeriod.class);

  // validate deserialization
  assertNotNull(actual);
  assertEquals(expected, actual);
}
 
Example 10
Source File: GrpcTxEventEndpointImpl.java    From txle with Apache License 2.0 5 votes vote down vote up
private byte[] serialize(Object[] objects) {
    Output output = new Output(4096, -1);
    Kryo kryo = pool.borrow();
    kryo.writeObjectOrNull(output, objects, Object[].class);
    pool.release(kryo);
    return output.toBytes();
}
 
Example 11
Source File: KryoSerializer.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
@Override
public <T> byte[] writeObject(T obj) {
    Output output = Outputs.getOutput();
    Kryo kryo = kryoThreadLocal.get();
    try {
        kryo.writeObject(output, obj);
        return output.toBytes();
    } finally {
        Outputs.clearOutput(output);
    }
}
 
Example 12
Source File: KryoTranscoder.java    From hibernate4-memcached with Apache License 2.0 5 votes vote down vote up
private byte[] kryoEncode(Object o) {
    Kryo kryo = createKryo();

    Output output = new Output(bufferSize, getMaxSize());
    kryo.writeClassAndObject(output, o);
    return output.toBytes();
}
 
Example 13
Source File: KryoMessageFormat.java    From servicecomb-pack with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Object[] objects) {
  Output output = new Output(DEFAULT_BUFFER_SIZE, -1);

  Kryo kryo = pool.borrow();
  kryo.writeObjectOrNull(output, objects, Object[].class);
  pool.release(kryo);

  return output.toBytes();
}
 
Example 14
Source File: KryoSerializer.java    From bird-java with MIT License 5 votes vote down vote up
@Override
public byte[] serialize(final Object obj) {
    byte[] bytes;
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        //获取kryo对象
        Kryo kryo = new Kryo();
        Output output = new Output(outputStream);
        kryo.writeClassAndObject(output, obj);
        bytes = output.toBytes();
        output.flush();
    } catch (IOException ex) {
        throw new SerializerException("kryo serialize error" + ex.getMessage());
    }
    return bytes;
}
 
Example 15
Source File: SerializeUtils.java    From blockchain-java with Apache License 2.0 5 votes vote down vote up
/**
 * 序列化
 *
 * @param object 需要序列化的对象
 * @return
 */
public static byte[] serialize(Object object) {
    Output output = new Output(4096, -1);
    new Kryo().writeClassAndObject(output, object);
    byte[] bytes = output.toBytes();
    output.close();
    return bytes;
}
 
Example 16
Source File: HiveStreamCodec.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public void writeExternal(ObjectOutput out) throws IOException
{

  ByteArrayOutputStream os = new ByteArrayOutputStream();
  ObjectOutputStream obj = new ObjectOutputStream(os);
  Output output = new Output(obj);
  kryo.writeClassAndObject(output, rollingOperator);
  byte[] outBytes = output.toBytes();
  out.writeInt(outBytes.length);
  out.write(outBytes, 0, outBytes.length);
  out.flush();
}
 
Example 17
Source File: KryoUtils.java    From kylin with Apache License 2.0 4 votes vote down vote up
public static byte[] serialize(Object obj) {
    Kryo kryo = getKryo();
    Output output = new Output(1024, 8 * 1024 * 1024);
    kryo.writeObject(output, obj);
    return output.toBytes();
}
 
Example 18
Source File: PartitionedEventSerializerTest.java    From eagle with Apache License 2.0 4 votes vote down vote up
private byte[] kryoSerialize(Object object) {
    Kryo kryo = new DefaultKryoFactory.KryoSerializableDefault();
    Output output = new Output(100000);
    kryo.writeClassAndObject(output, object);
    return output.toBytes();
}
 
Example 19
Source File: KryoCodec.java    From jvm-serializer with Apache License 2.0 4 votes vote down vote up
public static byte[] encode(Object object) throws Exception {
    //4K
    Output output = new Output(4096);
    getKryo().writeClassAndObject(output, object);
    return output.toBytes();
}
 
Example 20
Source File: PartitionedEventSerializerTest.java    From eagle with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void testPartitionEventSerializationEfficiency() throws IOException {
    PartitionedEvent partitionedEvent = MockSampleMetadataFactory.createPartitionedEventGroupedByName("sampleStream", System.currentTimeMillis());
    ;
    PartitionedEventSerializerImpl serializer = new PartitionedEventSerializerImpl(MockSampleMetadataFactory::createSampleStreamDefinition);

    int count = 100000;
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    int i = 0;
    while (i < count) {
        ByteArrayDataOutput dataOutput1 = ByteStreams.newDataOutput();
        serializer.serialize(partitionedEvent, dataOutput1);
        byte[] serializedBytes = dataOutput1.toByteArray();
        PartitionedEvent deserializedEvent = serializer.deserialize(ByteStreams.newDataInput(serializedBytes));
        Assert.assertEquals(partitionedEvent, deserializedEvent);
        i++;
    }
    stopWatch.stop();
    LOG.info("Cached Stream: {} ms", stopWatch.getTime());
    stopWatch.reset();
    PartitionedEventSerializerImpl compressSerializer = new PartitionedEventSerializerImpl(MockSampleMetadataFactory::createSampleStreamDefinition, true);
    i = 0;
    stopWatch.start();
    while (i < count) {
        byte[] serializedBytesCompressed = compressSerializer.serialize(partitionedEvent);
        PartitionedEvent deserializedEventCompressed = compressSerializer.deserialize(serializedBytesCompressed);
        Assert.assertEquals(partitionedEvent, deserializedEventCompressed);
        i++;
    }
    stopWatch.stop();
    LOG.info("Compressed Cached Stream: {} ms", stopWatch.getTime());
    stopWatch.reset();

    i = 0;
    stopWatch.start();
    while (i < count) {
        PartitionedEventDigestSerializer serializer2 = new PartitionedEventDigestSerializer(MockSampleMetadataFactory::createSampleStreamDefinition);
        ByteArrayDataOutput dataOutput2 = ByteStreams.newDataOutput();
        serializer2.serialize(partitionedEvent, dataOutput2);
        byte[] serializedBytes2 = dataOutput2.toByteArray();
        ByteArrayDataInput dataInput2 = ByteStreams.newDataInput(serializedBytes2);
        PartitionedEvent deserializedEvent2 = serializer2.deserialize(dataInput2);
        Assert.assertEquals(partitionedEvent, deserializedEvent2);
        i++;
    }
    stopWatch.stop();
    LOG.info("Cached Stream&Partition: {} ms", stopWatch.getTime());
    stopWatch.reset();
    i = 0;
    stopWatch.start();
    while (i < count) {
        byte[] javaSerialization = new DefaultSerializationDelegate().serialize(partitionedEvent);
        PartitionedEvent javaSerializedEvent = (PartitionedEvent) new DefaultSerializationDelegate().deserialize(javaSerialization);
        Assert.assertEquals(partitionedEvent, javaSerializedEvent);
        i++;
    }
    stopWatch.stop();
    LOG.info("Java Native: {} ms", stopWatch.getTime());
    stopWatch.reset();
    i = 0;
    stopWatch.start();
    Kryo kryo = new DefaultKryoFactory.KryoSerializableDefault();
    while (i < count) {
        Output output = new Output(10000);
        kryo.writeClassAndObject(output, partitionedEvent);
        byte[] kryoBytes = output.toBytes();
        Input input = new Input(kryoBytes);
        PartitionedEvent kryoDeserializedEvent = (PartitionedEvent) kryo.readClassAndObject(input);
        Assert.assertEquals(partitionedEvent, kryoDeserializedEvent);
        i++;
    }
    stopWatch.stop();
    LOG.info("Kryo: {} ms", stopWatch.getTime());
}