1 /* 2 * Copyright (c) 2019, 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 /* @test 25 * @bug 8227609 26 * @summary Test of InputStream and OutputStream created by java.nio.file.Files 27 * @library .. 28 */ 29 30 import java.io.InputStream; 31 import java.io.OutputStream; 32 import java.nio.channels.ClosedChannelException; 33 import java.nio.file.*; 34 import static java.nio.file.Files.*; 35 import static java.nio.file.LinkOption.*; 36 import java.nio.file.attribute.*; 37 import java.io.IOException; 38 import java.util.*; 39 40 public class InputStreamTest { 41 main(String[] args)42 public static void main(String[] args) throws IOException { 43 Path dir = TestUtil.createTemporaryDirectory(); 44 try { 45 testSkip(dir); 46 } finally { 47 TestUtil.removeAll(dir); 48 } 49 } 50 51 /** 52 * Tests Files.newInputStream(Path).skip(). 53 */ testSkip(Path tmpdir)54 static void testSkip(Path tmpdir) throws IOException { 55 Path file = createFile(tmpdir.resolve("foo")); 56 try (OutputStream out = Files.newOutputStream(file)) { 57 final int size = 512; 58 byte[] blah = new byte[size]; 59 for (int i = 0; i < size; i++) { 60 blah[i] = (byte)(i % 128); 61 } 62 out.write(blah); 63 out.close(); 64 65 try (InputStream in = Files.newInputStream(file)) { 66 assertTrue(in.available() == size); 67 assertTrue(in.skip(size/4) == size/4); // 0.25 68 assertTrue(in.available() == 3*size/4); 69 70 int b = in.read(); 71 assertTrue(b == blah[size/4]); 72 assertTrue(in.available() == 3*size/4 - 1); 73 assertTrue(in.skip(-1) == -1); // 0.25 74 assertTrue(in.available() == 3*size/4); 75 76 assertTrue(in.skip(-size/2) == -size/4); // 0 77 assertTrue(in.available() == size); 78 79 assertTrue(in.skip(5*size/4) == size); // 1.0 80 assertTrue(in.available() == 0); 81 82 assertTrue(in.skip(-3*size/4) == -3*size/4); // 0.25 83 assertTrue(in.available() == 3*size/4); 84 85 byte[] buf = new byte[16]; 86 in.read(buf, 2, 12); 87 assertTrue(Arrays.equals(buf, 2, 14, 88 blah, size/4, size/4 + 12)); 89 assertTrue(in.skip(-12) == -12); // 0.25 90 91 assertTrue(in.skip(3*size/4) == 3*size/4); // 1.0 92 assertTrue(in.available() == 0); 93 94 assertTrue(in.skip(-size/2) == -size/2); // 0.5 95 assertTrue(in.available() == size/2); 96 97 assertTrue(in.skip(-size) == -size/2); // 0 98 assertTrue(in.available() == size); 99 100 assertTrue(in.skip(size/2) == size/2); // 0.5 101 assertTrue(in.available() == size/2); 102 103 assertTrue(in.skip(Long.MIN_VALUE) == -size/2); // 0 104 assertTrue(in.available() == size); 105 106 assertTrue(in.skip(size/2) == size/2); // 0.5 107 assertTrue(in.available() == size/2); 108 109 assertTrue(in.skip(Long.MAX_VALUE - size/4) == size/2); 110 assertTrue(in.available() == 0); 111 112 in.close(); 113 try { 114 in.skip(1); 115 throw new RuntimeException("skip() did not fail"); 116 } catch (IOException ioe) { 117 if (!(ioe instanceof ClosedChannelException)) { 118 throw new RuntimeException 119 ("IOException is not a ClosedChannelException"); 120 } 121 } 122 } 123 } 124 } 125 assertTrue(boolean okay)126 static void assertTrue(boolean okay) { 127 if (!okay) 128 throw new RuntimeException("Assertion Failed"); 129 } 130 } 131