/** * Compress a byte array into rANS blob. * * @param data byte array to compress * @param order rANS order * @return compressed blob */ public static byte[] rans(final byte[] data, final RANS.ORDER order) { final ByteBuffer buffer = RANS.compress(ByteBuffer.wrap(data), order, null); return toByteArray(buffer); }
/** * Compress a byte array into rANS blob. * * @param data byte array to compress * @param order rANS order * @return compressed blob */ public static byte[] rans(final byte[] data, final RANS.ORDER order) { final ByteBuffer buffer = RANS.compress(ByteBuffer.wrap(data), order, null); return toByteArray(buffer); }
/** * Compress a byte array into rANS blob. * * @param data byte array to compress * @param order rANS order * @return compressed blob */ public static byte[] rans(final byte[] data, final int order) { final ByteBuffer buffer = RANS.compress(ByteBuffer.wrap(data), RANS.ORDER.fromInt(order), null); return toByteArray(buffer); }
/** * Compress a byte array into rANS blob. * * @param data byte array to compress * @param order rANS order * @return compressed blob */ public static byte[] rans(final byte[] data, final int order) { final ByteBuffer buffer = RANS.compress(ByteBuffer.wrap(data), RANS.ORDER.fromInt(order), null); return toByteArray(buffer); }
@Test public void testRansHeader() { byte[] data = randomBytes_GD(1000, 0.01); ByteBuffer compressed = RANS.compress(ByteBuffer.wrap(data), RANS.ORDER.ZERO, null); Assert.assertEquals(compressed.get(), (byte) 0); Assert.assertEquals(compressed.getInt(), compressed.limit() - 9); Assert.assertEquals(compressed.getInt(), data.length); }
private static void roundTrip(ByteBuffer data, RANS.ORDER order) { ByteBuffer compressed = RANS.compress(data, order, null); ByteBuffer uncompressed = RANS.uncompress(compressed, null); data.rewind(); while (data.hasRemaining()) { if (!uncompressed.hasRemaining()) Assert.fail("Premature end of uncompressed data."); Assert.assertEquals(uncompressed.get(), data.get()); } Assert.assertFalse(uncompressed.hasRemaining()); }
@Test public void testBuffersMeetBoundaryExpectations() { int size = 1001; ByteBuffer raw = ByteBuffer.wrap(randomBytes_GD(size, 0.01)); for (RANS.ORDER order : RANS.ORDER.values()) { ByteBuffer compressed = RANS.compress(raw, order, null); Assert.assertFalse(raw.hasRemaining()); Assert.assertEquals(raw.limit(), size); Assert.assertEquals(compressed.position(), 0); Assert.assertTrue(compressed.limit() > 10); Assert.assertEquals(compressed.get(), (byte) order.ordinal()); Assert.assertEquals(compressed.getInt(), compressed.limit() - 1 - 4 - 4); Assert.assertEquals(compressed.getInt(), size); compressed.rewind(); ByteBuffer uncompressed = RANS.uncompress(compressed, null); Assert.assertFalse(compressed.hasRemaining()); Assert.assertEquals(uncompressed.limit(), size); Assert.assertEquals(uncompressed.position(), 0); raw.rewind(); } }