Search in sources :

Example 21 with Input

use of com.esotericsoftware.kryo.io.Input 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 22 with Input

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

the class KryoMessageCodec method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 4) {
        return;
    }
    in.markReaderIndex();
    int msgSize = in.readInt();
    checkSize(msgSize);
    if (in.readableBytes() < msgSize) {
        // Incomplete message in buffer.
        in.resetReaderIndex();
        return;
    }
    try {
        ByteBuffer nioBuffer = maybeDecrypt(in.nioBuffer(in.readerIndex(), msgSize));
        Input kryoIn = new Input(new ByteBufferInputStream(nioBuffer));
        Object msg = kryos.get().readClassAndObject(kryoIn);
        LOG.debug("Decoded message of type {} ({} bytes)", msg != null ? msg.getClass().getName() : msg, msgSize);
        out.add(msg);
    } finally {
        in.skipBytes(msgSize);
    }
}
Also used : Input(com.esotericsoftware.kryo.io.Input) ByteBufferInputStream(com.esotericsoftware.kryo.io.ByteBufferInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 23 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 24 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 25 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)

Aggregations

Input (com.esotericsoftware.kryo.io.Input)49 Kryo (com.esotericsoftware.kryo.Kryo)31 Output (com.esotericsoftware.kryo.io.Output)21 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Test (org.junit.Test)8 Test (org.testng.annotations.Test)8 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)5 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)5 StdInstantiatorStrategy (org.objenesis.strategy.StdInstantiatorStrategy)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Schema (co.cask.cdap.api.data.schema.Schema)2 SAMFileHeader (htsjdk.samtools.SAMFileHeader)2 ArrayList (java.util.ArrayList)2 HiveKey (org.apache.hadoop.hive.ql.io.HiveKey)2 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)2