BinaryWriter implements AutoCloseable { public void writeUInt16(int v) throws IOException { if (v < 0 || v >= (int) Math.pow(2, 16)) { throw new IllegalArgumentException("Value of 16-bit unsigned integer was not in " + "interval [0, 2^16)."); } buffer.putInt(0, v); writer.write(array, 0, 2); } 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 writeUInt16() throws IOException { int uint = (int) Math.pow(2, 16) - 1; writer.writeUInt16(uint); assertAndResetStreamContents(new byte[]{(byte) 0xff, (byte) 0xff}); uint = 0; writer.writeUInt16(uint); assertAndResetStreamContents(new byte[]{0, 0}); uint = 12345; writer.writeUInt16(uint); assertAndResetStreamContents(new byte[]{0x39, 0x30}); }
@Test public void failWritingUInt16NotInRange() throws IOException { try { int uint = (int) Math.pow(2, 16); writer.writeUInt16(uint); fail(); } catch (IllegalArgumentException ignored) { } try { int uint = -1; writer.writeUInt16(uint); fail(); } catch (IllegalArgumentException ignored) { } }