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