@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(); } }
public static ByteBuffer uncompress(final ByteBuffer in, ByteBuffer out) { if (in.remaining() == 0) return ByteBuffer.allocate(0); final ORDER order = ORDER.fromInt(in.get()); in.order(ByteOrder.LITTLE_ENDIAN); final int in_sz = in.getInt(); if (in_sz != in.remaining() - RAW_BYTE_LENGTH) throw new RuntimeException("Incorrect input length."); final int out_sz = in.getInt(); if (out == null) out = ByteBuffer.allocate(out_sz); else out.limit(out_sz); if (out.remaining() < out_sz) throw new RuntimeException("Output buffer too small to fit " + out_sz + " bytes."); switch (order) { case ZERO: return uncompress_order0_way4(in, out); case ONE: return uncompress_order1_way4(in, out); default: throw new RuntimeException("Unknown rANS order: " + order); } }
public static ByteBuffer uncompress(final ByteBuffer in, ByteBuffer out) { if (in.remaining() == 0) return ByteBuffer.allocate(0); final ORDER order = ORDER.fromInt(in.get()); in.order(ByteOrder.LITTLE_ENDIAN); final int in_sz = in.getInt(); if (in_sz != in.remaining() - RAW_BYTE_LENGTH) throw new RuntimeException("Incorrect input length."); final int out_sz = in.getInt(); if (out == null) out = ByteBuffer.allocate(out_sz); else out.limit(out_sz); if (out.remaining() < out_sz) throw new RuntimeException("Output buffer too small to fit " + out_sz + " bytes."); switch (order) { case ZERO: return uncompress_order0_way4(in, out); case ONE: return uncompress_order1_way4(in, out); default: throw new RuntimeException("Unknown rANS order: " + order); } }
private static void roundTrip(byte[] data) { for (RANS.ORDER order : RANS.ORDER.values()) roundTrip(data, order); }
private static void roundTrip(ByteBuffer data) { for (RANS.ORDER order : RANS.ORDER.values()) { roundTrip(data, order); data.rewind(); } }
public static ORDER fromInt(final int value) { try { return ORDER.values()[value]; } catch (final ArrayIndexOutOfBoundsException e) { throw new RuntimeException("Unknown rANS order: " + value); } } }
/** * 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 testRANSRoundtrip() { for(RANS.ORDER order : RANS.ORDER.values()) { final byte[] compressed = ExternalCompression.rans(TEST_BYTES, order); final byte[] restored = ExternalCompression.unrans(compressed); Assert.assertEquals(TEST_BYTES, restored); } }
/** * 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); }
public static ORDER fromInt(final int value) { try { return ORDER.values()[value]; } catch (final ArrayIndexOutOfBoundsException e) { throw new RuntimeException("Unknown rANS order: " + value); } } }