8010837: FileInputStream.available() throw IOException when encountering negative available values
Summary: Remove the check in the native code to allow negative values
Reviewed-by: mchung
--- a/jdk/src/solaris/native/java/io/io_util_md.c Thu Mar 21 20:35:49 2013 +0100
+++ b/jdk/src/solaris/native/java/io/io_util_md.c Wed Mar 27 09:00:34 2013 -0700
@@ -200,12 +200,8 @@
return 0;
}
- if (size >= current) {
- *pbytes = size - current;
- return 1;
- } else {
- return 0;
- }
+ *pbytes = size - current;
+ return 1;
}
jint
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/io/FileInputStream/NegativeAvailable.java Wed Mar 27 09:00:34 2013 -0700
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8010837
+ * @summary Test if available returns correct value when skipping beyond
+ * the end of a file.
+ * @author Dan Xu
+ */
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class NegativeAvailable {
+
+ public static void main(String[] args) throws IOException {
+ final int SIZE = 10;
+ final int SKIP = 5;
+
+ // Create a temporary file with size of 10 bytes.
+ Path tmp = Files.createTempFile(null, null);
+ try (BufferedWriter writer =
+ Files.newBufferedWriter(tmp, Charset.defaultCharset())) {
+ for (int i = 0; i < SIZE; i++) {
+ writer.write('1');
+ }
+ }
+
+ File tempFile = tmp.toFile();
+ try (FileInputStream fis = new FileInputStream(tempFile)) {
+ if (tempFile.length() != SIZE) {
+ throw new RuntimeException("unexpected file size = "
+ + tempFile.length());
+ }
+ long space = skipBytes(fis, SKIP, SIZE);
+ space = skipBytes(fis, SKIP, space);
+ space = skipBytes(fis, SKIP, space);
+ space = skipBytes(fis, SKIP, space);
+ }
+ Files.deleteIfExists(tmp);
+ }
+
+ /**
+ * Skip toSkip number of bytes and return the remaining bytes of the file.
+ */
+ private static long skipBytes(FileInputStream fis, int toSkip, long space)
+ throws IOException {
+ long skip = fis.skip(toSkip);
+ if (skip != toSkip) {
+ throw new RuntimeException("skip() returns " + skip
+ + " but expected " + toSkip);
+ }
+ long remaining = space - toSkip;
+ int avail = fis.available();
+ if (avail != remaining) {
+ throw new RuntimeException("available() returns " + avail
+ + " but expected " + remaining);
+ }
+
+ System.out.println("Skipped " + skip + " bytes "
+ + " available() returns " + avail);
+ return remaining;
+ }
+}