BitBufferHelper { public static byte[] toByteArray(Number input) { Class<? extends Number> dataType = input.getClass(); short size = 0; long longValue = input.longValue(); if (dataType == Byte.class || dataType == byte.class) { size = Byte.SIZE; } else if (dataType == Short.class || dataType == short.class) { size = Short.SIZE; } else if (dataType == Integer.class || dataType == int.class) { size = Integer.SIZE; } else if (dataType == Long.class || dataType == long.class) { size = Long.SIZE; } else { throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n"); } int length = size / NetUtils.NUM_BITS_IN_A_BYTE; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK); } return bytes; } private  BitBufferHelper(); static byte getByte(byte[] data); static short getShort(byte[] data, int numBits); static short getShort(byte[] data); static int getInt(byte[] data, int numBits); static int getInt(byte[] data); static long getLong(byte[] data, int numBits); static long getLong(byte[] data); static byte[] getBits(byte[] data, int startOffset, int numBits); static void setByte(byte[] data, byte input, int startOffset, int numBits); static void setBytes(byte[] data, byte[] input, int startOffset, int numBits); static int getMSBMask(int numBits); static int getLSBMask(int numBits); static long toNumber(byte[] array); static long toNumber(@Nonnull byte[] array, int numBits); static byte[] toByteArray(Number input); static byte[] toByteArray(Number input, int numBits); static byte[] shiftBitsToMSB(byte[] inputBytes, int numBits); static byte[] shiftBitsToLSB(byte[] inputBytes, int numBits); static void insertBits(byte[] data, byte[] inputdataLSB, int startOffset, int numBits); static void checkExceptions(byte[] data, int startOffset, int numBits); static final long BYTE_MASK; }
@Test public void testToByteArray() { short sh = Short.MAX_VALUE; byte[] dataShort = new byte[Byte.SIZE / 8]; dataShort = BitBufferHelper.toByteArray(sh); Assert.assertTrue(dataShort[0] == 127); Assert.assertTrue(dataShort[1] == -1); short sh2 = Short.MIN_VALUE; byte[] dataShort2 = new byte[Byte.SIZE / 8]; dataShort2 = BitBufferHelper.toByteArray(sh2); Assert.assertTrue(dataShort2[0] == -128); Assert.assertTrue(dataShort2[1] == 0); short sh3 = 16384; byte[] dataShort3 = new byte[Byte.SIZE / 8]; dataShort3 = BitBufferHelper.toByteArray(sh3); Assert.assertTrue(dataShort3[0] == 64); Assert.assertTrue(dataShort3[1] == 0); short sh4 = 146; byte[] dataShort4 = new byte[Byte.SIZE / 8]; dataShort4 = BitBufferHelper.toByteArray(sh4); Assert.assertTrue(dataShort4[0] == 0); Assert.assertTrue(dataShort4[1] == -110); short sh5 = 5000; byte[] dataShort5 = new byte[Byte.SIZE / 8]; dataShort5 = BitBufferHelper.toByteArray(sh5); Assert.assertTrue(dataShort5[0] == 19); Assert.assertTrue(dataShort5[1] == -120); short sh6 = 5312; byte[] dataShort6 = new byte[Byte.SIZE / 8]; dataShort6 = BitBufferHelper.toByteArray(sh6); Assert.assertTrue(dataShort6[0] == 20); Assert.assertTrue(dataShort6[1] == -64); int int1 = Integer.MAX_VALUE; byte[] dataInt1 = new byte[Integer.SIZE / 8]; dataInt1 = BitBufferHelper.toByteArray(int1); Assert.assertTrue(dataInt1[0] == 127); Assert.assertTrue(dataInt1[1] == -1); Assert.assertTrue(dataInt1[2] == -1); Assert.assertTrue(dataInt1[3] == -1); int int2 = Integer.MIN_VALUE; byte[] dataInt2 = new byte[Integer.SIZE / 8]; dataInt2 = BitBufferHelper.toByteArray(int2); Assert.assertTrue(dataInt2[0] == -128); Assert.assertTrue(dataInt2[1] == 0); Assert.assertTrue(dataInt2[2] == 0); Assert.assertTrue(dataInt2[3] == 0); int int3 = 1077952576; byte[] dataInt3 = new byte[Integer.SIZE / 8]; dataInt3 = BitBufferHelper.toByteArray(int3); Assert.assertTrue(dataInt3[0] == 64); Assert.assertTrue(dataInt3[1] == 64); Assert.assertTrue(dataInt3[2] == 64); Assert.assertTrue(dataInt3[3] == 64); long long1 = Long.MAX_VALUE; byte[] dataLong1 = new byte[Long.SIZE / 8]; dataLong1 = BitBufferHelper.toByteArray(long1); Assert.assertTrue(dataLong1[0] == 127); Assert.assertTrue(dataLong1[1] == -1); Assert.assertTrue(dataLong1[2] == -1); Assert.assertTrue(dataLong1[3] == -1); Assert.assertTrue(dataLong1[4] == -1); Assert.assertTrue(dataLong1[5] == -1); Assert.assertTrue(dataLong1[6] == -1); Assert.assertTrue(dataLong1[7] == -1); long long2 = Long.MIN_VALUE; byte[] dataLong2 = new byte[Long.SIZE / 8]; dataLong2 = BitBufferHelper.toByteArray(long2); Assert.assertTrue(dataLong2[0] == -128); Assert.assertTrue(dataLong2[1] == 0); Assert.assertTrue(dataLong2[2] == 0); Assert.assertTrue(dataLong2[3] == 0); Assert.assertTrue(dataLong2[4] == 0); Assert.assertTrue(dataLong2[5] == 0); Assert.assertTrue(dataLong2[6] == 0); Assert.assertTrue(dataLong2[7] == 0); byte byte1 = Byte.MAX_VALUE; byte[] dataByte1 = new byte[Byte.SIZE / 8]; dataByte1 = BitBufferHelper.toByteArray(byte1); Assert.assertTrue(dataByte1[0] == 127); byte byte2 = Byte.MIN_VALUE; byte[] dataByte2 = new byte[Byte.SIZE / 8]; dataByte2 = BitBufferHelper.toByteArray(byte2); Assert.assertTrue(dataByte2[0] == -128); byte byte3 = 64; byte[] dataByte3 = new byte[Byte.SIZE / 8]; dataByte3 = BitBufferHelper.toByteArray(byte3); Assert.assertTrue(dataByte3[0] == 64); byte byte4 = 32; byte[] dataByte4 = new byte[Byte.SIZE / 8]; dataByte4 = BitBufferHelper.toByteArray(byte4); Assert.assertTrue(dataByte4[0] == 32); }
@Test public void testToByteArrayVariable() { int len = 9; byte[] dataShort = BitBufferHelper.toByteArray(511, len); Assert.assertTrue(dataShort[0] == (byte) 255); Assert.assertTrue(dataShort[1] == (byte) 128); dataShort = BitBufferHelper.toByteArray(511, len); Assert.assertTrue(dataShort[0] == (byte) 255); Assert.assertTrue(dataShort[1] == (byte) 128); dataShort = BitBufferHelper.toByteArray((long) 511, len); Assert.assertTrue(dataShort[0] == (byte) 255); Assert.assertTrue(dataShort[1] == (byte) 128); }