BinaryWriter implements AutoCloseable { public void writeVarInt(long v) throws IOException { if (v < 0) { throw new IllegalArgumentException(); } if (v < 0xFD) { writeByte((byte) v); } else if (v <= 0xFFFF) { writeByte((byte) 0xFD); writeUInt16((int) v); } else if (v <= 0xFFFFFFFFL) { writeByte((byte) 0xFE); writeUInt32(v); } else { writeByte((byte) 0xFF); writeInt64(v); } } BinaryWriter(OutputStream stream); @Override void close(); void flush(); void write(byte[] buffer); void write(byte[] buffer, int index, int length); void writeBoolean(boolean v); void writeByte(byte v); void writeDouble(double v); void writeECPoint(ECPoint v); void writeFixedString(String v, int length); void writeFloat(float v); void writeInt32(int v); void writeInt64(long v); void writeUInt32(long v); void writeSerializableVariableBytes(NeoSerializable v); void writeSerializableVariable(List<? extends NeoSerializable> v); void writeSerializableVariableBytes(List<? extends NeoSerializable> v); void writeSerializableFixed(NeoSerializable v); void writeSerializableFixed(List<? extends NeoSerializable> v); void writeUInt16(int v); void writeVarBytes(byte[] v); void writeVarInt(long v);  }
@Test public void writeVarInt() throws IOException { writer.writeVarInt(0L); assertAndResetStreamContents(new byte[]{(byte) 0x00}); writer.writeVarInt(252); assertAndResetStreamContents(new byte[]{(byte) 0xfc}); writer.writeVarInt(253); assertAndResetStreamContents(new byte[]{(byte) 0xfd, (byte) 0xfd, (byte) 0x00}); writer.writeVarInt(254); assertAndResetStreamContents(new byte[]{(byte) 0xfd, (byte) 0xfe, (byte) 0x00}); writer.writeVarInt(65_534); assertAndResetStreamContents(new byte[]{(byte) 0xfd, (byte) 0xfe, (byte) 0xff}); writer.writeVarInt(65_535); assertAndResetStreamContents(new byte[]{(byte) 0xfd, (byte) 0xff, (byte) 0xff}); writer.writeVarInt(65_536); assertAndResetStreamContents(new byte[]{(byte) 0xfe, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00}); writer.writeVarInt(4_294_967_294L); assertAndResetStreamContents(new byte[]{(byte) 0xfe, (byte) 0xfe, (byte) 0xff, (byte) 0xff, (byte) 0xff}); writer.writeVarInt(4_294_967_295L); assertAndResetStreamContents(new byte[]{(byte) 0xfe, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}); writer.writeVarInt(4_294_967_296L); assertAndResetStreamContents(new byte[]{(byte) 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}); }
@Test(expected = IllegalArgumentException.class) public void failWriteVarInt() throws IOException { writer.writeVarInt(-1L); }