1 /*
   2  * Copyright (c) 2016, 2017, 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  * @library /test/lib
  26  * @run main TestAvailable
  27  * @bug 7031075 8161426
  28  * @summary Make sure that available() method behaves as expected.
  29  * @key randomness
  30  */
  31 
  32 import java.io.*;
  33 import java.util.Random;
  34 import java.util.zip.*;
  35 import jdk.test.lib.RandomFactory;
  36 
  37 public class TestAvailable {
  38 
  39     public static void main(String args[]) throws Throwable {
  40         Random r = RandomFactory.getRandom();
  41         for (int n = 0; n < 10; n++) {
  42             byte[] src = new byte[r.nextInt(100) + 1];
  43             r.nextBytes(src);
  44             // test InflaterInputStream
  45             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  46             try (DeflaterOutputStream dos = new DeflaterOutputStream(baos)) {
  47                 dos.write(src);
  48             }
  49             try (InflaterInputStream iis = new InflaterInputStream(
  50                    new ByteArrayInputStream(baos.toByteArray()))) {
  51                 test(iis, src);
  52             }
  53 
  54             // test GZIPInputStream
  55             baos = new ByteArrayOutputStream();
  56             try (GZIPOutputStream dos = new GZIPOutputStream(baos)) {
  57                 dos.write(src);
  58             }
  59             try (GZIPInputStream gis = new GZIPInputStream(
  60                    new ByteArrayInputStream(baos.toByteArray()))) {
  61                 test(gis, src);
  62             }
  63         }
  64     }
  65 
  66     private static void test(InputStream is, byte[] expected) throws IOException {
  67         int cnt = 0;
  68         do {
  69             int available = is.available();
  70             if (available > 0) {
  71                 int b = is.read();
  72                 if (b == -1) {
  73                     throw new RuntimeException("available() > 0, read() == -1 : failed!");
  74                 }
  75                 if (expected[cnt++] != (byte)b) {
  76                     throw new RuntimeException("read() : failed!");
  77                 }
  78             } else if (available == 0) {
  79                 if (is.read() != -1) {
  80                     throw new RuntimeException("available() == 0, read() != -1 : failed!");
  81                 }
  82                 break;
  83             } else {
  84                 throw new RuntimeException("available() < 0 : failed!");
  85             }
  86         } while (true);
  87         if (cnt != expected.length) {
  88             throw new RuntimeException("read : failed!");
  89         }
  90     }
  91 
  92 }