< prev index next >

test/jdk/java/io/InputStream/ReadAllBytes.java

Print this page




  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 import java.io.ByteArrayInputStream;
  25 import java.io.FilterInputStream;
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.util.Arrays;
  29 import java.util.Random;
  30 import jdk.test.lib.RandomFactory;
  31 
  32 /*
  33  * @test
  34  * @bug 8080835
  35  * @library /test/lib
  36  * @build jdk.test.lib.RandomFactory
  37  * @run main ReadAllBytes
  38  * @summary Basic test for InputStream.readAllBytes
  39  * @key randomness
  40  */
  41 
  42 public class ReadAllBytes {
  43 
  44     private static Random generator = RandomFactory.getRandom();
  45 
  46     public static void main(String[] args) throws IOException {
  47         test(new byte[]{});
  48         test(new byte[]{1, 2, 3});
  49         test(createRandomBytes(1024));
  50         test(createRandomBytes((1 << 13) - 1));
  51         test(createRandomBytes((1 << 13)));
  52         test(createRandomBytes((1 << 13) + 1));
  53         test(createRandomBytes((1 << 15) - 1));
  54         test(createRandomBytes((1 << 15)));
  55         test(createRandomBytes((1 << 15) + 1));
  56         test(createRandomBytes((1 << 17) - 1));
  57         test(createRandomBytes((1 << 17)));
  58         test(createRandomBytes((1 << 17) + 1));
  59     }
  60 
  61     static void test(byte[] expectedBytes) throws IOException {
  62         int expectedLength = expectedBytes.length;
  63         WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes));
  64         byte[] readBytes = in.readAllBytes();
  65 
  66         int x;
  67         byte[] tmp = new byte[10];
  68         check((x = in.read()) == -1,
  69               "Expected end of stream from read(), got " + x);
  70         check((x = in.read(tmp)) == -1,
  71               "Expected end of stream from read(byte[]), got " + x);
  72         check((x = in.read(tmp, 0, tmp.length)) == -1,
  73               "Expected end of stream from read(byte[], int, int), got " + x);
  74         check(in.readAllBytes().length == 0,
  75               "Expected readAllBytes to return empty byte array");
  76         check(expectedLength == readBytes.length,
  77               "Expected length " + expectedLength + ", got " + readBytes.length);
  78         check(Arrays.equals(expectedBytes, readBytes),




  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 import java.io.ByteArrayInputStream;
  25 import java.io.FilterInputStream;
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.util.Arrays;
  29 import java.util.Random;
  30 import jdk.test.lib.RandomFactory;
  31 
  32 /*
  33  * @test
  34  * @bug 8080835 8193832
  35  * @library /test/lib
  36  * @build jdk.test.lib.RandomFactory
  37  * @run main ReadAllBytes
  38  * @summary Basic test for InputStream.readAllBytes
  39  * @key randomness
  40  */
  41 
  42 public class ReadAllBytes {
  43 
  44     private static Random generator = RandomFactory.getRandom();
  45 
  46     public static void main(String[] args) throws IOException {
  47         test(new byte[]{});
  48         test(new byte[]{1, 2, 3});
  49         test(createRandomBytes(1024));
  50         for (int shift : new int[] {13, 14, 15, 17}) {
  51             for (int offset : new int[] {-1, 0, 1}) {
  52                 test(createRandomBytes((1 << shift) + offset));
  53             }
  54         }




  55     }
  56 
  57     static void test(byte[] expectedBytes) throws IOException {
  58         int expectedLength = expectedBytes.length;
  59         WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes));
  60         byte[] readBytes = in.readAllBytes();
  61 
  62         int x;
  63         byte[] tmp = new byte[10];
  64         check((x = in.read()) == -1,
  65               "Expected end of stream from read(), got " + x);
  66         check((x = in.read(tmp)) == -1,
  67               "Expected end of stream from read(byte[]), got " + x);
  68         check((x = in.read(tmp, 0, tmp.length)) == -1,
  69               "Expected end of stream from read(byte[], int, int), got " + x);
  70         check(in.readAllBytes().length == 0,
  71               "Expected readAllBytes to return empty byte array");
  72         check(expectedLength == readBytes.length,
  73               "Expected length " + expectedLength + ", got " + readBytes.length);
  74         check(Arrays.equals(expectedBytes, readBytes),


< prev index next >