/* package whatever; // don't place package name! */ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.Random; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.zip.DataFormatException; import java.util.zip.Deflater; import java.util.zip.Inflater; class ZlibMain { private static byte[] compress(final byte[] data) { deflater.setInput(data); deflater.finish(); final int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed); final byte[] returnValues = new byte[numberOfBytesAfterCompression]; return returnValues; } private static byte[] decompress(final byte[] data) { inflater.setInput(data); while (!inflater.finished()) { final int count = inflater.inflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); final byte[] output = outputStream.toByteArray(); return output; } } roundTrip(100); roundTrip(1000); roundTrip(10000); roundTrip(20000); roundTrip(30000); roundTrip(32000); for (int i = 32700; i < 33000; i++) { if(!roundTrip(i))break; } } private static boolean roundTrip(final int i) { final byte[] data = new byte[i]; for (int j = 0; j < data.length; j++) { data[j]= (byte) j; } shuffleArray(data); final byte[] compressed = compress(data); try { final byte[] decompressed = CompletableFuture.supplyAsync(() -> decompress(compressed)) .get(2, TimeUnit.SECONDS); return true; return false; } } // Implementing Fisher–Yates shuffle // source: https://stackoverflow.com/a/1520212/342852 static void shuffleArray(byte[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap byte a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } }
Standard input is empty
Starting round trip with size 100: Success (matching) Starting round trip with size 1000: Success (matching) Starting round trip with size 10000: Success (matching) Starting round trip with size 20000: Success (matching) Starting round trip with size 30000: Success (matching) Starting round trip with size 32000: Success (matching) Starting round trip with size 32700: Success (matching) Starting round trip with size 32701: Success (matching) Starting round trip with size 32702: Success (matching) Starting round trip with size 32703: Success (matching) Starting round trip with size 32704: Success (matching) Starting round trip with size 32705: Success (matching) Starting round trip with size 32706: Success (matching) Starting round trip with size 32707: Success (matching) Starting round trip with size 32708: Success (matching) Starting round trip with size 32709: Success (matching) Starting round trip with size 32710: Success (matching) Starting round trip with size 32711: Success (matching) Starting round trip with size 32712: Success (matching) Starting round trip with size 32713: Success (matching) Starting round trip with size 32714: Success (matching) Starting round trip with size 32715: Success (matching) Starting round trip with size 32716: Success (matching) Starting round trip with size 32717: Success (matching) Starting round trip with size 32718: Success (matching) Starting round trip with size 32719: Success (matching) Starting round trip with size 32720: Success (matching) Starting round trip with size 32721: Success (matching) Starting round trip with size 32722: Success (matching) Starting round trip with size 32723: Success (matching) Starting round trip with size 32724: Success (matching) Starting round trip with size 32725: Success (matching) Starting round trip with size 32726: Success (matching) Starting round trip with size 32727: Success (matching) Starting round trip with size 32728: Success (matching) Starting round trip with size 32729: Success (matching) Starting round trip with size 32730: Success (matching) Starting round trip with size 32731: Success (matching) Starting round trip with size 32732: Success (matching) Starting round trip with size 32733: Success (matching) Starting round trip with size 32734: Success (matching) Starting round trip with size 32735: Success (matching) Starting round trip with size 32736: Success (matching) Starting round trip with size 32737: Success (matching) Starting round trip with size 32738: Success (matching) Starting round trip with size 32739: Success (matching) Starting round trip with size 32740: Success (matching) Starting round trip with size 32741: Success (matching) Starting round trip with size 32742: Success (matching) Starting round trip with size 32743: Success (matching) Starting round trip with size 32744: Success (matching) Starting round trip with size 32745: Success (matching) Starting round trip with size 32746: Success (matching) Starting round trip with size 32747: Success (matching) Starting round trip with size 32748: Success (matching) Starting round trip with size 32749: Success (matching) Starting round trip with size 32750: Success (matching) Starting round trip with size 32751: Success (matching) Starting round trip with size 32752: Failure!