1 /*
   2  * Copyright (c) 2012, 2013, 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 /* @test
  25  * @bug 7188852
  26  * @summary Test De/Inflater.getBytesRead/Written()
  27  */
  28 
  29 import java.io.*;
  30 import java.util.*;
  31 import java.util.zip.*;
  32 
  33 
  34 public class TotalInOut {
  35      static final int BUF_SIZE= 1 * 1024 * 1024;
  36 
  37      static void realMain (String[] args) throws Throwable {
  38          long dataSize = 128L * 1024L * 1024L;      // 128MB
  39          if (args.length > 0 && "large".equals(args[0]))
  40              dataSize = 5L * 1024L * 1024L * 1024L; //  5GB
  41 
  42          Deflater deflater = new Deflater();
  43          Inflater inflater = new Inflater();
  44 
  45          byte[] dataIn = new byte[BUF_SIZE];
  46          byte[] dataOut = new byte[BUF_SIZE];
  47          byte[] tmp = new byte[BUF_SIZE];
  48 
  49          Random r = new Random();
  50          r.nextBytes(dataIn);
  51          long bytesReadDef    = 0;
  52          long bytesWrittenDef = 0;
  53          long bytesReadInf    = 0;
  54          long bytesWrittenInf = 0;
  55 
  56          deflater.setInput(dataIn, 0, dataIn.length);
  57          while (bytesReadDef < dataSize || bytesWrittenInf < dataSize) {
  58              int len = r.nextInt(BUF_SIZE/2) + BUF_SIZE / 2;
  59              if (deflater.needsInput()) {
  60                  bytesReadDef += dataIn.length;
  61                  check(bytesReadDef == deflater.getBytesRead());
  62                  deflater.setInput(dataIn, 0, dataIn.length);
  63              }
  64              int n = deflater.deflate(tmp, 0, len);
  65              bytesWrittenDef += n;
  66              check(bytesWrittenDef == deflater.getBytesWritten());
  67 
  68              inflater.setInput(tmp, 0, n);
  69              bytesReadInf += n;
  70              while (!inflater.needsInput()) {
  71                  bytesWrittenInf += inflater.inflate(dataOut, 0, dataOut.length);
  72                  check(bytesWrittenInf == inflater.getBytesWritten());
  73              }
  74              check(bytesReadInf == inflater.getBytesRead());
  75          }
  76      }
  77 
  78      //--------------------- Infrastructure ---------------------------
  79      static volatile int passed = 0, failed = 0;
  80      static void pass() {passed++;}
  81      static void pass(String msg) {System.out.println(msg); passed++;}
  82      static void fail() {failed++; Thread.dumpStack();}
  83      static void fail(String msg) {System.out.println(msg); fail();}
  84      static void unexpected(Throwable t) {failed++; t.printStackTrace();}
  85      static void unexpected(Throwable t, String msg) {
  86          System.out.println(msg); failed++; t.printStackTrace();}
  87      static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
  88      static void equal(Object x, Object y) {
  89           if (x == null ? y == null : x.equals(y)) pass();
  90           else fail(x + " not equal to " + y);}
  91      public static void main(String[] args) throws Throwable {
  92           try {realMain(args);} catch (Throwable t) {unexpected(t);}
  93           System.out.println("\nPassed = " + passed + " failed = " + failed);
  94           if (failed > 0) throw new AssertionError("Some tests failed");}
  95 }