author | dxu |
Wed, 27 Mar 2013 09:00:34 -0700 | |
changeset 16716 | 6d3f9ce0d776 |
child 17691 | 13931cd9405f |
permissions | -rw-r--r-- |
16716
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
1 |
/* |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
4 |
* |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
8 |
* |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
13 |
* accompanied this code). |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
14 |
* |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
18 |
* |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
21 |
* questions. |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
22 |
*/ |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
23 |
|
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
24 |
/* |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
25 |
* @test |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
26 |
* @bug 8010837 |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
27 |
* @summary Test if available returns correct value when skipping beyond |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
28 |
* the end of a file. |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
29 |
* @author Dan Xu |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
30 |
*/ |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
31 |
|
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
32 |
import java.io.BufferedWriter; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
33 |
import java.io.File; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
34 |
import java.io.FileInputStream; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
35 |
import java.io.IOException; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
36 |
import java.nio.charset.Charset; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
37 |
import java.nio.file.Files; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
38 |
import java.nio.file.Path; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
39 |
|
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
40 |
public class NegativeAvailable { |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
41 |
|
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
42 |
public static void main(String[] args) throws IOException { |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
43 |
final int SIZE = 10; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
44 |
final int SKIP = 5; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
45 |
|
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
46 |
// Create a temporary file with size of 10 bytes. |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
47 |
Path tmp = Files.createTempFile(null, null); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
48 |
try (BufferedWriter writer = |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
49 |
Files.newBufferedWriter(tmp, Charset.defaultCharset())) { |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
50 |
for (int i = 0; i < SIZE; i++) { |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
51 |
writer.write('1'); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
52 |
} |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
53 |
} |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
54 |
|
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
55 |
File tempFile = tmp.toFile(); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
56 |
try (FileInputStream fis = new FileInputStream(tempFile)) { |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
57 |
if (tempFile.length() != SIZE) { |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
58 |
throw new RuntimeException("unexpected file size = " |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
59 |
+ tempFile.length()); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
60 |
} |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
61 |
long space = skipBytes(fis, SKIP, SIZE); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
62 |
space = skipBytes(fis, SKIP, space); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
63 |
space = skipBytes(fis, SKIP, space); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
64 |
space = skipBytes(fis, SKIP, space); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
65 |
} |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
66 |
Files.deleteIfExists(tmp); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
67 |
} |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
68 |
|
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
69 |
/** |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
70 |
* Skip toSkip number of bytes and return the remaining bytes of the file. |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
71 |
*/ |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
72 |
private static long skipBytes(FileInputStream fis, int toSkip, long space) |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
73 |
throws IOException { |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
74 |
long skip = fis.skip(toSkip); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
75 |
if (skip != toSkip) { |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
76 |
throw new RuntimeException("skip() returns " + skip |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
77 |
+ " but expected " + toSkip); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
78 |
} |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
79 |
long remaining = space - toSkip; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
80 |
int avail = fis.available(); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
81 |
if (avail != remaining) { |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
82 |
throw new RuntimeException("available() returns " + avail |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
83 |
+ " but expected " + remaining); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
84 |
} |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
85 |
|
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
86 |
System.out.println("Skipped " + skip + " bytes " |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
87 |
+ " available() returns " + avail); |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
88 |
return remaining; |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
89 |
} |
6d3f9ce0d776
8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff
changeset
|
90 |
} |