public void writeLength(int length) throws IOException { bitCount = 0; writeInt(length); }
public void writeString(String string) throws IOException { byte[] bytes = string.getBytes(UTF8); writeInt(bytes.length); write(bytes, 0, bytes.length); }
public void writeBytes(byte[] bytes, int start, int len) { writeInt(len); write(bytes, start, len); }
void write(OutputBuffer out) throws IOException { out.writeInt(size()); for (Map.Entry<String,byte[]> e : entrySet()) { out.writeString(e.getKey()); out.writeBytes(e.getValue()); } }
@Test public void testInt() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeInt(random.nextInt()); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); random = TestUtil.createRandom(); for (int i = 0; i < COUNT; i++) Assert.assertEquals(random.nextInt(), in.readInt()); }
writeBoolean((Boolean)value); break; case INT: writeInt((Integer)value); break; case LONG: writeLong((Long)value); break;
@Test public void testZero() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); out.writeInt(0); byte[] bytes = out.toByteArray(); Assert.assertEquals(1, bytes.length); Assert.assertEquals(0, bytes[0]); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); Assert.assertEquals(0, in.readInt()); }
public void writeString(String string) throws IOException { byte[] bytes = string.getBytes(UTF8); writeInt(bytes.length); write(bytes, 0, bytes.length); }
@Override public void writeLength(int length) throws IOException { assert this.length == 0; this.length = length; getBuffer().writeInt(length); }
public void writeBytes(byte[] bytes, int start, int len) throws IOException { writeInt(len); write(bytes, start, len); }
void write(OutputBuffer out) throws IOException { out.writeInt(size()); for (Map.Entry<String,byte[]> e : entrySet()) { out.writeString(e.getKey()); out.writeBytes(e.getValue()); } }
@Test public void testInt() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); for (int i = 0; i < COUNT; i++) out.writeInt(random.nextInt()); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); random = TestUtil.createRandom(); for (int i = 0; i < COUNT; i++) Assert.assertEquals(random.nextInt(), in.readInt()); }
public void writeValue(Object value, ValueType type) throws IOException { switch (type) { case NULL: break; case INT: writeInt((Integer)value); break; case LONG: writeLong((Long)value); break; case FIXED32: writeFixed32((Integer)value); break; case FIXED64: writeFixed64((Long)value); break; case FLOAT: writeFloat((Float)value); break; case DOUBLE: writeDouble((Double)value); break; case STRING: writeString((String)value); break; case BYTES: if (value instanceof ByteBuffer) writeBytes((ByteBuffer)value); else writeBytes((byte[])value); break; default: throw new TrevniRuntimeException("Unknown value type: "+type); } }
@Test public void testZero() throws Exception { Random random = TestUtil.createRandom(); OutputBuffer out = new OutputBuffer(); out.writeInt(0); byte[] bytes = out.toByteArray(); Assert.assertEquals(1, bytes.length); Assert.assertEquals(0, bytes[0]); InputBuffer in = new InputBuffer(new InputBytes(out.toByteArray())); Assert.assertEquals(0, in.readInt()); }