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