1 /*
   2  * Copyright (c) 2012, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /* @test
  27  * @bug 7188852
  28  * @summary Test De/Inflater.getBytesRead/Written()
  29  */
  30 
  31 import java.io.*;
  32 import java.util.*;
  33 import java.util.zip.*;
  34 
  35 
  36 public class TotalInOut {
  37      static final int BUF_SIZE= 1 * 1024 * 1024;
  38 
  39      static void realMain (String[] args) throws Throwable {
  40          long dataSize = 128L * 1024L * 1024L;      // 128MB
  41          if (args.length > 0 && "large".equals(args[0]))
  42              dataSize = 5L * 1024L * 1024L * 1024L; //  5GB
  43 
  44          Deflater deflater = new Deflater();
  45          Inflater inflater = new Inflater();
  46 
  47          byte[] dataIn = new byte[BUF_SIZE];
  48          byte[] dataOut = new byte[BUF_SIZE];
  49          byte[] tmp = new byte[BUF_SIZE];
  50 
  51          Random r = new Random();
  52          r.nextBytes(dataIn);
  53          long bytesReadDef    = 0;
  54          long bytesWrittenDef = 0;
  55          long bytesReadInf    = 0;
  56          long bytesWrittenInf = 0;
  57 
  58          deflater.setInput(dataIn, 0, dataIn.length);
  59          while (bytesReadDef < dataSize || bytesWrittenInf < dataSize) {
  60              int len = r.nextInt(BUF_SIZE/2) + BUF_SIZE / 2;
  61              if (deflater.needsInput()) {
  62                  bytesReadDef += dataIn.length;
  63                  check(bytesReadDef == deflater.getBytesRead());
  64                  deflater.setInput(dataIn, 0, dataIn.length);
  65              }
  66              int n = deflater.deflate(tmp, 0, len);
  67              bytesWrittenDef += n;
  68              check(bytesWrittenDef == deflater.getBytesWritten());
  69 
  70              inflater.setInput(tmp, 0, n);
  71              bytesReadInf += n;
  72              while (!inflater.needsInput()) {
  73                  bytesWrittenInf += inflater.inflate(dataOut, 0, dataOut.length);
  74                  check(bytesWrittenInf == inflater.getBytesWritten());
  75              }
  76              check(bytesReadInf == inflater.getBytesRead());
  77          }
  78      }
  79 
  80      //--------------------- Infrastructure ---------------------------
  81      static volatile int passed = 0, failed = 0;
  82      static void pass() {passed++;}
  83      static void pass(String msg) {System.out.println(msg); passed++;}
  84      static void fail() {failed++; Thread.dumpStack();}
  85      static void fail(String msg) {System.out.println(msg); fail();}
  86      static void unexpected(Throwable t) {failed++; t.printStackTrace();}
  87      static void unexpected(Throwable t, String msg) {
  88          System.out.println(msg); failed++; t.printStackTrace();}
  89      static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
  90      static void equal(Object x, Object y) {
  91           if (x == null ? y == null : x.equals(y)) pass();
  92           else fail(x + " not equal to " + y);}
  93      public static void main(String[] args) throws Throwable {
  94           try {realMain(args);} catch (Throwable t) {unexpected(t);}
  95           System.out.println("\nPassed = " + passed + " failed = " + failed);
  96           if (failed > 0) throw new AssertionError("Some tests failed");}
  97 }