author | jbhateja |
Tue, 26 Nov 2019 16:09:25 +0300 | |
changeset 59278 | 8375560db76b |
parent 55548 | 1ba68bf27e27 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54823
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, 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 |
55548
1ba68bf27e27
8224480: (fc) java/nio/channels/FileChannel/MapTest.java fails intermittently
bpb
parents:
54823
diff
changeset
|
27 |
* @run main/othervm/timeout=240 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"); |
54823
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
70 |
testForce(); |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
71 |
out.println("Force: OK"); |
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
72 |
testExceptions(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
73 |
out.println("Exceptions: OK"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
74 |
} finally { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
75 |
blah.delete(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
76 |
} |
2 | 77 |
} |
78 |
||
79 |
/** |
|
80 |
* Creates file blah: |
|
81 |
* 0000 |
|
82 |
* 0001 |
|
83 |
* 0002 |
|
84 |
* 0003 |
|
85 |
* . |
|
86 |
* . |
|
87 |
* . |
|
88 |
* 3999 |
|
89 |
* |
|
90 |
* Blah extends beyond a single page of memory so that the |
|
91 |
* ability to index into a file of multiple pages is tested. |
|
92 |
*/ |
|
93 |
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
|
94 |
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
|
95 |
for (int i=0; i<4000; i++) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
96 |
String number = new Integer(i).toString(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
97 |
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
|
98 |
writer.write("0"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
99 |
writer.write(""+i); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
100 |
writer.newLine(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
101 |
} |
2 | 102 |
} |
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Tests zero size file mapping |
|
107 |
*/ |
|
108 |
private static void testZero() throws Exception { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
109 |
try (FileInputStream fis = new FileInputStream(blah)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
110 |
FileChannel fc = fis.getChannel(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
111 |
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
|
112 |
} |
2 | 113 |
} |
114 |
||
115 |
/** |
|
116 |
* Maps blah file with a random offset and checks to see if read |
|
117 |
* from the ByteBuffer gets the right line number |
|
118 |
*/ |
|
119 |
private static void testRead() throws Exception { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
120 |
StringBuilder sb = new StringBuilder(); |
2 | 121 |
sb.setLength(4); |
122 |
||
123 |
for (int x=0; x<1000; x++) { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
124 |
try (FileInputStream fis = new FileInputStream(blah)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
125 |
FileChannel fc = fis.getChannel(); |
2 | 126 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
127 |
long offset = generator.nextInt(10000); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
128 |
long expectedResult = offset / CHARS_PER_LINE; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
129 |
offset = expectedResult * CHARS_PER_LINE; |
2 | 130 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
131 |
MappedByteBuffer b = fc.map(MapMode.READ_ONLY, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
132 |
offset, 100); |
2 | 133 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
134 |
for (int i=0; i<4; i++) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
135 |
byte aByte = b.get(i); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
136 |
sb.setCharAt(i, (char)aByte); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
137 |
} |
2 | 138 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
139 |
int result = Integer.parseInt(sb.toString()); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
140 |
if (result != expectedResult) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
141 |
err.println("I expected "+expectedResult); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
142 |
err.println("I got "+result); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
143 |
throw new Exception("Read test failed"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
144 |
} |
2 | 145 |
} |
146 |
} |
|
147 |
} |
|
148 |
||
149 |
/** |
|
150 |
* Maps blah file with a random offset and checks to see if data |
|
151 |
* written out to the file can be read back in |
|
152 |
*/ |
|
153 |
private static void testWrite() throws Exception { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
154 |
StringBuilder sb = new StringBuilder(); |
2 | 155 |
sb.setLength(4); |
156 |
||
157 |
for (int x=0; x<1000; x++) { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
158 |
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
159 |
FileChannel fc = raf.getChannel(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
160 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
161 |
long offset = generator.nextInt(1000); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
162 |
MappedByteBuffer b = fc.map(MapMode.READ_WRITE, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
163 |
offset, 100); |
2 | 164 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
165 |
for (int i=0; i<4; i++) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
166 |
b.put(i, (byte)('0' + i)); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
167 |
} |
2 | 168 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
169 |
for (int i=0; i<4; i++) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
170 |
byte aByte = b.get(i); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
171 |
sb.setCharAt(i, (char)aByte); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
172 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
173 |
if (!sb.toString().equals("0123")) |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
174 |
throw new Exception("Write test failed"); |
2 | 175 |
} |
176 |
} |
|
177 |
} |
|
178 |
||
179 |
private static void testHighOffset() throws Exception { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
180 |
StringBuilder sb = new StringBuilder(); |
2 | 181 |
sb.setLength(4); |
182 |
||
183 |
for (int x=0; x<1000; x++) { |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
184 |
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
185 |
FileChannel fc = raf.getChannel(); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
186 |
long offset = 66000; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
187 |
MappedByteBuffer b = fc.map(MapMode.READ_WRITE, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
188 |
offset, 100); |
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 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
193 |
/** |
54823
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
194 |
* Maps blah file, writes some data and forcing writeback of |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
195 |
* the data exercising various valid and invalid writeback ranges. |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
196 |
*/ |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
197 |
private static void testForce() throws Exception { |
55548
1ba68bf27e27
8224480: (fc) java/nio/channels/FileChannel/MapTest.java fails intermittently
bpb
parents:
54823
diff
changeset
|
198 |
for (int x=0; x<50; x++) { |
54823
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
199 |
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
200 |
FileChannel fc = raf.getChannel(); |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
201 |
final int BLOCK_SIZE = 64; |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
202 |
final int BLOCK_COUNT = (4096 * 2)/ BLOCK_SIZE; |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
203 |
int offset = 0; |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
204 |
MappedByteBuffer b = fc.map(MapMode.READ_WRITE, |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
205 |
0, BLOCK_SIZE * (BLOCK_COUNT + 1)); |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
206 |
|
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
207 |
for (int blocks = 0; blocks < BLOCK_COUNT; blocks++) { |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
208 |
for (int i = 0; i < BLOCK_SIZE; i++) { |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
209 |
b.put(offset + i, (byte)('0' + i)); |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
210 |
} |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
211 |
b.force(offset, BLOCK_SIZE); |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
212 |
offset += BLOCK_SIZE; |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
213 |
} |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
214 |
|
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
215 |
Exception exc = null; |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
216 |
try { |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
217 |
// start and end are out of range |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
218 |
b.force(offset + BLOCK_SIZE, BLOCK_SIZE); |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
219 |
} catch (IndexOutOfBoundsException e) { |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
220 |
exc = e; |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
221 |
} |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
222 |
if (exc == null) { |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
223 |
throw new RuntimeException("expected Exception for force beyond buffer extent"); |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
224 |
} |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
225 |
|
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
226 |
exc = null; |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
227 |
try { |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
228 |
// start is in range but end is out of range |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
229 |
b.force(offset, 2 * BLOCK_SIZE); |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
230 |
} catch (IndexOutOfBoundsException e) { |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
231 |
exc = e; |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
232 |
} |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
233 |
if (exc == null) { |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
234 |
throw new RuntimeException("expected Exception for force beyond write limit"); |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
235 |
} |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
236 |
} |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
237 |
} |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
238 |
} |
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
239 |
|
1b940da275d2
8221696: MappedByteBuffer.force method to specify range
adinn
parents:
47216
diff
changeset
|
240 |
/** |
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
241 |
* Test exceptions specified by map method |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
242 |
*/ |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
243 |
private static void testExceptions() throws Exception { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
244 |
// check exceptions when channel opened for read access |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
245 |
try (FileChannel fc = FileChannel.open(blah.toPath(), READ)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
246 |
testExceptions(fc); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
247 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
248 |
checkException(fc, MapMode.READ_WRITE, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
249 |
NonWritableChannelException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
250 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
251 |
checkException(fc, MapMode.READ_WRITE, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
252 |
NonWritableChannelException.class, IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
253 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
254 |
checkException(fc, MapMode.READ_WRITE, 0L, -1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
255 |
NonWritableChannelException.class, IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
256 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
257 |
checkException(fc, MapMode.PRIVATE, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
258 |
NonWritableChannelException.class); |
2 | 259 |
|
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
260 |
checkException(fc, MapMode.PRIVATE, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
261 |
NonWritableChannelException.class, IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
262 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
263 |
checkException(fc, MapMode.PRIVATE, 0L, -1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
264 |
NonWritableChannelException.class, IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
265 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
266 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
267 |
// check exceptions when channel opened for write access |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
268 |
try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
269 |
testExceptions(fc); |
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 |
checkException(fc, MapMode.READ_ONLY, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
272 |
NonReadableChannelException.class); |
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 |
checkException(fc, MapMode.READ_ONLY, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
275 |
NonReadableChannelException.class, IllegalArgumentException.class); |
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 |
/* |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
278 |
* implementation/spec mismatch, these tests disabled for now |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
279 |
*/ |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
280 |
//checkException(fc, MapMode.READ_WRITE, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
281 |
// NonWritableChannelException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
282 |
//checkException(fc, MapMode.PRIVATE, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
283 |
// NonWritableChannelException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
284 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
285 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
286 |
// 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
|
287 |
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
|
288 |
testExceptions(fc); |
2 | 289 |
} |
290 |
} |
|
291 |
||
14401
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
292 |
private static void testExceptions(FileChannel fc) throws IOException { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
293 |
checkException(fc, null, 0L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
294 |
NullPointerException.class); |
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 |
checkException(fc, MapMode.READ_ONLY, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
297 |
IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
298 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
299 |
checkException(fc, null, -1L, fc.size(), |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
300 |
IllegalArgumentException.class, NullPointerException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
301 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
302 |
checkException(fc, MapMode.READ_ONLY, 0L, -1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
303 |
IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
304 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
305 |
checkException(fc, null, 0L, -1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
306 |
IllegalArgumentException.class, NullPointerException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
307 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
308 |
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
|
309 |
IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
310 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
311 |
checkException(fc, null, 0L, Integer.MAX_VALUE + 1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
312 |
IllegalArgumentException.class, NullPointerException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
313 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
314 |
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
|
315 |
IllegalArgumentException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
316 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
317 |
checkException(fc, null, Long.MAX_VALUE, 1L, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
318 |
IllegalArgumentException.class, NullPointerException.class); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
319 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
320 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
321 |
|
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
322 |
/** |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
323 |
* 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
|
324 |
* when invoked with the given inputs. |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
325 |
*/ |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
326 |
private static void checkException(FileChannel fc, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
327 |
MapMode mode, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
328 |
long position, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
329 |
long size, |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
330 |
Class<?>... expected) |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
331 |
throws IOException |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
332 |
{ |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
333 |
Exception exc = null; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
334 |
try { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
335 |
fc.map(mode, position, size); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
336 |
} catch (Exception actual) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
337 |
exc = actual; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
338 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
339 |
if (exc != null) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
340 |
for (Class<?> clazz: expected) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
341 |
if (clazz.isInstance(exc)) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
342 |
return; |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
343 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
344 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
345 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
346 |
System.err.println("Expected one of"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
347 |
for (Class<?> clazz: expected) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
348 |
System.out.println(clazz); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
349 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
350 |
if (exc == null) { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
351 |
throw new RuntimeException("No expection thrown"); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
352 |
} else { |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
353 |
throw new RuntimeException("Unexpected exception thrown", exc); |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
354 |
} |
1f9a8607f4df
8000330: (fc) FileChannel.truncate issues when given size > file size
alanb
parents:
7668
diff
changeset
|
355 |
} |
2 | 356 |
} |