TlvEncoder { public static byte[] encodeInteger(Number number) { ByteBuffer iBuf; long lValue = number.longValue(); if (lValue >= Byte.MIN_VALUE && lValue <= Byte.MAX_VALUE) { iBuf = ByteBuffer.allocate(1); iBuf.put((byte) lValue); } else if (lValue >= Short.MIN_VALUE && lValue <= Short.MAX_VALUE) { iBuf = ByteBuffer.allocate(2); iBuf.putShort((short) lValue); } else if (lValue >= Integer.MIN_VALUE && lValue <= Integer.MAX_VALUE) { iBuf = ByteBuffer.allocate(4); iBuf.putInt((int) lValue); } else { iBuf = ByteBuffer.allocate(8); iBuf.putLong(lValue); } return iBuf.array(); }  static ByteBuffer encode(Tlv[] tlvs); static byte[] encodeInteger(Number number); static byte[] encodeFloat(Number number); static byte[] encodeBoolean(boolean value); static byte[] encodeString(String value); static byte[] encodeDate(Date value); static byte[] encodeObjlnk(ObjectLink value);  }
@Test public void encode_short() { byte[] encoded = TlvEncoder.encodeInteger(1234); ByteBuffer bb = ByteBuffer.wrap(encoded); assertEquals(1234, bb.getShort()); assertEquals(0, bb.remaining()); }
@Test public void encode_integer() { byte[] encoded = TlvEncoder.encodeInteger(1245823); ByteBuffer bb = ByteBuffer.wrap(encoded); assertEquals(1245823, bb.getInt()); assertEquals(0, bb.remaining()); }
@Test public void encode_long() { long value = System.currentTimeMillis(); byte[] encoded = TlvEncoder.encodeInteger(value); ByteBuffer bb = ByteBuffer.wrap(encoded); assertEquals(value, bb.getLong()); assertEquals(0, bb.remaining()); }