Path: blob/master/test/jdk/java/io/DataInputStream/ReadFully.java
41152 views
/*1* Copyright (c) 1999, 2020, 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/* @test24* @bug 4214513 824503625* @summary Passing a negative offset or length,26* or passing a combination of offset and length too big27* for readFully must throw IndexOutOfBoundsException.28*/2930import java.io.*;3132public class ReadFully {3334private static final void testNegativeOffset() throws Exception {35File file = new File(System.getProperty("test.src"),36"ReadFully.java");37try (FileInputStream in = new FileInputStream(file);38DataInputStream dis = new DataInputStream(in);) {39byte[] buffer = new byte[100];40dis.readFully(buffer, -1, buffer.length);41throw new RuntimeException("Test testNegativeOffset() failed");42} catch (IndexOutOfBoundsException ignore) {43}44}4546private static final void testNegativeLength() throws Exception {47File file = new File(System.getProperty("test.src"),48"ReadFully.java");49try (FileInputStream in = new FileInputStream(file);50DataInputStream dis = new DataInputStream(in);) {51byte[] buffer = new byte[100];52dis.readFully(buffer, 0, -1);53throw new RuntimeException("Test testNegativeLength() failed");54} catch (IndexOutOfBoundsException ignore) {55}56}5758private static final void testNegativeOffsetZeroLength() throws Exception {59File file = new File(System.getProperty("test.src"),60"ReadFully.java");61try (FileInputStream in = new FileInputStream(file);62DataInputStream dis = new DataInputStream(in);) {63byte[] buffer = new byte[100];64dis.readFully(buffer, -1, 0);65throw new RuntimeException("Test testNegativeOffsetZeroLength() failed");66} catch (IndexOutOfBoundsException ignore) {67}68}6970private static final void testBigOffsetLength1() throws Exception {71File file = new File(System.getProperty("test.src"),72"ReadFully.java");73try (FileInputStream in = new FileInputStream(file);74DataInputStream dis = new DataInputStream(in);) {75byte[] buffer = new byte[100];76dis.readFully(buffer, 0, buffer.length + 1);77throw new RuntimeException("Test testBigOffsetLength1() failed");78} catch (IndexOutOfBoundsException ignore) {79}80}8182private static final void testBigOffsetLength2() throws Exception {83File file = new File(System.getProperty("test.src"),84"ReadFully.java");85try (FileInputStream in = new FileInputStream(file);86DataInputStream dis = new DataInputStream(in);) {87byte[] buffer = new byte[100];88dis.readFully(buffer, 1, buffer.length);89throw new RuntimeException("Test testBigOffsetLength2() failed");90} catch (IndexOutOfBoundsException ignore) {91}92}9394private static final void testBigOffsetLength3() throws Exception {95File file = new File(System.getProperty("test.src"),96"ReadFully.java");97try (FileInputStream in = new FileInputStream(file);98DataInputStream dis = new DataInputStream(in);) {99byte[] buffer = new byte[100];100dis.readFully(buffer, buffer.length, 1);101throw new RuntimeException("Test testBigOffsetLength3() failed");102} catch (IndexOutOfBoundsException ignore) {103}104}105106private static final void testBigOffsetLength4() throws Exception {107File file = new File(System.getProperty("test.src"),108"ReadFully.java");109try (FileInputStream in = new FileInputStream(file);110DataInputStream dis = new DataInputStream(in);) {111byte[] buffer = new byte[100];112dis.readFully(buffer, buffer.length + 1, 0);113throw new RuntimeException("Test testBigOffsetLength4() failed");114} catch (IndexOutOfBoundsException ignore) {115}116}117118public static final void main(String[] args) throws Exception {119testNegativeOffset();120testNegativeLength();121testNegativeOffsetZeroLength();122testBigOffsetLength1();123testBigOffsetLength2();124testBigOffsetLength3();125testBigOffsetLength4();126}127128}129130131