Path: blob/master/test/jdk/java/io/FileInputStream/ReadXBytes.java
41149 views
/*1* Copyright (c) 2021, 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* @library /test/lib26* @build jdk.test.lib.RandomFactory27* @run main ReadXBytes28* @bug 826477729* @summary Test read{All,N}Bytes overrides (use -Dseed=X to set PRNG seed)30* @key randomness31*/3233import java.io.File;34import java.io.FileInputStream;35import java.io.IOException;36import java.io.RandomAccessFile;37import java.util.Arrays;38import java.util.Random;39import jdk.test.lib.RandomFactory;4041public class ReadXBytes {42private static final int ITERATIONS = 10;43private static final int MAX_FILE_SIZE = 1_000_000;44private static final Random RND = RandomFactory.getRandom();4546public static void main(String args[]) throws IOException {47File dir = new File(System.getProperty("test.src", "."));48dir.deleteOnExit();4950File empty = File.createTempFile("foo", "bar", dir);51empty.deleteOnExit();52try (FileInputStream fis = new FileInputStream(empty)) {53try {54fis.readNBytes(-1);55throw new RuntimeException("IllegalArgumentException expected");56} catch (IllegalArgumentException expected) {57}58byte[] nbytes = fis.readNBytes(0);59if (nbytes.length != 0)60throw new RuntimeException("readNBytes() zero length for empty");6162byte[] b = fis.readNBytes(1);63if (b.length != 0)64throw new RuntimeException("readNBytes: zero-length byte[] expected");6566b = fis.readAllBytes();67if (b.length != 0)68throw new RuntimeException("readAllBytes: zero-length byte[] expected");69}7071for (int i = 0; i < ITERATIONS; i++) {72File file = File.createTempFile("foo", "bar", dir);73file.deleteOnExit();7475int size = 1 + RND.nextInt(MAX_FILE_SIZE);76System.out.printf("size %d%n", size);77byte[] bytes = new byte[size];78RND.nextBytes(bytes);79try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {80raf.write(bytes);81}8283try (FileInputStream fis = new FileInputStream(file)) {84int pos = RND.nextInt(size);85int len = RND.nextInt(size - pos);86fis.getChannel().position(pos);87byte[] nbytes = fis.readNBytes(0);88if (nbytes.length != 0)89throw new RuntimeException("readNBytes() zero length");90nbytes = fis.readNBytes(len);91if (nbytes.length != len)92throw new RuntimeException("readNBytes() length");93if (!Arrays.equals(nbytes, 0, len, bytes, pos, pos + len))94throw new RuntimeException("readNBytes() content");95}9697try (FileInputStream fis = new FileInputStream(file)) {98int pos = RND.nextInt(size);99fis.getChannel().position(pos);100byte[] allbytes = fis.readAllBytes();101if (allbytes.length != size - pos)102throw new RuntimeException("readAllBytes() length");103if (!Arrays.equals(allbytes, 0, allbytes.length,104bytes, pos, pos + allbytes.length))105throw new RuntimeException("readAllBytes() content");106}107108file.delete();109}110dir.delete();111}112}113114115