1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 7110149
  27  * @summary Test basic deflater & inflater functionality
  28  */
  29 
  30 import java.io.*;
  31 import java.util.*;
  32 import java.util.zip.*;
  33 
  34 public class DeInflate {
  35 
  36     static void check(Deflater compresser, byte[] in, int len,
  37                       byte[] out1, byte[] out2, boolean nowrap)
  38         throws Throwable
  39     {
  40         Arrays.fill(out1, (byte)0);
  41         Arrays.fill(out2, (byte)0);
  42 
  43         compresser.setInput(in, 0, len);
  44         compresser.finish();
  45         int m = compresser.deflate(out1);
  46 
  47         Inflater decompresser = new Inflater(nowrap);
  48         decompresser.setInput(out1, 0, m);
  49         int n = decompresser.inflate(out2);
  50 
  51         if (n != len ||
  52             !Arrays.equals(Arrays.copyOf(in, len), Arrays.copyOf(out2, len)) ||
  53             decompresser.inflate(out2) != 0) {
  54             System.out.printf("m=%d, n=%d, len=%d, eq=%b%n",
  55                               m, n, len, Arrays.equals(in, out2));
  56             throw new RuntimeException("De/inflater failed:" + compresser);
  57         }
  58     }
  59 
  60     public static void main(String[] args) throws Throwable {
  61         byte[] dataIn = new byte[1024 * 512];
  62         new Random().nextBytes(dataIn);
  63         byte[] dataOut1 = new byte[dataIn.length + 1024];
  64         byte[] dataOut2 = new byte[dataIn.length];
  65         boolean wrap[] = new boolean[] { false, true };
  66 
  67         for (int level = Deflater.DEFAULT_COMPRESSION;
  68                  level <= Deflater.BEST_COMPRESSION; level++) {
  69             System.out.print("level=" + level + ", strategy= ");
  70             for (int strategy = Deflater.DEFAULT_STRATEGY;
  71                      strategy <= Deflater.HUFFMAN_ONLY; strategy++) {
  72                 System.out.print(" " + strategy + " nowrap[");
  73                 for (int dowrap = 0; dowrap <= 1; dowrap++) {
  74                     System.out.print(" " + wrap[dowrap]);
  75                     for (int i = 0; i < 5; i++) {
  76                         Deflater def = new Deflater(level, wrap[dowrap]);
  77                         if (strategy != Deflater.DEFAULT_STRATEGY) {
  78                             def.setStrategy(strategy);
  79                             // The first invocation after setLevel/Strategy()
  80                             // with a different level/stragety returns 0, if
  81                             // there is no need to flush out anything for the
  82                             // previous setting/"data", this is tricky and
  83                             // appears un-documented.
  84                             def.deflate(dataOut2);
  85                         }
  86                         int len = (i == 0)? dataIn.length
  87                                           : new Random().nextInt(dataIn.length);
  88                             check(def, dataIn, len, dataOut1, dataOut2, wrap[dowrap]);
  89                     }
  90                 }
  91                 System.out.print("] ");
  92             }
  93             System.out.println();
  94         }
  95     }
  96 }