author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
child 54823 | 1b940da275d2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
25 |
* @bug 4429043 8002180 |
2 | 26 |
* @summary Test file mapping with FileChannel |
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
27 |
* @run main/othervm MapTest |
30046 | 28 |
* @key randomness |
2 | 29 |
*/ |
30 |
||
31 |
import java.io.*; |
|
32 |
import java.nio.MappedByteBuffer; |
|
33 |
import java.nio.channels.*; |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
34 |
import java.nio.channels.FileChannel.MapMode; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
35 |
import java.nio.file.Files; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
36 |
import static java.nio.file.StandardOpenOption.*; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
37 |
import static java.nio.charset.StandardCharsets.*; |
2 | 38 |
import java.util.Random; |
39 |
||
40 |
||
41 |
/** |
|
42 |
* Testing FileChannel's mapping capabilities. |
|
43 |
*/ |
|
44 |
||
45 |
public class MapTest { |
|
46 |
||
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
47 |
private static PrintStream out = System.out; |
2 | 48 |
private static PrintStream err = System.err; |
49 |
||
50 |
private static Random generator = new Random(); |
|
51 |
||
52 |
private static int CHARS_PER_LINE = File.separatorChar == '/' ? 5 : 6; |
|
53 |
||
54 |
private static File blah; |
|
55 |
||
56 |
public static void main(String[] args) throws Exception { |
|
57 |
blah = File.createTempFile("blah", null); |
|
58 |
blah.deleteOnExit(); |
|
59 |
initTestFile(blah); |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
60 |
try { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
61 |
out.println("Test file " + blah + " initialized"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
62 |
testZero(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
63 |
out.println("Zero size: OK"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
64 |
testRead(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
65 |
out.println("Read: OK"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
66 |
testWrite(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
67 |
out.println("Write: OK"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
68 |
testHighOffset(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
69 |
out.println("High offset: OK"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
70 |
testExceptions(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
71 |
out.println("Exceptions: OK"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
72 |
} finally { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
73 |
blah.delete(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
74 |
} |
2 | 75 |
} |
76 |
||
77 |
/** |
|
78 |
* Creates file blah: |
|
79 |
* 0000 |
|
80 |
* 0001 |
|
81 |
* 0002 |
|
82 |
* 0003 |
|
83 |
* . |
|
84 |
* . |
|
85 |
* . |
|
86 |
* 3999 |
|
87 |
* |
|
88 |
* Blah extends beyond a single page of memory so that the |
|
89 |
* ability to index into a file of multiple pages is tested. |
|
90 |
*/ |
|
91 |
private static void initTestFile(File blah) throws Exception { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
92 |
try (BufferedWriter writer = Files.newBufferedWriter(blah.toPath(), ISO_8859_1)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
93 |
for (int i=0; i<4000; i++) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
94 |
String number = new Integer(i).toString(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
95 |
for (int h=0; h<4-number.length(); h++) |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
96 |
writer.write("0"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
97 |
writer.write(""+i); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
98 |
writer.newLine(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
99 |
} |
2 | 100 |
} |
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Tests zero size file mapping |
|
105 |
*/ |
|
106 |
private static void testZero() throws Exception { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
107 |
try (FileInputStream fis = new FileInputStream(blah)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
108 |
FileChannel fc = fis.getChannel(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
109 |
MappedByteBuffer b = fc.map(MapMode.READ_ONLY, 0, 0); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
110 |
} |
2 | 111 |
} |
112 |
||
113 |
/** |
|
114 |
* Maps blah file with a random offset and checks to see if read |
|
115 |
* from the ByteBuffer gets the right line number |
|
116 |
*/ |
|
117 |
private static void testRead() throws Exception { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
118 |
StringBuilder sb = new StringBuilder(); |
2 | 119 |
sb.setLength(4); |
120 |
||
121 |
for (int x=0; x<1000; x++) { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
122 |
try (FileInputStream fis = new FileInputStream(blah)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
123 |
FileChannel fc = fis.getChannel(); |
2 | 124 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
125 |
long offset = generator.nextInt(10000); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
126 |
long expectedResult = offset / CHARS_PER_LINE; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
127 |
offset = expectedResult * CHARS_PER_LINE; |
2 | 128 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
129 |
MappedByteBuffer b = fc.map(MapMode.READ_ONLY, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
130 |
offset, 100); |
2 | 131 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
132 |
for (int i=0; i<4; i++) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
133 |
byte aByte = b.get(i); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
134 |
sb.setCharAt(i, (char)aByte); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
135 |
} |
2 | 136 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
137 |
int result = Integer.parseInt(sb.toString()); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
138 |
if (result != expectedResult) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
139 |
err.println("I expected "+expectedResult); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
140 |
err.println("I got "+result); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
141 |
throw new Exception("Read test failed"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
142 |
} |
2 | 143 |
} |
144 |
} |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Maps blah file with a random offset and checks to see if data |
|
149 |
* written out to the file can be read back in |
|
150 |
*/ |
|
151 |
private static void testWrite() throws Exception { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
152 |
StringBuilder sb = new StringBuilder(); |
2 | 153 |
sb.setLength(4); |
154 |
||
155 |
for (int x=0; x<1000; x++) { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
156 |
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
157 |
FileChannel fc = raf.getChannel(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
158 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
159 |
long offset = generator.nextInt(1000); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
160 |
MappedByteBuffer b = fc.map(MapMode.READ_WRITE, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
161 |
offset, 100); |
2 | 162 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
163 |
for (int i=0; i<4; i++) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
164 |
b.put(i, (byte)('0' + i)); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
165 |
} |
2 | 166 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
167 |
for (int i=0; i<4; i++) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
168 |
byte aByte = b.get(i); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
169 |
sb.setCharAt(i, (char)aByte); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
170 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
171 |
if (!sb.toString().equals("0123")) |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
172 |
throw new Exception("Write test failed"); |
2 | 173 |
} |
174 |
} |
|
175 |
} |
|
176 |
||
177 |
private static void testHighOffset() throws Exception { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
178 |
StringBuilder sb = new StringBuilder(); |
2 | 179 |
sb.setLength(4); |
180 |
||
181 |
for (int x=0; x<1000; x++) { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
182 |
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
183 |
FileChannel fc = raf.getChannel(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
184 |
long offset = 66000; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
185 |
MappedByteBuffer b = fc.map(MapMode.READ_WRITE, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
186 |
offset, 100); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
187 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
188 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
189 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
190 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
191 |
/** |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
192 |
* Test exceptions specified by map method |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
193 |
*/ |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
194 |
private static void testExceptions() throws Exception { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
195 |
// check exceptions when channel opened for read access |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
196 |
try (FileChannel fc = FileChannel.open(blah.toPath(), READ)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
197 |
testExceptions(fc); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
198 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
199 |
checkException(fc, MapMode.READ_WRITE, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
200 |
NonWritableChannelException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
201 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
202 |
checkException(fc, MapMode.READ_WRITE, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
203 |
NonWritableChannelException.class, IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
204 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
205 |
checkException(fc, MapMode.READ_WRITE, 0L, -1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
206 |
NonWritableChannelException.class, IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
207 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
208 |
checkException(fc, MapMode.PRIVATE, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
209 |
NonWritableChannelException.class); |
2 | 210 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
211 |
checkException(fc, MapMode.PRIVATE, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
212 |
NonWritableChannelException.class, IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
213 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
214 |
checkException(fc, MapMode.PRIVATE, 0L, -1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
215 |
NonWritableChannelException.class, IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
216 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
217 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
218 |
// check exceptions when channel opened for write access |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
219 |
try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
220 |
testExceptions(fc); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
221 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
222 |
checkException(fc, MapMode.READ_ONLY, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
223 |
NonReadableChannelException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
224 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
225 |
checkException(fc, MapMode.READ_ONLY, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
226 |
NonReadableChannelException.class, IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
227 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
228 |
/* |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
229 |
* implementation/spec mismatch, these tests disabled for now |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
230 |
*/ |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
231 |
//checkException(fc, MapMode.READ_WRITE, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
232 |
// NonWritableChannelException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
233 |
//checkException(fc, MapMode.PRIVATE, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
234 |
// NonWritableChannelException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
235 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
236 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
237 |
// check exceptions when channel opened for read and write access |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
238 |
try (FileChannel fc = FileChannel.open(blah.toPath(), READ, WRITE)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
239 |
testExceptions(fc); |
2 | 240 |
} |
241 |
} |
|
242 |
||
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
243 |
private static void testExceptions(FileChannel fc) throws IOException { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
244 |
checkException(fc, null, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
245 |
NullPointerException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
246 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
247 |
checkException(fc, MapMode.READ_ONLY, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
248 |
IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
249 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
250 |
checkException(fc, null, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
251 |
IllegalArgumentException.class, NullPointerException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
252 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
253 |
checkException(fc, MapMode.READ_ONLY, 0L, -1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
254 |
IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
255 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
256 |
checkException(fc, null, 0L, -1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
257 |
IllegalArgumentException.class, NullPointerException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
258 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
259 |
checkException(fc, MapMode.READ_ONLY, 0L, Integer.MAX_VALUE + 1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
260 |
IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
261 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
262 |
checkException(fc, null, 0L, Integer.MAX_VALUE + 1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
263 |
IllegalArgumentException.class, NullPointerException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
264 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
265 |
checkException(fc, MapMode.READ_ONLY, Long.MAX_VALUE, 1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
266 |
IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
267 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
268 |
checkException(fc, null, Long.MAX_VALUE, 1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
269 |
IllegalArgumentException.class, NullPointerException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
270 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
271 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
272 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
273 |
/** |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
274 |
* Checks that FileChannel map throws one of the expected exceptions |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
275 |
* when invoked with the given inputs. |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
276 |
*/ |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
277 |
private static void checkException(FileChannel fc, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
278 |
MapMode mode, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
279 |
long position, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
280 |
long size, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
281 |
Class<?>... expected) |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
282 |
throws IOException |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
283 |
{ |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
284 |
Exception exc = null; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
285 |
try { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
286 |
fc.map(mode, position, size); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
287 |
} catch (Exception actual) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
288 |
exc = actual; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
289 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
290 |
if (exc != null) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
291 |
for (Class<?> clazz: expected) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
292 |
if (clazz.isInstance(exc)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
293 |
return; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
294 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
295 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
296 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
297 |
System.err.println("Expected one of"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
298 |
for (Class<?> clazz: expected) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
299 |
System.out.println(clazz); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
300 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
301 |
if (exc == null) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
302 |
throw new RuntimeException("No expection thrown"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
303 |
} else { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
304 |
throw new RuntimeException("Unexpected exception thrown", exc); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
305 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
306 |
} |
2 | 307 |
} |