fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. import java.util.Random;
  7. import java.util.concurrent.CompletableFuture;
  8. import java.util.concurrent.ExecutionException;
  9. import java.util.concurrent.ThreadLocalRandom;
  10. import java.util.concurrent.TimeUnit;
  11. import java.util.concurrent.TimeoutException;
  12. import java.util.zip.DataFormatException;
  13. import java.util.zip.Deflater;
  14. import java.util.zip.Inflater;
  15.  
  16. class ZlibMain {
  17.  
  18. private static byte[] compress(final byte[] data) {
  19. final Deflater deflater = new Deflater();
  20. deflater.setInput(data);
  21.  
  22. deflater.finish();
  23. final byte[] bytesCompressed = new byte[Short.MAX_VALUE];
  24. final int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed);
  25. final byte[] returnValues = new byte[numberOfBytesAfterCompression];
  26. System.arraycopy(bytesCompressed, 0, returnValues, 0, numberOfBytesAfterCompression);
  27. return returnValues;
  28.  
  29. }
  30.  
  31. private static byte[] decompress(final byte[] data) {
  32. final Inflater inflater = new Inflater();
  33. inflater.setInput(data);
  34. try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length)) {
  35. final byte[] buffer = new byte[Math.max(1024, data.length / 10)];
  36. while (!inflater.finished()) {
  37. final int count = inflater.inflate(buffer);
  38. outputStream.write(buffer, 0, count);
  39. }
  40. outputStream.close();
  41. final byte[] output = outputStream.toByteArray();
  42. return output;
  43. throw new RuntimeException(e);
  44. }
  45. }
  46.  
  47. public static void main(final String[] args) {
  48. roundTrip(100);
  49. roundTrip(1000);
  50. roundTrip(10000);
  51. roundTrip(20000);
  52. roundTrip(30000);
  53. roundTrip(32000);
  54. for (int i = 32700; i < 33000; i++) {
  55. if(!roundTrip(i))break;
  56. }
  57. }
  58.  
  59. private static boolean roundTrip(final int i) {
  60. System.out.printf("Starting round trip with size %d: ", i);
  61. final byte[] data = new byte[i];
  62. for (int j = 0; j < data.length; j++) {
  63. data[j]= (byte) j;
  64. }
  65. shuffleArray(data);
  66.  
  67. final byte[] compressed = compress(data);
  68. try {
  69. final byte[] decompressed = CompletableFuture.supplyAsync(() -> decompress(compressed))
  70. .get(2, TimeUnit.SECONDS);
  71. System.out.printf("Success (%s)%n", Arrays.equals(data, decompressed) ? "matching" : "non-matching");
  72. return true;
  73. } catch (InterruptedException | ExecutionException | TimeoutException e) {
  74. System.out.println("Failure!");
  75. return false;
  76. }
  77. }
  78.  
  79. // Implementing Fisher–Yates shuffle
  80. // source: https://stackoverflow.com/a/1520212/342852
  81. static void shuffleArray(byte[] ar)
  82. {
  83. // If running on Java 6 or older, use `new Random()` on RHS here
  84. Random rnd = ThreadLocalRandom.current();
  85. for (int i = ar.length - 1; i > 0; i--)
  86. {
  87. int index = rnd.nextInt(i + 1);
  88. // Simple swap
  89. byte a = ar[index];
  90. ar[index] = ar[i];
  91. ar[i] = a;
  92. }
  93. }
  94. }
Success #stdin #stdout 2.25s 4452352KB
stdin
Standard input is empty
stdout
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!