1 /* 2 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @library /test/lib 27 * @build jdk.test.lib.RandomFactory 28 * @run main ReadXBytes 29 * @bug 8264777 30 * @summary Test read{All,N}Bytes overrides (use -Dseed=X to set PRNG seed) 31 * @key randomness 32 */ 33 34 package test.java.io.FileInputStream; 35 36 import java.io.File; 37 import java.io.FileInputStream; 38 import java.io.IOException; 39 import java.io.RandomAccessFile; 40 import java.util.Arrays; 41 import java.util.Random; 42 import jdk.test.lib.RandomFactory; 43 44 public class ReadXBytes { 45 private static final int ITERATIONS = 10; 46 private static final int MAX_FILE_SIZE = 1_000_000; 47 private static final Random RND = RandomFactory.getRandom(); 48 main(String args[])49 public static void main(String args[]) throws IOException { 50 // Android-removed: dir is a read-only directory on Android. 51 // File dir = new File(System.getProperty("test.src", ".")); 52 // dir.deleteOnExit(); 53 54 // File empty = File.createTempFile("foo", "bar", dir); 55 File empty = File.createTempFile("foo", "bar"); 56 empty.deleteOnExit(); 57 try (FileInputStream fis = new FileInputStream(empty)) { 58 try { 59 fis.readNBytes(-1); 60 throw new RuntimeException("IllegalArgumentException expected"); 61 } catch (IllegalArgumentException expected) { 62 } 63 byte[] nbytes = fis.readNBytes(0); 64 if (nbytes.length != 0) 65 throw new RuntimeException("readNBytes() zero length for empty"); 66 67 byte[] b = fis.readNBytes(1); 68 if (b.length != 0) 69 throw new RuntimeException("readNBytes: zero-length byte[] expected"); 70 71 b = fis.readAllBytes(); 72 if (b.length != 0) 73 throw new RuntimeException("readAllBytes: zero-length byte[] expected"); 74 } 75 76 for (int i = 0; i < ITERATIONS; i++) { 77 // Android-removed: dir is a read-only directory on Android. 78 // File file = File.createTempFile("foo", "bar", dir); 79 File file = File.createTempFile("foo", "bar"); 80 file.deleteOnExit(); 81 82 int size = 1 + RND.nextInt(MAX_FILE_SIZE); 83 System.out.printf("size %d%n", size); 84 byte[] bytes = new byte[size]; 85 RND.nextBytes(bytes); 86 try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) { 87 raf.write(bytes); 88 } 89 90 try (FileInputStream fis = new FileInputStream(file)) { 91 int pos = RND.nextInt(size); 92 int len = RND.nextInt(size - pos); 93 fis.getChannel().position(pos); 94 byte[] nbytes = fis.readNBytes(0); 95 if (nbytes.length != 0) 96 throw new RuntimeException("readNBytes() zero length"); 97 nbytes = fis.readNBytes(len); 98 if (nbytes.length != len) 99 throw new RuntimeException("readNBytes() length"); 100 if (!Arrays.equals(nbytes, 0, len, bytes, pos, pos + len)) 101 throw new RuntimeException("readNBytes() content"); 102 } 103 104 try (FileInputStream fis = new FileInputStream(file)) { 105 int pos = RND.nextInt(size); 106 fis.getChannel().position(pos); 107 byte[] allbytes = fis.readAllBytes(); 108 if (allbytes.length != size - pos) 109 throw new RuntimeException("readAllBytes() length"); 110 if (!Arrays.equals(allbytes, 0, allbytes.length, 111 bytes, pos, pos + allbytes.length)) 112 throw new RuntimeException("readAllBytes() content"); 113 } 114 115 file.delete(); 116 } 117 // Android-removed: dir is a read-only directory on Android. 118 // dir.delete(); 119 } 120 } 121