Search in sources :

Example 31 with Input

use of com.esotericsoftware.kryo.io.Input in project MantaroBot by Mantaro.

the class KryoUtils method unserialize.

public static Object unserialize(Kryo kryo, byte[] data) {
    Input in = new Input(new ByteArrayInputStream(checkNotNull(data, "data")));
    Object o = checkNotNull(kryo, "kryo").readClassAndObject(in);
    in.close();
    return o;
}
Also used : Input(com.esotericsoftware.kryo.io.Input) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 32 with Input

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

the class SerializationUtilities method deserializeObjectFromKryo.

private static <T extends Serializable> T deserializeObjectFromKryo(byte[] bytes, Class<T> clazz) {
    Input inp = new Input(new ByteArrayInputStream(bytes));
    Kryo kryo = borrowKryo();
    T func = null;
    try {
        func = kryo.readObject(inp, clazz);
    } finally {
        releaseKryo(kryo);
    }
    inp.close();
    return func;
}
Also used : Input(com.esotericsoftware.kryo.io.Input) ByteArrayInputStream(java.io.ByteArrayInputStream) Kryo(com.esotericsoftware.kryo.Kryo)

Example 33 with Input

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

the class SerializationUtilities method deserializeObjectByKryo.

private static <T> T deserializeObjectByKryo(Kryo kryo, InputStream in, Class<T> clazz) {
    Input inp = new Input(in);
    kryo.setClassLoader(Utilities.getSessionSpecifiedClassLoader());
    T t = kryo.readObject(inp, clazz);
    inp.close();
    return t;
}
Also used : Input(com.esotericsoftware.kryo.io.Input)

Example 34 with Input

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

the class HiveKVResultCache method next.

public synchronized Tuple2<HiveKey, BytesWritable> next() {
    Preconditions.checkState(hasNext());
    if (!readBufferUsed) {
        try {
            if (input == null && output != null) {
                // Close output stream if open
                output.close();
                output = null;
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(tmpFile);
                    input = new Input(fis);
                } finally {
                    if (input == null && fis != null) {
                        fis.close();
                    }
                }
            }
            if (input != null) {
                // Load next batch from disk
                for (int i = 0; i < IN_MEMORY_NUM_ROWS; i++) {
                    ObjectPair<HiveKey, BytesWritable> pair = readBuffer[i];
                    pair.setFirst(readHiveKey(input));
                    pair.setSecond(readValue(input));
                }
                if (input.eof()) {
                    input.close();
                    input = null;
                }
                rowsInReadBuffer = IN_MEMORY_NUM_ROWS;
                readBufferUsed = true;
                readCursor = 0;
            } else if (writeCursor == 1) {
                ObjectPair<HiveKey, BytesWritable> pair = writeBuffer[0];
                Tuple2<HiveKey, BytesWritable> row = new Tuple2<HiveKey, BytesWritable>(pair.getFirst(), pair.getSecond());
                pair.setFirst(null);
                pair.setSecond(null);
                writeCursor = 0;
                return row;
            } else {
                // No record on disk, more data in write buffer
                switchBufferAndResetCursor();
            }
        } catch (Exception e) {
            // Clean up the cache
            clear();
            throw new RuntimeException("Failed to load rows from disk", e);
        }
    }
    ObjectPair<HiveKey, BytesWritable> pair = readBuffer[readCursor];
    Tuple2<HiveKey, BytesWritable> row = new Tuple2<HiveKey, BytesWritable>(pair.getFirst(), pair.getSecond());
    pair.setFirst(null);
    pair.setSecond(null);
    if (++readCursor >= rowsInReadBuffer) {
        readBufferUsed = false;
        rowsInReadBuffer = 0;
        readCursor = 0;
    }
    return row;
}
Also used : Input(com.esotericsoftware.kryo.io.Input) HiveKey(org.apache.hadoop.hive.ql.io.HiveKey) Tuple2(scala.Tuple2) BytesWritable(org.apache.hadoop.io.BytesWritable) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ObjectPair(org.apache.hadoop.hive.common.ObjectPair)

Example 35 with Input

use of com.esotericsoftware.kryo.io.Input in project potato by eyeem.

the class KryoTransportLayer method loadSync.

public boolean loadSync(Storage.List storageList) {
    Kryo kyro = new Kryo();
    Storage storage = storageList.getStorage();
    Class klazz = storage.classname();
    try {
        Input input = new Input(new FileInputStream(filename(storageList)));
        HashMap<String, Object> data = kyro.readObject(input, HashMap.class);
        ArrayList list = (ArrayList) data.get("list");
        input.close();
        Storage.List transaction = storageList.transaction();
        transaction.meta = (HashMap<String, Object>) data.get("meta");
        // don't add objects that already exist in cache as they're most likely fresher
        for (Object loadedObject : list) {
            Object storedObject = storage.get(storage.id(loadedObject));
            transaction.add(storedObject != null ? storedObject : loadedObject);
        }
        transaction.commit(new Storage.Subscription.Action(Storage.Subscription.LOADED));
        return true;
    } catch (FileNotFoundException e) {
        // clean up
        deleteFilesRecursively(getBaseDir(klazz), klazz);
        Log.w(klazz.getSimpleName(), "load() error: file " + filename(storageList) + " missing");
    } catch (Throwable e) {
        Log.e(klazz.getSimpleName(), "load() error", e);
    }
    // prolly should be other thing
    storageList.publish(new Storage.Subscription.Action(Storage.Subscription.LOADED));
    return false;
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) Input(com.esotericsoftware.kryo.io.Input) ObjectStreamClass(java.io.ObjectStreamClass) Kryo(com.esotericsoftware.kryo.Kryo)

Aggregations

Input (com.esotericsoftware.kryo.io.Input)73 Kryo (com.esotericsoftware.kryo.Kryo)37 Output (com.esotericsoftware.kryo.io.Output)28 ByteArrayInputStream (java.io.ByteArrayInputStream)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 Test (org.junit.Test)19 Slice (com.datatorrent.netlet.util.Slice)9 Test (org.testng.annotations.Test)8 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 HashMap (java.util.HashMap)3 Schema (co.cask.cdap.api.data.schema.Schema)2 SAMFileHeader (htsjdk.samtools.SAMFileHeader)2 ObjectInput (java.io.ObjectInput)2