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()));
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations