BitBufferHelper { public static short getShort(byte[] data, int numBits) { if (numBits > Short.SIZE) { LOG.error("getShort", new BufferException("Container is too small for the number of requested bits")); } int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits; byte[] bits = null; try { bits = getBits(data, startOffset, numBits); } catch (BufferException e) { LOG.error("getBits failed", e); return 0; } return (short) toNumber(bits, numBits); } 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 testGetShort() throws Exception { byte[] data = new byte[2]; data[0] = 7; data[1] = 8; int length = 9; Assert.assertTrue(BitBufferHelper.getShort(data, length) == 264); data[0] = 6; data[1] = 8; short result = BitBufferHelper.getShort(data, length); Assert.assertTrue(result == 8); data[0] = 8; data[1] = 47; result = BitBufferHelper.getShort(data, length); Assert.assertTrue(result == 47); byte[] data1 = new byte[2]; data1[0] = 1; data1[1] = 20; length = 15; result = BitBufferHelper.getShort(data1, length); Assert.assertTrue(result == 276); byte[] data2 = new byte[2]; data2[0] = 64; data2[1] = 99; length = 13; result = BitBufferHelper.getShort(data2, length); Assert.assertTrue(result == 99); byte[] data3 = { 100, 50 }; result = BitBufferHelper.getShort(data3); Assert.assertTrue(result == 25650); }