Path: blob/master/test/jdk/java/io/InputStream/ReadParams.java
41149 views
/*1* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4008296 4008293 4190090 4193729 435877426* @summary Check for correct handling of parameters to27* XXXXInputStream.read(b, off, len).28*29*/3031import java.io.*;32import java.util.zip.ZipInputStream;33import java.util.zip.InflaterInputStream;34import java.util.zip.DeflaterOutputStream;3536public class ReadParams {3738/* check for correct handling of different values of off and len */39public static void doTest(InputStream in) throws Exception {4041int off[] = {-1, -1, 0, 0, 33, 33, 0, 32, 32, 4, 1, 0, -1,42Integer.MAX_VALUE, 1, Integer.MIN_VALUE,43Integer.MIN_VALUE, 1};44int len[] = {-1, 0, -1, 33, 0, 4, 32, 0, 4, 16, 31, 0,45Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE,461, -1, Integer.MIN_VALUE};47boolean results[] = { false, false, false, false, false, false,48true, true, false, true, true, true, false,49false, false, false, false, false};50int numCases = off.length;51byte b[] = new byte[32];52int numBad = 0;5354for(int i = 0; i < numCases; i++) {55try {56in.read(b , off[i] , len[i]);57} catch (IndexOutOfBoundsException aiobe) {58if (results[i]) {59System.err.println("Error:IndexOutOfBoundsException thrown"+60" for read(b, " + off[i] + " " + len[i] +61" ) on " + in + "\nb.length = 32");62numBad++;63} else {64/* System.err.println("PassE: " + off[i] + " " + len[i]); */65}66continue;67} catch (OutOfMemoryError ome) {68System.err.println("Error: OutOfMemoryError in read(b, " +69off[i] + " " + len[i] + " ) on " + in +70"\nb.length = 32");71numBad++;72continue;73}74if (!results[i]) {75System.err.println("Error:No IndexOutOfBoundsException thrown"+76" for read(b, " + off[i] + " " + len[i] +77" ) on " + in + "\nb.length = 32");78numBad++;79} else {80/* System.err.println("Pass: " + off[i] + " " + len[i]); */81}82}8384if (numBad > 0) {85throw new RuntimeException(in + " Failed " + numBad + " cases");86} else {87System.err.println("Successfully completed bounds tests on " + in);88}89}9091/* check for correct handling of null b */92public static void doTest1(InputStream in) throws Exception {93byte b[] = null;94try {95in.read(b, 0, 32);96} catch (NullPointerException npe) {97System.err.println("SuccessFully completed null b test on " + in);98return;99}100throw new RuntimeException(in + " Failed null b test");101}102103public static void main(String args[]) throws Exception{104/* initialise stuff */105File fn = new File("x.ReadBounds");106FileOutputStream fout = new FileOutputStream(fn);107for (int i = 0; i < 32; i++) {108fout.write(i);109}110fout.close();111112byte b[] = new byte[64];113for(int i = 0; i < 64; i++) {114b[i] = 1;115}116117/* test all input streams */118FileInputStream fis = new FileInputStream(fn);119doTest(fis);120doTest1(fis);121fis.close();122123BufferedInputStream bis =124new BufferedInputStream(new MyInputStream(1024));125doTest(bis);126doTest1(bis);127bis.close();128129ByteArrayInputStream bais = new ByteArrayInputStream(b);130doTest(bais);131doTest1(bais);132bais.close();133134FileOutputStream fos = new FileOutputStream(fn);135ObjectOutputStream oos = new ObjectOutputStream(fos);136oos.writeInt(12345);137oos.writeObject("Today");138oos.writeObject(new Integer(32));139oos.close();140ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fn));141doTest(ois);142doTest1(ois);143ois.close();144145DataInputStream dis = new DataInputStream(new MyInputStream(1024));146doTest(dis);147doTest1(dis);148dis.close();149150LineNumberInputStream lis =151new LineNumberInputStream(new MyInputStream(1024));152doTest(lis);153doTest1(lis);154lis.close();155156PipedOutputStream pos = new PipedOutputStream();157PipedInputStream pis = new PipedInputStream();158pos.connect(pis);159pos.write(b, 0, 64);160doTest(pis);161doTest1(pis);162pis.close();163164PushbackInputStream pbis =165new PushbackInputStream(new MyInputStream(1024));166doTest(pbis);167doTest1(pbis);168pbis.close();169170StringBufferInputStream sbis =171new StringBufferInputStream(new String(b));172doTest(sbis);173doTest1(sbis);174sbis.close();175176SequenceInputStream sis =177new SequenceInputStream(new MyInputStream(1024),178new MyInputStream(1024));179doTest(sis);180doTest1(sis);181sis.close();182183ZipInputStream zis = new ZipInputStream(new FileInputStream(fn));184doTest(zis);185doTest1(zis);186zis.close();187188byte[] data = new byte[256];189ByteArrayOutputStream bos = new ByteArrayOutputStream();190DeflaterOutputStream dos = new DeflaterOutputStream(bos);191dos.write(data, 0, data.length);192dos.close();193InflaterInputStream ifs = new InflaterInputStream194(new ByteArrayInputStream(bos.toByteArray()));195doTest(ifs);196doTest1(ifs);197ifs.close();198199InputStream nis = InputStream.nullInputStream();200doTest(nis);201doTest1(nis);202nis.close();203204/* cleanup */205fn.delete();206}207}208209/* An InputStream class used in the above tests */210class MyInputStream extends InputStream {211212private int readctr = 0;213private long endoffile;214215public MyInputStream(long endoffile) {216this.endoffile = endoffile;217}218219public int read() {220if (readctr == endoffile) {221return -1;222}223else {224readctr++;225return 0;226}227}228229public int available() { return 0; }230}231232233