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