Path: blob/master/test/jdk/java/io/FileInputStream/NegativeAvailable.java
41149 views
/*1* Copyright (c) 2013, 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* @bug 8010837 801113626* @summary Test if available returns correct value when skipping beyond27* the end of a file.28* @author Dan Xu29*/3031import java.io.BufferedWriter;32import java.io.File;33import java.io.FileInputStream;34import java.io.IOException;35import java.nio.charset.Charset;36import java.nio.file.Files;37import java.nio.file.Path;3839public class NegativeAvailable {4041public static void main(String[] args) throws IOException {42final int SIZE = 10;43final int SKIP = 5;44final int NEGATIVE_SKIP = -5;4546// Create a temporary file with size of 10 bytes.47Path tmp = Files.createTempFile(null, null);48try (BufferedWriter writer =49Files.newBufferedWriter(tmp, Charset.defaultCharset())) {50for (int i = 0; i < SIZE; i++) {51writer.write('1');52}53}5455File tempFile = tmp.toFile();56try (FileInputStream fis = new FileInputStream(tempFile)) {57if (tempFile.length() != SIZE) {58throw new RuntimeException("unexpected file size = "59+ tempFile.length());60}61long space = skipBytes(fis, SKIP, SIZE);62space = skipBytes(fis, NEGATIVE_SKIP, space);63space = skipBytes(fis, SKIP, space);64space = skipBytes(fis, SKIP, space);65space = skipBytes(fis, SKIP, space);66space = skipBytes(fis, NEGATIVE_SKIP, space);67space = skipBytes(fis, NEGATIVE_SKIP, space);68}69Files.deleteIfExists(tmp);70}7172/**73* Skip toSkip number of bytes and return the remaining bytes of the file.74*/75private static long skipBytes(FileInputStream fis, int toSkip, long space)76throws IOException {77long skip = fis.skip(toSkip);78if (skip != toSkip) {79throw new RuntimeException("skip() returns " + skip80+ " but expected " + toSkip);81}82long newSpace = space - toSkip;83long remaining = newSpace > 0 ? newSpace : 0;84int avail = fis.available();85if (avail != remaining) {86throw new RuntimeException("available() returns " + avail87+ " but expected " + remaining);88}8990System.out.println("Skipped " + skip + " bytes "91+ " available() returns " + avail);92return newSpace;93}94}959697