test/jdk/java/util/zip/FlaterTest.java
author coffeys
Wed, 09 Oct 2019 10:14:03 +0000
changeset 58513 7605e97c9491
parent 49834 99644c75eaed
permissions -rw-r--r--
8231770: Test java/util/zip/FlaterTest.java fails with -Xcheck:jni Reviewed-by: alanb, coffeys, chegar Contributed-by: kiran.sidhartha.ravikumar@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58513
7605e97c9491 8231770: Test java/util/zip/FlaterTest.java fails with -Xcheck:jni
coffeys
parents: 49834
diff changeset
     2
 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
58513
7605e97c9491 8231770: Test java/util/zip/FlaterTest.java fails with -Xcheck:jni
coffeys
parents: 49834
diff changeset
    26
 * @bug 6348045 6341887 8231770
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary GZipOutputStream/InputStream goes critical(calls JNI_Get*Critical)
58513
7605e97c9491 8231770: Test java/util/zip/FlaterTest.java fails with -Xcheck:jni
coffeys
parents: 49834
diff changeset
    28
 * and causes slowness. This test uses Deflater and Inflater directly.
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 5506
diff changeset
    29
 * @key randomness
58513
7605e97c9491 8231770: Test java/util/zip/FlaterTest.java fails with -Xcheck:jni
coffeys
parents: 49834
diff changeset
    30
 * @run main/othervm -Xcheck:jni FlaterTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This test runs Inflater and Defalter in a number of simultaneous threads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * validating that the deflated & then inflated data matches the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class FlaterTest extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final int DATA_LEN = 1024 * 128;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    44
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    45
    private static ByteBuffer dataDirect;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    46
    private static ByteBuffer dataHeap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // If true, print extra info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // Set of Flater threads running.
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    52
    private static Set<Flater> flaters =
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    53
        Collections.synchronizedSet(new HashSet<>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** Fill in {@code data} with random values. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static void createData() {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    57
        ByteBuffer bb = ByteBuffer.allocateDirect(DATA_LEN * 8);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    58
        for (int i = 0; i < DATA_LEN * 8; i += 8) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    59
            bb.putDouble(i, Math.random());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    61
        dataDirect = bb;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    62
        final ByteBuffer hb = ByteBuffer.allocate(bb.capacity());
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    63
        hb.duplicate().put(bb.duplicate());
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    64
        dataHeap = hb;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    65
        if (debug) System.out.println("data length is " + bb.capacity());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** @return the length of the deflated {@code data}. */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    69
    private static int getDeflatedLength() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Deflater deflater = new Deflater();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    71
        deflater.setInput(dataDirect.duplicate());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        deflater.finish();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    73
        byte[] out = new byte[dataDirect.capacity()];
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    74
        int rc = deflater.deflate(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        deflater.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (debug) System.out.println("deflatedLength is " + rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /** Compares given bytes with those in {@code data}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @throws Exception if given bytes don't match {@code data}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    83
    private static void validate(ByteBuffer buf, int offset, int len) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        for (int i = 0; i < len; i++ ) {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    85
            if (buf.get(i) != dataDirect.get(offset+i)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                throw new Exception("mismatch at " + (offset + i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    91
    public static void realMain(String[] args) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    92
        int numThreads = args.length > 0 ? Integer.parseInt(args[0]) : 5;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        createData();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    94
        for (int srcMode = 0; srcMode <= 2; srcMode ++) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    95
            for (int dstMode = 0; dstMode <= 2; dstMode ++) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    96
                new FlaterTest().go(numThreads, srcMode, dstMode);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    97
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
    98
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   101
    private synchronized void go(int numThreads, int srcMode, int dstMode) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int deflatedLength = getDeflatedLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i = 0; i < numThreads; i++) {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   106
            Flater f = new Flater(deflatedLength, srcMode, dstMode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            flaters.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            f.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   110
        synchronized (flaters) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   111
            while (flaters.size() != 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   112
                try {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   113
                    flaters.wait();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   114
                } catch (InterruptedException ex) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   115
                    unexpected(ex);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   116
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        time = System.currentTimeMillis() - time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println("Time needed for " + numThreads
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   121
                           + " threads to deflate/inflate: " + time + " ms (srcMode="+srcMode+",dstMode="+dstMode+")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /** Deflates and inflates data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static class Flater extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        private final int deflatedLength;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   127
        private final int srcMode, dstMode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   129
        private Flater(int length, int srcMode, int dstMode) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            this.deflatedLength = length;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   131
            this.srcMode = srcMode;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   132
            this.dstMode = dstMode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        /** Deflates and inflates {@code data}. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (debug) System.out.println(getName() + " starting run()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            try {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   139
                ByteBuffer deflated = DeflateData(deflatedLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                InflateData(deflated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                fail(getName() + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } finally {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   145
                synchronized (flaters) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   146
                    flaters.remove(this);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   147
                    if (flaters.isEmpty()) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   148
                        flaters.notifyAll();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   149
                    }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   150
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        /** Returns a copy of {@code data} in deflated form. */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   155
        private ByteBuffer DeflateData(int length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            Deflater deflater = new Deflater();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   157
            if (srcMode == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   158
                deflater.setInput(dataHeap.array());
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   159
            } else if (srcMode == 1) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   160
                deflater.setInput(dataHeap.duplicate());
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   161
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   162
                assert srcMode == 2;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   163
                deflater.setInput(dataDirect.duplicate());
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   164
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            deflater.finish();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   166
            ByteBuffer out = dstMode == 2 ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   167
            int deflated;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   168
            if (dstMode == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   169
                deflated = deflater.deflate(out.array(), 0, length);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   170
                out.position(deflated);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   171
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   172
                deflater.deflate(out);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   173
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   174
            out.flip();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        /** Inflate a byte array, comparing it with {@code data} during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * inflation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * @throws Exception if inflated bytes don't match {@code data}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         */
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   182
        private void InflateData(ByteBuffer bytes) throws Throwable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            Inflater inflater = new Inflater();
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   184
            if (dstMode == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   185
                inflater.setInput(bytes.array(), 0, bytes.remaining());
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   186
            } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   187
                inflater.setInput(bytes);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   188
            }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   189
            if (inflater.getRemaining() == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   190
                throw new Exception("Nothing to inflate (bytes=" + bytes + ")");
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   191
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            int len = 1024 * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            int offset = 0;
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   194
            ByteBuffer buf = srcMode == 2 ? ByteBuffer.allocateDirect(len) : ByteBuffer.allocate(len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            while (inflater.getRemaining() > 0) {
49834
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   196
                buf.clear();
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   197
                int inflated;
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   198
                if (srcMode == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   199
                    inflated = inflater.inflate(buf.array(), 0, buf.remaining());
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   200
                } else {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   201
                    inflated = inflater.inflate(buf);
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   202
                }
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   203
                if (inflated == 0) {
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   204
                    throw new Exception("Nothing inflated (dst=" + buf + ",offset=" + offset + ",rem=" + inflater.getRemaining() + ",srcMode="+srcMode+",dstMode="+dstMode+")");
99644c75eaed 6341887: java.util.zip: Add ByteBuffer methods to Inflater/Deflater
sherman
parents: 47216
diff changeset
   205
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                validate(buf, offset, inflated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                offset += inflated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        System.out.println("\nPassed = " + passed + " failed = " + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}