Path: blob/master/test/jdk/java/io/OutputStream/WriteParams.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 1267039 1267043 4193729 435877426* @summary Check for correct handling of parameters to27* XXXXOutputStream.write(b, off, len).28*29*/3031import java.io.*;32import java.util.zip.DeflaterOutputStream;3334public class WriteParams {3536/* check for correct handling of different values of off and len */37public static void doTest(OutputStream out) throws Exception {3839int off[] = {-1, -1, 0, 0, 33, 33, 0, 32, 32, 4, 1, 0, -1,40Integer.MAX_VALUE, 1, Integer.MIN_VALUE,41Integer.MIN_VALUE, 1};42int len[] = {-1, 0, -1, 33, 0, 4, 32, 0, 4, 16, 31, 0,43Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE,441, -1, Integer.MIN_VALUE};45boolean results[] = { false, false, false, false, false, false,46true, true, false, true, true, true, false,47false, false, false, false, false};48int numCases = off.length;49byte b[] = new byte[32];50int numBad = 0;5152for(int i = 0; i < numCases; i++) {53try {54out.write(b , off[i] , len[i]);55} catch (IndexOutOfBoundsException aiobe) {56if (results[i]) {57System.err.println("Error:IndexOutOfBoundsException thrown"+58" for write(b, " + off[i] + " " + len[i] +59" ) on " + out + "\nb.length = 32");60numBad++;61} else {62/* System.err.println("PassE: " + off[i] + " " + len[i]); */63}64continue;65} catch (OutOfMemoryError ome) {66System.err.println("Error: OutOfMemoryError in write(b, " +67off[i] + " " + len[i] + " ) on " + out +68"\nb.length = 32");69numBad++;70continue;71}72if (!results[i]) {73System.err.println("Error:No IndexOutOfBoundsException thrown"+74" for write(b, " + off[i] + " " + len[i] +75" ) on " + out + "\nb.length = 32");76numBad++;77} else {78/* System.err.println("Pass: " + off[i] + " " + len[i]); */79}80}8182if (numBad > 0) {83throw new RuntimeException(out + " Failed " + numBad + " cases");84} else {85System.err.println("Successfully completed bounds tests on " + out);86}87}8889/* check for correct handling of null b */90public static void doTest1(OutputStream out) throws Exception {91byte b[] = null;92try {93out.write(b, 0, 32);94} catch (NullPointerException npe) {95System.err.println("SuccessFully completed null b test on " + out);96return;97}98throw new RuntimeException(out + " Failed null b test");99}100101public static void main(String args[]) throws Exception{102/* initialise stuff here */103File fn = new File("x.WriteBounds");104FileOutputStream fout = new FileOutputStream(fn);105for (int i = 0; i < 32; i++) {106fout.write(i);107}108fout.close();109110byte b[] = new byte[64];111for(int i = 0; i < 64; i++) {112b[i] = 1;113}114115/* test for different output streams */116FileOutputStream fos = new FileOutputStream(fn);117doTest(fos);118doTest1(fos);119fos.close();120121ObjectOutputStream oos = new ObjectOutputStream(new MyOutputStream());122doTest(oos);123doTest1(oos);124oos.close();125126BufferedOutputStream bos =127new BufferedOutputStream(new MyOutputStream());128doTest(bos);129doTest1(bos);130bos.close();131132ByteArrayOutputStream baos = new ByteArrayOutputStream();133doTest(baos);134doTest1(baos);135baos.close();136137DataOutputStream dos = new DataOutputStream(new MyOutputStream());138doTest(dos);139doTest1(dos);140dos.close();141142PipedInputStream pis = new PipedInputStream();143PipedOutputStream pos = new PipedOutputStream();144pos.connect(pis);145doTest(pos);146doTest1(pos);147pos.close();148149DeflaterOutputStream dfos = new DeflaterOutputStream(new MyOutputStream());150doTest(dfos);151doTest1(dfos);152dfos.close();153154OutputStream nos = OutputStream.nullOutputStream();155doTest(nos);156doTest1(nos);157nos.close();158159/* cleanup */160fn.delete();161162}163}164165/* An OutputStream class used in the above tests */166class MyOutputStream extends OutputStream {167168public MyOutputStream() {169}170171public void write(int b) throws IOException {172}173}174175176