|
1 /* |
|
2 * Copyright (c) 2013, 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 * @bug 8010837 |
|
27 * @summary Test if available returns correct value when skipping beyond |
|
28 * the end of a file. |
|
29 * @author Dan Xu |
|
30 */ |
|
31 |
|
32 import java.io.BufferedWriter; |
|
33 import java.io.File; |
|
34 import java.io.FileInputStream; |
|
35 import java.io.IOException; |
|
36 import java.nio.charset.Charset; |
|
37 import java.nio.file.Files; |
|
38 import java.nio.file.Path; |
|
39 |
|
40 public class NegativeAvailable { |
|
41 |
|
42 public static void main(String[] args) throws IOException { |
|
43 final int SIZE = 10; |
|
44 final int SKIP = 5; |
|
45 |
|
46 // Create a temporary file with size of 10 bytes. |
|
47 Path tmp = Files.createTempFile(null, null); |
|
48 try (BufferedWriter writer = |
|
49 Files.newBufferedWriter(tmp, Charset.defaultCharset())) { |
|
50 for (int i = 0; i < SIZE; i++) { |
|
51 writer.write('1'); |
|
52 } |
|
53 } |
|
54 |
|
55 File tempFile = tmp.toFile(); |
|
56 try (FileInputStream fis = new FileInputStream(tempFile)) { |
|
57 if (tempFile.length() != SIZE) { |
|
58 throw new RuntimeException("unexpected file size = " |
|
59 + tempFile.length()); |
|
60 } |
|
61 long space = skipBytes(fis, SKIP, SIZE); |
|
62 space = skipBytes(fis, SKIP, space); |
|
63 space = skipBytes(fis, SKIP, space); |
|
64 space = skipBytes(fis, SKIP, space); |
|
65 } |
|
66 Files.deleteIfExists(tmp); |
|
67 } |
|
68 |
|
69 /** |
|
70 * Skip toSkip number of bytes and return the remaining bytes of the file. |
|
71 */ |
|
72 private static long skipBytes(FileInputStream fis, int toSkip, long space) |
|
73 throws IOException { |
|
74 long skip = fis.skip(toSkip); |
|
75 if (skip != toSkip) { |
|
76 throw new RuntimeException("skip() returns " + skip |
|
77 + " but expected " + toSkip); |
|
78 } |
|
79 long remaining = space - toSkip; |
|
80 int avail = fis.available(); |
|
81 if (avail != remaining) { |
|
82 throw new RuntimeException("available() returns " + avail |
|
83 + " but expected " + remaining); |
|
84 } |
|
85 |
|
86 System.out.println("Skipped " + skip + " bytes " |
|
87 + " available() returns " + avail); |
|
88 return remaining; |
|
89 } |
|
90 } |