LittleEndianUtils { public static void writeUnsigned4Le(@NonNull ByteBuffer output, long value) throws IOException { Preconditions.checkNotNull(output, "output == null"); Preconditions.checkArgument(value >= 0, "value (%s) < 0", value); Preconditions.checkArgument(value <= 0x00000000ffffffffL, "value (%s) > 0x00000000ffffffffL", value); output.put((byte) (value & 0xff)); output.put((byte) ((value >> 8) & 0xff)); output.put((byte) ((value >> 16) & 0xff)); output.put((byte) ((value >> 24) & 0xff)); } private  LittleEndianUtils(); static long readUnsigned4Le(@NonNull ByteBuffer bytes); static int readUnsigned2Le(@NonNull ByteBuffer bytes); static void writeUnsigned4Le(@NonNull ByteBuffer output, long value); static void writeUnsigned2Le(@NonNull ByteBuffer output, int value);  }
@Test public void write4Le() throws Exception { ByteBuffer out = ByteBuffer.allocate(4); LittleEndianUtils.writeUnsigned4Le(out, 0x01020304); assertArrayEquals(new byte[] { 4, 3, 2, 1 }, out.array()); out = ByteBuffer.allocate(4); LittleEndianUtils.writeUnsigned4Le(out, 0xfedcba98L); assertArrayEquals(new byte[] { (byte) 0x98, (byte) 0xba, (byte) 0xdc, (byte) 0xfe }, out.array()); }