Container { public void compress(byte[] data, int[] keys) throws IOException { OutputStream stream = new OutputStream(); byte[] compressedData; int length; switch (compression) { case CompressionType.NONE: compressedData = data; length = compressedData.length; break; case CompressionType.BZ2: compressedData = concat(Ints.toByteArray(data.length), BZip2.compress(data)); length = compressedData.length - 4; break; case CompressionType.GZ: compressedData = concat(Ints.toByteArray(data.length), GZip.compress(data)); length = compressedData.length - 4; break; default: throw new RuntimeException("Unknown compression type"); } compressedData = encrypt(compressedData, compressedData.length, keys); stream.writeByte(compression); stream.writeInt(length); stream.writeBytes(compressedData); if (revision != -1) { stream.writeShort(revision); } this.data = stream.flip(); } Container(int compression, int revision); void compress(byte[] data, int[] keys); static Container decompress(byte[] b, int[] keys); public byte[] data; public int compression; public int revision; public int crc; }
@Test public void testCompress() throws IOException { int[] keys = new int[] { 4, 8, 15, 16 }; Random random = new Random(42L); byte[] data = new byte[1024]; random.nextBytes(data); Container container = new Container(GZ, -1); container.compress(data, keys); byte[] compressedData = container.data; container = Container.decompress(compressedData, keys); assertArrayEquals(data, container.data); }