5352N/A/*
5352N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5352N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5352N/A *
5352N/A * This code is free software; you can redistribute it and/or modify it
5352N/A * under the terms of the GNU General Public License version 2 only, as
5352N/A * published by the Free Software Foundation. Oracle designates this
5352N/A * particular file as subject to the "Classpath" exception as provided
5352N/A * by Oracle in the LICENSE file that accompanied this code.
5352N/A *
5352N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5352N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5352N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5352N/A * version 2 for more details (a copy is included in the LICENSE file that
5352N/A * accompanied this code).
5352N/A *
5352N/A * You should have received a copy of the GNU General Public License version
5352N/A * 2 along with this work; if not, write to the Free Software Foundation,
5352N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5352N/A *
5352N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5352N/A * or visit www.oracle.com if you need additional information or have any
5352N/A * questions.
5352N/A */
5352N/A
5352N/A/* @test
5352N/A * @bug 7188852
5352N/A * @summary Test De/Inflater.getBytesRead/Written()
5352N/A */
5352N/A
5352N/Aimport java.io.*;
5352N/Aimport java.util.*;
5352N/Aimport java.util.zip.*;
5352N/A
5352N/A
5352N/Apublic class TotalInOut {
5352N/A static final int BUF_SIZE= 1 * 1024 * 1024;
5352N/A
5352N/A static void realMain (String[] args) throws Throwable {
5352N/A long dataSize = 128L * 1024L * 1024L; // 128MB
5352N/A if (args.length > 0 && "large".equals(args[0]))
5352N/A dataSize = 5L * 1024L * 1024L * 1024L; // 5GB
5352N/A
5352N/A Deflater deflater = new Deflater();
5352N/A Inflater inflater = new Inflater();
5352N/A
5352N/A byte[] dataIn = new byte[BUF_SIZE];
5352N/A byte[] dataOut = new byte[BUF_SIZE];
5352N/A byte[] tmp = new byte[BUF_SIZE];
5352N/A
5352N/A Random r = new Random();
5352N/A r.nextBytes(dataIn);
5352N/A long bytesReadDef = 0;
5352N/A long bytesWrittenDef = 0;
5352N/A long bytesReadInf = 0;
5352N/A long bytesWrittenInf = 0;
5352N/A
5352N/A deflater.setInput(dataIn, 0, dataIn.length);
5352N/A while (bytesReadDef < dataSize || bytesWrittenInf < dataSize) {
5352N/A int len = r.nextInt(BUF_SIZE/2) + BUF_SIZE / 2;
5352N/A if (deflater.needsInput()) {
5352N/A bytesReadDef += dataIn.length;
5352N/A check(bytesReadDef == deflater.getBytesRead());
5352N/A deflater.setInput(dataIn, 0, dataIn.length);
5352N/A }
5352N/A int n = deflater.deflate(tmp, 0, len);
5352N/A bytesWrittenDef += n;
5352N/A check(bytesWrittenDef == deflater.getBytesWritten());
5352N/A
5352N/A inflater.setInput(tmp, 0, n);
5352N/A bytesReadInf += n;
5352N/A while (!inflater.needsInput()) {
5352N/A bytesWrittenInf += inflater.inflate(dataOut, 0, dataOut.length);
5352N/A check(bytesWrittenInf == inflater.getBytesWritten());
5352N/A }
5352N/A check(bytesReadInf == inflater.getBytesRead());
5352N/A }
5352N/A }
5352N/A
5352N/A //--------------------- Infrastructure ---------------------------
5352N/A static volatile int passed = 0, failed = 0;
5352N/A static void pass() {passed++;}
5352N/A static void pass(String msg) {System.out.println(msg); passed++;}
5352N/A static void fail() {failed++; Thread.dumpStack();}
5352N/A static void fail(String msg) {System.out.println(msg); fail();}
5352N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
5352N/A static void unexpected(Throwable t, String msg) {
5352N/A System.out.println(msg); failed++; t.printStackTrace();}
5352N/A static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
5352N/A static void equal(Object x, Object y) {
5352N/A if (x == null ? y == null : x.equals(y)) pass();
5352N/A else fail(x + " not equal to " + y);}
5352N/A public static void main(String[] args) throws Throwable {
5352N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
5352N/A System.out.println("\nPassed = " + passed + " failed = " + failed);
5352N/A if (failed > 0) throw new AssertionError("Some tests failed");}
5352N/A}