Path: blob/master/test/jdk/java/nio/file/Files/InputStreamTest.java
41153 views
/*1* Copyright (c) 2019, 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 822760925* @summary Test of InputStream and OutputStream created by java.nio.file.Files26* @library ..27*/2829import java.io.InputStream;30import java.io.OutputStream;31import java.nio.channels.ClosedChannelException;32import java.nio.file.*;33import static java.nio.file.Files.*;34import static java.nio.file.LinkOption.*;35import java.nio.file.attribute.*;36import java.io.IOException;37import java.util.*;3839public class InputStreamTest {4041public static void main(String[] args) throws IOException {42Path dir = TestUtil.createTemporaryDirectory();43try {44testSkip(dir);45} finally {46TestUtil.removeAll(dir);47}48}4950/**51* Tests Files.newInputStream(Path).skip().52*/53static void testSkip(Path tmpdir) throws IOException {54Path file = createFile(tmpdir.resolve("foo"));55try (OutputStream out = Files.newOutputStream(file)) {56final int size = 512;57byte[] blah = new byte[size];58for (int i = 0; i < size; i++) {59blah[i] = (byte)(i % 128);60}61out.write(blah);62out.close();6364try (InputStream in = Files.newInputStream(file)) {65assertTrue(in.available() == size);66assertTrue(in.skip(size/4) == size/4); // 0.2567assertTrue(in.available() == 3*size/4);6869int b = in.read();70assertTrue(b == blah[size/4]);71assertTrue(in.available() == 3*size/4 - 1);72assertTrue(in.skip(-1) == -1); // 0.2573assertTrue(in.available() == 3*size/4);7475assertTrue(in.skip(-size/2) == -size/4); // 076assertTrue(in.available() == size);7778assertTrue(in.skip(5*size/4) == size); // 1.079assertTrue(in.available() == 0);8081assertTrue(in.skip(-3*size/4) == -3*size/4); // 0.2582assertTrue(in.available() == 3*size/4);8384byte[] buf = new byte[16];85in.read(buf, 2, 12);86assertTrue(Arrays.equals(buf, 2, 14,87blah, size/4, size/4 + 12));88assertTrue(in.skip(-12) == -12); // 0.258990assertTrue(in.skip(3*size/4) == 3*size/4); // 1.091assertTrue(in.available() == 0);9293assertTrue(in.skip(-size/2) == -size/2); // 0.594assertTrue(in.available() == size/2);9596assertTrue(in.skip(-size) == -size/2); // 097assertTrue(in.available() == size);9899assertTrue(in.skip(size/2) == size/2); // 0.5100assertTrue(in.available() == size/2);101102assertTrue(in.skip(Long.MIN_VALUE) == -size/2); // 0103assertTrue(in.available() == size);104105assertTrue(in.skip(size/2) == size/2); // 0.5106assertTrue(in.available() == size/2);107108assertTrue(in.skip(Long.MAX_VALUE - size/4) == size/2);109assertTrue(in.available() == 0);110111in.close();112try {113in.skip(1);114throw new RuntimeException("skip() did not fail");115} catch (IOException ioe) {116if (!(ioe instanceof ClosedChannelException)) {117throw new RuntimeException118("IOException is not a ClosedChannelException");119}120}121}122}123}124125static void assertTrue(boolean okay) {126if (!okay)127throw new RuntimeException("Assertion Failed");128}129}130131132