BsUtil { static long readFormattedLong(InputStream inputStream) throws IOException { long result = 0; for (int bitshift = 0; bitshift < 64; bitshift += 8) { result |= ((long) inputStream.read()) << bitshift; } if ((result - NEGATIVE_MASK) > 0) { result = (result & ~NEGATIVE_MASK) * -1; } return result; }    }
@Test public void readFormattedLongTest() throws IOException { byte[] data = { (byte) 0x78, (byte) 0x56, (byte) 0x34, (byte) 0x12, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0xef, (byte) 0xbe, (byte) 0xad, (byte) 0x0e, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; ByteArrayInputStream inputStream = new ByteArrayInputStream(data); Assert.assertEquals(0x12345678, BsUtil.readFormattedLong(inputStream)); Assert.assertEquals(0x0eadbeef, BsUtil.readFormattedLong(inputStream)); }