public void testWriteBytes () throws IOException { UnsafeOutput buffer = new UnsafeOutput(512); buffer.writeBytes(new byte[] {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26}); buffer.writeBytes(new byte[] {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46}); buffer.writeByte(51); buffer.writeBytes(new byte[] {52, 53, 54, 55, 56, 57, 58}); buffer.writeByte(61); buffer.writeByte(62); buffer.writeByte(63); buffer.writeByte(64); buffer.writeByte(65); buffer.flush(); assertEquals(new byte[] { // 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, // 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, // 51, 52, 53, 54, 55, 56, 57, 58, // 61, 62, 63, 64, 65}, buffer.toBytes()); }
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()); }
@Override public byte[] serialize(T packet) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Output out = new UnsafeOutput(baos); kryo.writeClassAndObject(out, packet); out.close(); return baos.toByteArray(); }
@Override public void write(ObjectDataOutput objectDataOutput, Object object) throws IOException { final Output output = new UnsafeOutput((OutputStream) objectDataOutput); final Kryo kryo = kryoThreadLocal.get(); kryo.writeClassAndObject(output, object); output.flush(); }
@Override public byte[] objectToBytes(Object value) { Output output = new UnsafeOutput(2000, -1); output.writeByte(MARKER_KRYO); kryo.get().writeClassAndObject(output, value); return output.toBytes(); }
@Override public byte[] objectToBytes(Object object) { if (object == null) { return EMPTY; } Output output = new UnsafeOutput(2000, -1); kryo.get().writeClassAndObject(output, object); return output.toBytes(); }
@Override public void write(ObjectDataOutput out, KryoMarker object) throws IOException { String className; byte[] data; if (object instanceof KryoEnvelope) { KryoEnvelope envelope = (KryoEnvelope) object; className = envelope.getClassName(); data = envelope.getData(); } else { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Output output = new UnsafeOutput(baos);) { className = object.getClass().getName(); getKryo().writeObject(output, object); output.flush(); data = baos.toByteArray(); } } out.writeUTF(className); out.writeInt(data.length); out.write(data); }
private byte[] toBytes(SubjectT subject) { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); UnsafeOutput out = new UnsafeOutput(byteOut); kryo.writeObject(out, subject); out.flush(); return byteOut.toByteArray(); }
public void testOutputStream () throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); UnsafeOutput output = new UnsafeOutput(buffer, 2); output.writeBytes(new byte[] {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26}); output.writeBytes(new byte[] {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46}); output.writeBytes(new byte[] {51, 52, 53, 54, 55, 56, 57, 58}); output.writeBytes(new byte[] {61, 62, 63, 64, 65}); output.flush(); assertEquals(new byte[] { // 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, // 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, // 51, 52, 53, 54, 55, 56, 57, 58, // 61, 62, 63, 64, 65}, buffer.toByteArray()); }
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 Output getOutput() { return (isUnsafe)? new UnsafeOutput() : new Output(); }
@Override public Output getOutput(int bufferSize) { return (isUnsafe)? new UnsafeOutput(bufferSize) : new Output(bufferSize); }
@Override public Output getOutput(int bufferSize, int maxBufferSize) { return (isUnsafe)? new UnsafeOutput(bufferSize, maxBufferSize) : new Output(bufferSize, maxBufferSize); }
@Override public Output getOutput(byte[] buffer) { return (isUnsafe)? new UnsafeOutput(buffer) : new Output(buffer); }
@Override public Output getOutput(byte[] buffer, int maxBufferSize) { return (isUnsafe)? new UnsafeOutput(buffer, maxBufferSize) : new Output(buffer, maxBufferSize); }
@Override public Output getOutput(OutputStream outputStream) { return (isUnsafe)? new UnsafeOutput(outputStream) : new Output(outputStream); }
@Override public Output getOutput(OutputStream outputStream, int bufferSize) { return (isUnsafe)? new UnsafeOutput(outputStream, bufferSize) : new Output(outputStream, bufferSize); }
public UnSafeKryoObjectOutput(OutputStream outputStream){ output = new UnsafeOutput(outputStream); }
@Override protected void streamedWrite(OutputStream outputStream, T object) throws IOException { final Output output = new UnsafeOutput(outputStream); get().writeObject(output, object); output.flush(); }