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

The following examples show how to use com.esotericsoftware.kryo.io.Output#getBuffer() . 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: SimplePipelineCompressor.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public byte[] compress(Object o) throws IOException{
    if(o instanceof BulkWrites){
        BulkWrites bw = (BulkWrites)o;
        return PipelineEncoding.encode(txnOperationFactory,bw);
    }else {
        Output out = new Output(128,-1);
        Kryo kryo = kp.get();
        try{
            kryo.writeObject(out,o);
            out.flush();
            return out.getBuffer();
        }finally{
            kp.returnInstance(kryo);
        }
    }
}
 
Example 2
Source File: DefaultKryoStreamCodec.java    From Bats with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 3
Source File: PercentileSerializerTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 4
Source File: SerializationUtil.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public static byte[] serialize(Object obj) {
	if (obj == null) {
		return null;
	}
	
	Output output = new Output(1024, -1);
	kryos.get().writeClassAndObject(output, obj);
	
	return output.getBuffer();
}
 
Example 5
Source File: KryoValueEncoder.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] apply(Object value) {
    try {
        Object[] kryoAndBuffer = kryoThreadLocal.get();
        Kryo kryo = (Kryo) kryoAndBuffer[0];
        WeakReference<byte[]> ref = (WeakReference<byte[]>) kryoAndBuffer[1];
        byte[] buffer = ref.get();
        if (buffer == null) {
            buffer = new byte[INIT_BUFFER_SIZE];
        }
        Output output = new Output(buffer, -1);

        try {
            if (useIdentityNumber) {
                output.writeInt(IDENTITY_NUMBER);
            }
            kryo.writeClassAndObject(output, value);
            return output.toBytes();
        } finally {
            //reuse buffer if possible
            if (ref.get() == null || buffer != output.getBuffer()) {
                ref = new WeakReference<>(output.getBuffer());
                kryoAndBuffer[1] = ref;
            }
        }
    } catch (Exception e) {
        StringBuilder sb = new StringBuilder("Kryo Encode error. ");
        sb.append("msg=").append(e.getMessage());
        throw new CacheEncodeException(sb.toString(), e);
    }
}
 
Example 6
Source File: Outputs.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 7
Source File: DefaultKryoStreamCodec.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 8
Source File: ApplicationDimensionComputation.java    From streaming-benchmarks with Apache License 2.0 5 votes vote down vote up
@Override
public Slice toByteArray(Aggregate o)
{
  final Output output = new Output(32, -1);
  try {
    kryo.writeClassAndObject(output, o);
  } finally {
    output.close();
  }
  return new Slice(output.getBuffer(), 0, output.position());
}
 
Example 9
Source File: PercentileSerializerTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 10
Source File: PSTaxonomyDatabaseTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSerializeDeserialize() {

    final Map<String,Integer> accessionToTaxMap = new HashMap<>();
    accessionToTaxMap.put("A",1);
    accessionToTaxMap.put("B",1);
    accessionToTaxMap.put("C",2);
    accessionToTaxMap.put("D",3);

    final PSTree tree = new PSTree(1);
    tree.addNode(2, "node2", 1, 0, "genus");
    tree.addNode(3, "node3", 1, 0, "genus");
    tree.addNode(4, "node4", 2, 200, "species");
    tree.addNode(5, "node5", 2, 300, "species");
    tree.addNode(6, "node6", 3, 100, "species");

    final PSTaxonomyDatabase taxonomyDatabase = new PSTaxonomyDatabase(tree, accessionToTaxMap);

    final Kryo kryo = new Kryo();
    final Output output = new Output(new ByteArrayOutputStream());
    kryo.writeObject(output, taxonomyDatabase);

    final Input input = new Input(new ByteArrayInputStream(output.getBuffer()));
    final PSTaxonomyDatabase taxonomyDatabaseTest = kryo.readObject(input, PSTaxonomyDatabase.class);

    Assert.assertEquals(taxonomyDatabaseTest.tree, taxonomyDatabase.tree);
    Assert.assertEquals(taxonomyDatabaseTest.accessionToTaxId, taxonomyDatabase.accessionToTaxId);
}
 
Example 11
Source File: RyaSubGraphKafkaSerDe.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}