/** * 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); }
/** * Uncompress a rANS data blob into a new byte array. * * @param data compressed data blob * @return uncompressed data */ public static byte[] unrans(final byte[] data) { final ByteBuffer buf = RANS.uncompress(ByteBuffer.wrap(data), null); return toByteArray(buf); }
public static ByteBuffer compress(final ByteBuffer in, final ORDER order, final ByteBuffer out) { if (in.remaining() == 0) return EMPTY_BUFFER; if (in.remaining() < 4) return encode_order0_way4(in, out); switch (order) { case ZERO: return encode_order0_way4(in, out); case ONE: return encode_order1_way4(in, out); default: throw new RuntimeException("Unknown rANS order: " + order); } }
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()); }
private static ByteBuffer encode_order0_way4(final ByteBuffer in, ByteBuffer out_buf) { final int in_size = in.remaining(); out_buf = allocateIfNeeded(in_size, out_buf); final int freqTableStart = PREFIX_BYTE_LENGTH; out_buf.position(freqTableStart); final int[] F = Frequencies.calcFrequencies_o0(in); final RansEncSymbol[] syms = Frequencies.buildSyms_o0(F); final ByteBuffer cp = out_buf.slice(); final int frequencyTable_size = Frequencies.writeFrequencies_o0(cp, F); in.rewind(); final int compressedBlob_size = E04.compress(in, syms, cp); finalizeCompressed(0, out_buf, in_size, frequencyTable_size, compressedBlob_size); return out_buf; }
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); } }
@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(); } }
private static ByteBuffer encode_order1_way4(final ByteBuffer in, ByteBuffer out_buf) { final int in_size = in.remaining(); out_buf = allocateIfNeeded(in_size, out_buf); final int freqTableStart = PREFIX_BYTE_LENGTH; out_buf.position(freqTableStart); final int[][] F = Frequencies.calcFrequencies_o1(in); final RansEncSymbol[][] syms = Frequencies.buildSyms_o1(F); final ByteBuffer cp = out_buf.slice(); final int frequencyTable_size = Frequencies.writeFrequencies_o1(cp, F); in.rewind(); final int compressedBlob_size = E14.compress(in, syms, cp); finalizeCompressed(1, out_buf, in_size, frequencyTable_size, compressedBlob_size); return out_buf; }
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 ByteBuffer encode_order0_way4(final ByteBuffer in, ByteBuffer out_buf) { final int in_size = in.remaining(); out_buf = allocateIfNeeded(in_size, out_buf); final int freqTableStart = PREFIX_BYTE_LENGTH; out_buf.position(freqTableStart); final int[] F = Frequencies.calcFrequencies_o0(in); final RansEncSymbol[] syms = Frequencies.buildSyms_o0(F); final ByteBuffer cp = out_buf.slice(); final int frequencyTable_size = Frequencies.writeFrequencies_o0(cp, F); in.rewind(); final int compressedBlob_size = E04.compress(in, syms, cp); finalizeCompressed(0, out_buf, in_size, frequencyTable_size, compressedBlob_size); return out_buf; }
/** * 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); }
public static ByteBuffer compress(final ByteBuffer in, final ORDER order, final ByteBuffer out) { if (in.remaining() == 0) return EMPTY_BUFFER; if (in.remaining() < 4) return encode_order0_way4(in, out); switch (order) { case ZERO: return encode_order0_way4(in, out); case ONE: return encode_order1_way4(in, out); default: throw new RuntimeException("Unknown rANS order: " + order); } }
/** * Uncompress a rANS data blob into a new byte array. * * @param data compressed data blob * @return uncompressed data */ public static byte[] unrans(final byte[] data) { final ByteBuffer buf = RANS.uncompress(ByteBuffer.wrap(data), null); return toByteArray(buf); }
private static ByteBuffer encode_order1_way4(final ByteBuffer in, ByteBuffer out_buf) { final int in_size = in.remaining(); out_buf = allocateIfNeeded(in_size, out_buf); final int freqTableStart = PREFIX_BYTE_LENGTH; out_buf.position(freqTableStart); final int[][] F = Frequencies.calcFrequencies_o1(in); final RansEncSymbol[][] syms = Frequencies.buildSyms_o1(F); final ByteBuffer cp = out_buf.slice(); final int frequencyTable_size = Frequencies.writeFrequencies_o1(cp, F); in.rewind(); final int compressedBlob_size = E14.compress(in, syms, cp); finalizeCompressed(1, out_buf, in_size, frequencyTable_size, compressedBlob_size); return out_buf; }
/** * 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); }