Search in sources :

Example 41 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 42 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)

Example 43 with Output

use of com.esotericsoftware.kryo.io.Output in project Paper by pilgr.

the class DbStoragePlainFile method writeTableFile.

/**
     * Attempt to write the file, delete the backup and return true as atomically as
     * possible.  If any exception occurs, delete the new file; next time we will restore
     * from the backup.
     *
     * @param key          table key
     * @param paperTable   table instance
     * @param originalFile file to write new data
     * @param backupFile   backup file to be used if write is failed
     */
private <E> void writeTableFile(String key, PaperTable<E> paperTable, File originalFile, File backupFile) {
    try {
        FileOutputStream fileStream = new FileOutputStream(originalFile);
        final Output kryoOutput = new Output(fileStream);
        getKryo().writeObject(kryoOutput, paperTable);
        kryoOutput.flush();
        fileStream.flush();
        sync(fileStream);
        //also close file stream
        kryoOutput.close();
        // Writing was successful, delete the backup file if there is one.
        //noinspection ResultOfMethodCallIgnored
        backupFile.delete();
    } catch (IOException | KryoException e) {
        // Clean up an unsuccessfully written file
        if (originalFile.exists()) {
            if (!originalFile.delete()) {
                throw new PaperDbException("Couldn't clean up partially-written file " + originalFile, e);
            }
        }
        throw new PaperDbException("Couldn't save table: " + key + ". " + "Backed up table will be used on next read attempt", e);
    }
}
Also used : KryoException(com.esotericsoftware.kryo.KryoException) FileOutputStream(java.io.FileOutputStream) Output(com.esotericsoftware.kryo.io.Output) IOException(java.io.IOException)

Example 44 with Output

use of com.esotericsoftware.kryo.io.Output in project heron by twitter.

the class KryoSerializer method initialize.

@Override
public void initialize(Map<String, Object> config) {
    kryo = new Kryo();
    kryo.setReferences(false);
    kryoOut = new Output(2000, 2000000000);
    kryoIn = new Input(1);
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Output(com.esotericsoftware.kryo.io.Output) Kryo(com.esotericsoftware.kryo.Kryo)

Example 45 with Output

use of com.esotericsoftware.kryo.io.Output in project heron by twitter.

the class HeronPluggableSerializerDelegate method initialize.

@Override
@SuppressWarnings("rawtypes")
public void initialize(Map config) {
    kryo = SerializationFactory.getKryo(config);
    kryoOut = new Output(2000, 2000000000);
    kryoIn = new Input(1);
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Output(com.esotericsoftware.kryo.io.Output)

Aggregations

Output (com.esotericsoftware.kryo.io.Output)67 Kryo (com.esotericsoftware.kryo.Kryo)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)34 Input (com.esotericsoftware.kryo.io.Input)28 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Test (org.junit.Test)11 FileOutputStream (java.io.FileOutputStream)7 IOException (java.io.IOException)6 Test (org.testng.annotations.Test)6 KryoException (com.esotericsoftware.kryo.KryoException)5 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)4 SAMFileHeader (htsjdk.samtools.SAMFileHeader)3 HashMap (java.util.HashMap)3 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)3 Schema (co.cask.cdap.api.data.schema.Schema)2 Slice (com.datatorrent.netlet.util.Slice)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 ObjectOutput (java.io.ObjectOutput)2