Search in sources :

Example 21 with Output

use of com.esotericsoftware.kryo.io.Output in project cdap by caskdata.

the class KryoSerializerTest method testStructuredRecordSerializer.

@Test
public void testStructuredRecordSerializer() throws IOException {
    Schema schema = createSchema();
    StructuredRecord record = StructuredRecord.builder(schema).set("boolean", true).set("int", 10).set("long", 1L + Integer.MAX_VALUE).set("float", 1.5f).set("double", 2.25d).set("string", "Hello World").set("bytes", "Hello Bytes".getBytes(StandardCharsets.UTF_8)).set("enum", "a").set("array", new int[] { 1, 2, 3 }).set("map", ImmutableMap.of("1", 1, "2", 2, "3", 3)).set("union", null).build();
    Kryo kryo = new Kryo();
    kryo.addDefaultSerializer(Schema.class, SchemaSerializer.class);
    kryo.addDefaultSerializer(StructuredRecord.class, StructuredRecordSerializer.class);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (Output output = new Output(bos)) {
        kryo.writeObject(output, record);
    }
    Input input = new Input(bos.toByteArray());
    StructuredRecord newRecord = kryo.readObject(input, StructuredRecord.class);
    // The StructuredRecord.equals is broken, Json it and compare for now
    Assert.assertEquals(StructuredRecordStringConverter.toJsonString(record), StructuredRecordStringConverter.toJsonString(newRecord));
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Schema(co.cask.cdap.api.data.schema.Schema) Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StructuredRecord(co.cask.cdap.api.data.format.StructuredRecord) Kryo(com.esotericsoftware.kryo.Kryo) Test(org.junit.Test)

Example 22 with Output

use of com.esotericsoftware.kryo.io.Output in project flink by apache.

the class KryoSerializer method serialize.

@Override
public void serialize(T record, DataOutputView target) throws IOException {
    checkKryoInitialized();
    if (target != previousOut) {
        DataOutputViewStream outputStream = new DataOutputViewStream(target);
        output = new Output(outputStream);
        previousOut = target;
    }
    // otherwise data might be written multiple times in case of a previous EOFException
    if (output.position() != 0) {
        throw new IllegalStateException("The Kryo Output still contains data from a previous " + "serialize call. It has to be flushed or cleared at the end of the serialize call.");
    }
    try {
        kryo.writeClassAndObject(output, record);
        output.flush();
    } catch (KryoException ke) {
        // make sure that the Kryo output buffer is cleared in case that we can recover from
        // the exception (e.g. EOFException which denotes buffer full)
        output.clear();
        Throwable cause = ke.getCause();
        if (cause instanceof EOFException) {
            throw (EOFException) cause;
        } else {
            throw ke;
        }
    }
}
Also used : KryoException(com.esotericsoftware.kryo.KryoException) DataOutputViewStream(org.apache.flink.api.java.typeutils.runtime.DataOutputViewStream) Output(com.esotericsoftware.kryo.io.Output) EOFException(java.io.EOFException)

Example 23 with Output

use of com.esotericsoftware.kryo.io.Output in project storm by apache.

the class BlowfishTupleSerializerTest method testEncryptsAndDecryptsMessage.

/**
     * Reads a string encrypted by another instance with a shared key
     */
@Test
public void testEncryptsAndDecryptsMessage() {
    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();
    String arbitraryKey = "7dd6fb3203878381b08f9c89d25ed105";
    Map stormConf = ImmutableMap.of(BlowfishTupleSerializer.SECRET_KEY, arbitraryKey);
    BlowfishTupleSerializer writerBTS = new BlowfishTupleSerializer(kryo, stormConf);
    BlowfishTupleSerializer readerBTS = new BlowfishTupleSerializer(kryo, stormConf);
    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()));
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Output(com.esotericsoftware.kryo.io.Output) ListDelegate(org.apache.storm.utils.ListDelegate) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) HashMap(java.util.HashMap) Kryo(com.esotericsoftware.kryo.Kryo) Test(org.junit.Test)

Example 24 with Output

use of com.esotericsoftware.kryo.io.Output in project storm by apache.

the class KinesisConnectionInfo method getKryoSerializedBytes.

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();
}
Also used : StdInstantiatorStrategy(org.objenesis.strategy.StdInstantiatorStrategy) Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Kryo(com.esotericsoftware.kryo.Kryo)

Example 25 with Output

use of com.esotericsoftware.kryo.io.Output in project hive by apache.

the class KryoMessageCodec method encode.

@Override
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf buf) throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    Output kryoOut = new Output(bytes);
    kryos.get().writeClassAndObject(kryoOut, msg);
    kryoOut.flush();
    byte[] msgData = maybeEncrypt(bytes.toByteArray());
    LOG.debug("Encoded message of type {} ({} bytes)", msg.getClass().getName(), msgData.length);
    checkSize(msgData.length);
    buf.ensureWritable(msgData.length + 4);
    buf.writeInt(msgData.length);
    buf.writeBytes(msgData);
}
Also used : Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Output (com.esotericsoftware.kryo.io.Output)51 Kryo (com.esotericsoftware.kryo.Kryo)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 Input (com.esotericsoftware.kryo.io.Input)21 ByteArrayInputStream (java.io.ByteArrayInputStream)12 Test (org.junit.Test)8 FileOutputStream (java.io.FileOutputStream)7 Test (org.testng.annotations.Test)6 IOException (java.io.IOException)5 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 KryoException (com.esotericsoftware.kryo.KryoException)4 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)4 SAMFileHeader (htsjdk.samtools.SAMFileHeader)3 Schema (co.cask.cdap.api.data.schema.Schema)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 HashMap (java.util.HashMap)2 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)2 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)1 ClassIdPair (com.datatorrent.stram.codec.DefaultStatefulStreamCodec.ClassIdPair)1