Path: blob/master/test/jdk/java/io/OutputStreamWriter/BoundsCheck.java
41149 views
/*1* Copyright (c) 1999, 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 422190126* @summary Ensure that negative offset or negative len parameter for27* write(String str, int off, int len) throws28* IndexOutOfBoundsException.29*/3031import java.io.*;3233public class BoundsCheck {34public static void main(String args[]) throws Exception {35ByteArrayOutputStream bos = new ByteArrayOutputStream();36OutputStreamWriter osw = new OutputStreamWriter(bos);37String data = "Data to be written";38char cdata[] = {'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'};3940boolean caughtException = false;41try {42osw.write(data, -3, 5);43throw new RuntimeException("Test failed for negative offset");44} catch (IndexOutOfBoundsException e){ }4546try {47osw.write(data, 3, -5);48throw new RuntimeException("Test failed for negative length");49} catch (IndexOutOfBoundsException e){ }5051try {52osw.write(data, 3, 75);53throw new RuntimeException("Test failed for len+off > str.length");54} catch (IndexOutOfBoundsException e){ }5556try {57osw.write(cdata, -3, 5);58throw new RuntimeException("Test failed for negative offset");59} catch (IndexOutOfBoundsException e){ }6061try {62osw.write(cdata, 3, -5);63throw new RuntimeException("Test failed for negative length");64} catch (IndexOutOfBoundsException e){ }6566try {67osw.write(cdata, 3, 75);68throw new RuntimeException("Test failed for len+off > str.length");69} catch (IndexOutOfBoundsException e){ }70}71}727374