0N/A/*
3261N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
787N/A * @test
787N/A * @bug 4017728 4079849 6788196
787N/A * @summary Check for correct Array Bounds check in read of FileInputStream and
787N/A * RandomAccessFile
787N/A */
0N/A
0N/Aimport java.io.*;
0N/A
0N/A/*
787N/A * The test calls the read(byte buf[] , int off , int len) of
787N/A * FileInputStream with different values of off and len to see if the
787N/A * IndexOutOfBoundsException is thrown. The read(...) method calls
787N/A * readBytes(...) in native code(io_util.c). The read(...) method in
787N/A * RandomAccessFile also calls the same native method. So one should
787N/A * see similar results.
0N/A */
0N/A
0N/Apublic class ReadBytesBounds {
0N/A
787N/A static final FileInputStream fis;
787N/A static final RandomAccessFile raf;
787N/A static final byte[] b = new byte[32];
0N/A
787N/A static {
787N/A try {
787N/A String dir = System.getProperty("test.src", ".");
787N/A File testFile = new File(dir, "input.txt");
787N/A fis = new FileInputStream(testFile);
787N/A raf = new RandomAccessFile(testFile , "r");
787N/A } catch (Throwable t) {
787N/A throw new Error(t);
787N/A }
787N/A }
0N/A
787N/A public static void main(String argv[]) throws Throwable {
2497N/A try {
2497N/A testRead(-1, -1, false);
2497N/A testRead(-1, 0, false);
2497N/A testRead( 0, -1, false);
2497N/A testRead( 0, 33, false);
2497N/A testRead(33, 0, false);
2497N/A testRead(33, 4, false);
2497N/A testRead( 0, 32, true);
2497N/A testRead(32, 0, true);
2497N/A testRead(32, 4, false);
2497N/A testRead( 4, 16, true);
2497N/A testRead( 1, 31, true);
2497N/A testRead( 0, 0, true);
2497N/A testRead(31, Integer.MAX_VALUE, false);
2497N/A testRead( 0, Integer.MAX_VALUE, false);
2497N/A testRead(-1, Integer.MAX_VALUE, false);
2497N/A testRead(-4, Integer.MIN_VALUE, false);
2497N/A testRead( 0, Integer.MIN_VALUE, false);
2497N/A } finally {
2497N/A fis.close();
2497N/A raf.close();
2497N/A }
787N/A }
0N/A
787N/A static void testRead(int off, int len, boolean expected) throws Throwable {
787N/A System.err.printf("off=%d len=%d expected=%b%n", off, len, expected);
787N/A boolean result;
787N/A try {
787N/A fis.read(b, off, len);
787N/A raf.read(b, off, len);
787N/A result = true;
787N/A } catch (IndexOutOfBoundsException e) {
787N/A result = false;
0N/A }
0N/A
787N/A if (result != expected) {
787N/A throw new RuntimeException
787N/A (String.format("Unexpected result off=%d len=%d expected=%b",
787N/A off, len, expected));
787N/A }
0N/A }
0N/A}