LongUtils { public static byte[] toByteArray(long value) { return toByteArray(value, 8); }  static byte[] toByteArray(long value); static byte[] toByteArray(long value, int numBits); static byte[] toByteArray(long value, int numBits, boolean bigEndian); static byte[] toByteArray(long value, boolean padArrayToTypeLength); static byte[] toByteArray(long value, boolean padArrayToTypeLength, boolean bigEndian);  }
@Test public void testToByteArray() { byte[] expected = new byte[] { (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }; assertEquals(expected, LongUtils.toByteArray(Long.MAX_VALUE)); expected = new byte[] { 0x00 }; assertEquals(expected, LongUtils.toByteArray(0)); expected = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; assertEquals(expected, LongUtils.toByteArray(0, true)); expected = new byte[] { 0x00 }; assertEquals(expected, LongUtils.toByteArray(0, false)); expected = new byte[] { (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }; assertEquals(expected, LongUtils.toByteArray(Long.MAX_VALUE, true)); expected = new byte[] { (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }; assertEquals(expected, LongUtils.toByteArray(Long.MAX_VALUE, true)); expected = new byte[] { 0x01, 0x00, 0x00, 0x00 }; assertEquals(expected, LongUtils.toByteArray(8, 1)); try { expected = new byte[] { 0x01, 0x00, 0x00, 0x00 }; assertEquals(expected, LongUtils.toByteArray(8, 0)); fail("A numbits of '0' should give an IllegalArgumentException"); } catch (IllegalArgumentException e) { } try { expected = new byte[] { 0x01, 0x00, 0x00, 0x00 }; assertEquals(expected, LongUtils.toByteArray(8, -5)); fail("A negative value for numbits should give an IllegalArgumentException"); } catch (IllegalArgumentException e) { } try { expected = new byte[] { 0x01, 0x00, 0x00, 0x00 }; assertEquals(expected, LongUtils.toByteArray(8, 9)); fail("A value above 8 for numbits should give an IllegalArgumentException"); } catch (IllegalArgumentException e) { } }