@Override public KryoMarker read(ObjectDataInput in) throws IOException { String className = in.readUTF(); int dataLength = in.readInt(); byte[] data = new byte[dataLength]; in.readFully(data); try (Input input = new UnsafeInput(data)) { Class<? extends KryoMarker> clazz = (Class<? extends KryoMarker>) Class.forName(className); return getKryo().readObject(input, clazz); } catch (ClassNotFoundException e) { throw new IOException(e); } }
/** * Junk tolerance test. * * <p>Verifies that the Serializer only throws KryoException or IndexOutOfBoundsException. * * <p>TODO(shahan): Allowing IndexOutOfBoundsException and NegativeArraySizeException here is not * ideal, but Kryo itself encodes lengths in the stream and seeking to random lengths triggers * this. */ void testDeserializeJunkData() { Random rng = new Random(0); int numFailures = 0; for (int i = 0; i < DEFAULT_JUNK_INPUTS; ++i) { byte[] junkData = new byte[rng.nextInt(JUNK_LENGTH_UPPER_BOUND)]; rng.nextBytes(junkData); try { UnsafeInput input = new UnsafeInput(junkData); kryo.readObject(input, type); // OK. Junk string was coincidentally parsed. } catch (IndexOutOfBoundsException | NegativeArraySizeException | KryoException e) { // OK. Deserialization of junk failed. ++numFailures; } } assertThat(numFailures).isAtLeast(1); }
public void testStrings () throws IOException { runStringTest(new UnsafeOutput(4096)); runStringTest(new UnsafeOutput(897)); runStringTest(new UnsafeOutput(new ByteArrayOutputStream())); UnsafeOutput write = new UnsafeOutput(21); String value = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234"; write.writeString(value); Input read = new UnsafeInput(write.toBytes()); assertEquals(value, read.readString()); runStringTest(127); runStringTest(256); runStringTest(1024 * 1023); runStringTest(1024 * 1024); runStringTest(1024 * 1025); runStringTest(1024 * 1026); runStringTest(1024 * 1024 * 2); }
private void runCharTest (UnsafeOutput write) throws IOException { write.writeChar((char)0); write.writeChar((char)63); write.writeChar((char)64); write.writeChar((char)127); write.writeChar((char)128); write.writeChar((char)8192); write.writeChar((char)16384); write.writeChar((char)32767); write.writeChar((char)65535); Input read = new UnsafeInput(write.toBytes()); assertEquals(0, read.readChar()); assertEquals(63, read.readChar()); assertEquals(64, read.readChar()); assertEquals(127, read.readChar()); assertEquals(128, read.readChar()); assertEquals(8192, read.readChar()); assertEquals(16384, read.readChar()); assertEquals(32767, read.readChar()); assertEquals(65535, read.readChar()); }
private void runVarIntTest (UnsafeOutput write, ByteArrayOutputStream os) throws IOException { write.writeVarInt(0, true); write.writeVarInt(63, true); write.writeVarInt(64, true); write.writeVarInt(65535, true); assertEquals(6, write.total()); write.flush(); assertEquals(6, os.size()); Input read = new UnsafeInput(os.toByteArray()); assertEquals(0, read.readVarInt(true)); assertEquals(63, read.readVarInt(true)); assertEquals(64, read.readVarInt(true)); assertEquals(65535, read.readVarInt(true)); assertEquals(true, read.eof()); }
@SuppressWarnings("unchecked") @Override public T unserialize(byte[] data) { Input in = new UnsafeInput(new ByteArrayInputStream(data)); T t = (T) kryo.readClassAndObject(in); in.close(); return t; }
@Override public <T> T bytesToObject(byte[] bytes) { if (bytes == null || bytes.length == 0) { return null; } Input input = new UnsafeInput(bytes); return (T) kryo.get().readClassAndObject(input); }
private void runShortTest (UnsafeOutput write) throws IOException { write.writeShort(0); write.writeShort(63); write.writeShort(64); write.writeShort(127); write.writeShort(128); write.writeShort(8192); write.writeShort(16384); write.writeShort(32767); write.writeShort(-63); write.writeShort(-64); write.writeShort(-127); write.writeShort(-128); write.writeShort(-8192); write.writeShort(-16384); write.writeShort(-32768); Input read = new UnsafeInput(write.toBytes()); assertEquals(0, read.readShort()); assertEquals(63, read.readShort()); assertEquals(64, read.readShort()); assertEquals(127, read.readShort()); assertEquals(128, read.readShort()); assertEquals(8192, read.readShort()); assertEquals(16384, read.readShort()); assertEquals(32767, read.readShort()); assertEquals(-63, read.readShort()); assertEquals(-64, read.readShort()); assertEquals(-127, read.readShort()); assertEquals(-128, read.readShort()); assertEquals(-8192, read.readShort()); assertEquals(-16384, read.readShort()); assertEquals(-32768, read.readShort()); }
private void runBooleanTest (UnsafeOutput write) throws IOException { for (int i = 0; i < 100; i++) { write.writeBoolean(true); write.writeBoolean(false); } Input read = new UnsafeInput(write.toBytes()); for (int i = 0; i < 100; i++) { assertEquals(true, read.readBoolean()); assertEquals(false, read.readBoolean()); } }
public void testSimpleVarInt() { final int value = 39117; final Output out = new UnsafeOutput(1024); out.writeVarInt(value, true); out.flush(); final Input in = new UnsafeInput(out.toBytes()); final int actualValue = in.readVarInt(true); assertEquals(value, actualValue); }
@Override public Object read(ObjectDataInput objectDataInput) throws IOException { final Input input = new UnsafeInput((InputStream) objectDataInput); final Kryo kryo = kryoThreadLocal.get(); return kryo.readClassAndObject(input); }
@Override public <T> T valueToObject(byte[] data) { Input input = new UnsafeInput(data); input.read(); return (T) kryo.get().readClassAndObject(input); }
@Override public Input getInput() { return (isUnsafe)? new UnsafeInput() : new Input(); }
@Override public Input getInput(int bufferSize) { return (isUnsafe)? new UnsafeInput(bufferSize) : new Input(bufferSize); }
@Override public Input getInput(byte[] buffer) { return (isUnsafe)? new UnsafeInput(buffer) : new Input(buffer); }
@Override public Input getInput(byte[] buffer, int offset, int count) { return (isUnsafe)? new UnsafeInput(buffer, offset, count) : new Input(buffer, offset, count); }
@Override public Input getInput(InputStream inputStream) { return (isUnsafe)? new UnsafeInput(inputStream) : new Input(inputStream); }
@Override public Input getInput(InputStream inputStream, int bufferSize) { return (isUnsafe)? new UnsafeInput(inputStream, bufferSize) : new Input(inputStream, bufferSize); }
public UnSafeKryoObjectInput(InputStream inputStream){ input = new UnsafeInput(inputStream); }
private SubjectT fromBytes(byte[] bytes) { return kryo.readObject(new UnsafeInput(bytes), type); }
@Override protected T streamedRead(InputStream inputStream) throws IOException { return get().readObject(new UnsafeInput(inputStream), getType()); }
public void runStringTest (UnsafeOutput write) throws IOException { String value1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\rabcdefghijklmnopqrstuvwxyz\n1234567890\t\"!`?'.,;:()[]{}<>|/@\\^$-%+=#_&~*"; String value2 = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234"; write.writeString(""); write.writeString("1"); write.writeString("22"); write.writeString("uno"); write.writeString("dos"); write.writeString("tres"); write.writeString(null); write.writeString(value1); write.writeString(value2); for (int i = 0; i < 127; i++) write.writeString(String.valueOf((char)i)); for (int i = 0; i < 127; i++) write.writeString(String.valueOf((char)i) + "abc"); Input read = new UnsafeInput(write.toBytes()); assertEquals("", read.readString()); assertEquals("1", read.readString()); assertEquals("22", read.readString()); assertEquals("uno", read.readString()); assertEquals("dos", read.readString()); assertEquals("tres", read.readString()); assertEquals(null, read.readString()); assertEquals(value1, read.readString()); assertEquals(value2, read.readString()); for (int i = 0; i < 127; i++) assertEquals(String.valueOf((char)i), read.readString()); for (int i = 0; i < 127; i++) assertEquals(String.valueOf((char)i) + "abc", read.readString()); read.rewind(); assertEquals("", read.readStringBuilder().toString()); assertEquals("1", read.readStringBuilder().toString()); assertEquals("22", read.readStringBuilder().toString()); assertEquals("uno", read.readStringBuilder().toString()); assertEquals("dos", read.readStringBuilder().toString()); assertEquals("tres", read.readStringBuilder().toString()); assertEquals(null, read.readStringBuilder()); assertEquals(value1, read.readStringBuilder().toString()); assertEquals(value2, read.readStringBuilder().toString()); for (int i = 0; i < 127; i++) assertEquals(String.valueOf((char)i), read.readStringBuilder().toString()); for (int i = 0; i < 127; i++) assertEquals(String.valueOf((char)i) + "abc", read.readStringBuilder().toString()); }