author | bpb |
Mon, 30 Apr 2018 13:40:39 -0700 | |
changeset 49930 | 3aaaa5370999 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
2 |
* Copyright (c) 2001, 2017, 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 |
|
44273
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
44266
diff
changeset
|
25 |
* @bug 4434723 4482726 4559072 4795550 5081340 5103988 6984545 |
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
26 |
* @summary Test FileChannel.transferFrom and transferTo (use -Dseed=X to set PRNG seed) |
2 | 27 |
* @library .. |
45288
58be10a068c2
8180805: move RandomFactory to the top level testlibrary
iignatyev
parents:
44857
diff
changeset
|
28 |
* @library /test/lib |
45467
99c87a16a8e4
8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents:
45288
diff
changeset
|
29 |
* @build jdk.test.lib.RandomFactory |
44266
2b9cc0e5e79f
8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents:
44107
diff
changeset
|
30 |
* @run testng/timeout=300 Transfer |
30046 | 31 |
* @key randomness |
2 | 32 |
*/ |
33 |
||
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
34 |
import java.io.BufferedReader; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
35 |
import java.io.File; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
36 |
import java.io.FileInputStream; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
37 |
import java.io.FileOutputStream; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
38 |
import java.io.InputStreamReader; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
39 |
import java.io.IOException; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
40 |
import java.io.RandomAccessFile; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
41 |
import java.io.Reader; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
42 |
import java.net.InetAddress; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
43 |
import java.net.InetSocketAddress; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
44 |
import java.nio.ByteBuffer; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
45 |
import java.nio.channels.FileChannel; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
46 |
import java.nio.channels.NonReadableChannelException; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
47 |
import java.nio.channels.Pipe; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
48 |
import java.nio.channels.ServerSocketChannel; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
49 |
import java.nio.channels.SocketChannel; |
2 | 50 |
import java.nio.channels.spi.SelectorProvider; |
51 |
import java.util.Random; |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
52 |
import java.util.concurrent.TimeUnit; |
2 | 53 |
|
45288
58be10a068c2
8180805: move RandomFactory to the top level testlibrary
iignatyev
parents:
44857
diff
changeset
|
54 |
import jdk.test.lib.RandomFactory; |
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
55 |
|
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
56 |
import org.testng.annotations.Test; |
2 | 57 |
|
58 |
public class Transfer { |
|
59 |
||
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
60 |
private static Random generator = RandomFactory.getRandom(); |
2 | 61 |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
62 |
@Test |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
63 |
public void testFileChannel() throws Exception { |
2 | 64 |
File source = File.createTempFile("source", null); |
65 |
source.deleteOnExit(); |
|
66 |
File sink = File.createTempFile("sink", null); |
|
67 |
sink.deleteOnExit(); |
|
68 |
||
69 |
FileOutputStream fos = new FileOutputStream(source); |
|
70 |
FileChannel sourceChannel = fos.getChannel(); |
|
71 |
sourceChannel.write(ByteBuffer.wrap( |
|
72 |
"Use the source, Luke!".getBytes())); |
|
73 |
sourceChannel.close(); |
|
74 |
||
75 |
FileInputStream fis = new FileInputStream(source); |
|
76 |
sourceChannel = fis.getChannel(); |
|
77 |
||
78 |
RandomAccessFile raf = new RandomAccessFile(sink, "rw"); |
|
79 |
FileChannel sinkChannel = raf.getChannel(); |
|
80 |
long oldSinkPosition = sinkChannel.position(); |
|
81 |
long oldSourcePosition = sourceChannel.position(); |
|
82 |
||
83 |
long bytesWritten = sinkChannel.transferFrom(sourceChannel, 0, 10); |
|
84 |
if (bytesWritten != 10) |
|
85 |
throw new RuntimeException("Transfer failed"); |
|
86 |
||
87 |
if (sourceChannel.position() == oldSourcePosition) |
|
88 |
throw new RuntimeException("Source position didn't change"); |
|
89 |
||
90 |
if (sinkChannel.position() != oldSinkPosition) |
|
91 |
throw new RuntimeException("Sink position changed"); |
|
92 |
||
93 |
if (sinkChannel.size() != 10) |
|
94 |
throw new RuntimeException("Unexpected sink size"); |
|
95 |
||
96 |
bytesWritten = sinkChannel.transferFrom(sourceChannel, 1000, 10); |
|
97 |
||
98 |
if (bytesWritten > 0) |
|
99 |
throw new RuntimeException("Wrote past file size"); |
|
100 |
||
101 |
sourceChannel.close(); |
|
102 |
sinkChannel.close(); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
103 |
|
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
104 |
source.delete(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
105 |
sink.delete(); |
2 | 106 |
} |
107 |
||
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
108 |
@Test |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
109 |
public void testReadableByteChannel() throws Exception { |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
110 |
int[] testSizes = { 0, 10, 1023, 1024, 1025, 2047, 2048, 2049 }; |
2 | 111 |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
112 |
for (int size : testSizes) { |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
113 |
SelectorProvider sp = SelectorProvider.provider(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
114 |
Pipe p = sp.openPipe(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
115 |
Pipe.SinkChannel sink = p.sink(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
116 |
Pipe.SourceChannel source = p.source(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
117 |
sink.configureBlocking(false); |
2 | 118 |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
119 |
ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
120 |
byte[] someBytes = new byte[size + 10]; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
121 |
generator.nextBytes(someBytes); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
122 |
outgoingdata.put(someBytes); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
123 |
outgoingdata.flip(); |
2 | 124 |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
125 |
int totalWritten = 0; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
126 |
while (totalWritten < size + 10) { |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
127 |
int written = sink.write(outgoingdata); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
128 |
if (written < 0) |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
129 |
throw new Exception("Write failed"); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
130 |
totalWritten += written; |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
131 |
} |
2 | 132 |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
133 |
File f = File.createTempFile("blah"+size, null); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
134 |
f.deleteOnExit(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
135 |
RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
136 |
FileChannel fc = raf.getChannel(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
137 |
long oldPosition = fc.position(); |
2 | 138 |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
139 |
long bytesWritten = fc.transferFrom(source, 0, size); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
140 |
fc.force(true); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
141 |
if (bytesWritten != size) |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
142 |
throw new RuntimeException("Transfer failed"); |
2 | 143 |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
144 |
if (fc.position() != oldPosition) |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
145 |
throw new RuntimeException("Position changed"); |
2 | 146 |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
147 |
if (fc.size() != size) |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
148 |
throw new RuntimeException("Unexpected sink size "+ fc.size()); |
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
149 |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
150 |
fc.close(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
151 |
sink.close(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
152 |
source.close(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
153 |
|
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
154 |
f.delete(); |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
155 |
} |
2 | 156 |
} |
157 |
||
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
158 |
@Test |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
159 |
public void xferTest02() throws Exception { // for bug 4482726 |
2 | 160 |
byte[] srcData = new byte[5000]; |
161 |
for (int i=0; i<5000; i++) |
|
162 |
srcData[i] = (byte)generator.nextInt(); |
|
163 |
||
164 |
// get filechannel for the source file. |
|
165 |
File source = File.createTempFile("source", null); |
|
166 |
source.deleteOnExit(); |
|
167 |
RandomAccessFile raf1 = new RandomAccessFile(source, "rw"); |
|
168 |
FileChannel fc1 = raf1.getChannel(); |
|
169 |
||
170 |
// write out data to the file channel |
|
171 |
long bytesWritten = 0; |
|
172 |
while (bytesWritten < 5000) { |
|
173 |
bytesWritten = fc1.write(ByteBuffer.wrap(srcData)); |
|
174 |
} |
|
175 |
||
176 |
// get filechannel for the dst file. |
|
177 |
File dest = File.createTempFile("dest", null); |
|
178 |
dest.deleteOnExit(); |
|
179 |
RandomAccessFile raf2 = new RandomAccessFile(dest, "rw"); |
|
180 |
FileChannel fc2 = raf2.getChannel(); |
|
181 |
||
182 |
int bytesToWrite = 3000; |
|
183 |
int startPosition = 1000; |
|
184 |
||
185 |
bytesWritten = fc1.transferTo(startPosition, bytesToWrite, fc2); |
|
186 |
||
187 |
fc1.close(); |
|
188 |
fc2.close(); |
|
189 |
raf1.close(); |
|
190 |
raf2.close(); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
191 |
|
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
192 |
source.delete(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
193 |
dest.delete(); |
2 | 194 |
} |
195 |
||
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
196 |
@Test |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
197 |
public void xferTest03() throws Exception { // for bug 4559072 |
2 | 198 |
byte[] srcData = new byte[] {1,2,3,4} ; |
199 |
||
200 |
// get filechannel for the source file. |
|
201 |
File source = File.createTempFile("source", null); |
|
202 |
source.deleteOnExit(); |
|
203 |
RandomAccessFile raf1 = new RandomAccessFile(source, "rw"); |
|
204 |
FileChannel fc1 = raf1.getChannel(); |
|
205 |
fc1.truncate(0); |
|
206 |
||
207 |
// write out data to the file channel |
|
208 |
int bytesWritten = 0; |
|
209 |
while (bytesWritten < 4) { |
|
210 |
bytesWritten = fc1.write(ByteBuffer.wrap(srcData)); |
|
211 |
} |
|
212 |
||
213 |
// get filechannel for the dst file. |
|
214 |
File dest = File.createTempFile("dest", null); |
|
215 |
dest.deleteOnExit(); |
|
216 |
RandomAccessFile raf2 = new RandomAccessFile(dest, "rw"); |
|
217 |
FileChannel fc2 = raf2.getChannel(); |
|
218 |
fc2.truncate(0); |
|
219 |
||
220 |
fc1.transferTo(0, srcData.length + 1, fc2); |
|
221 |
||
222 |
if (fc2.size() > 4) |
|
223 |
throw new Exception("xferTest03 failed"); |
|
224 |
||
225 |
fc1.close(); |
|
226 |
fc2.close(); |
|
227 |
raf1.close(); |
|
228 |
raf2.close(); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
229 |
|
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
230 |
source.delete(); |
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
231 |
dest.delete(); |
2 | 232 |
} |
233 |
||
44273
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
44266
diff
changeset
|
234 |
// xferTest04() and xferTest05() moved to Transfer4GBFile.java |
2 | 235 |
|
236 |
static void checkFileData(File file, String expected) throws Exception { |
|
237 |
FileInputStream fis = new FileInputStream(file); |
|
238 |
Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII")); |
|
239 |
StringBuilder sb = new StringBuilder(); |
|
240 |
int c; |
|
241 |
while ((c = r.read()) != -1) |
|
242 |
sb.append((char)c); |
|
243 |
String contents = sb.toString(); |
|
244 |
if (! contents.equals(expected)) |
|
245 |
throw new Exception("expected: " + expected |
|
246 |
+ ", got: " + contents); |
|
247 |
r.close(); |
|
248 |
} |
|
249 |
||
250 |
// Test transferFrom asking for more bytes than remain in source |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
251 |
@Test |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
252 |
public void xferTest06() throws Exception { // for bug 5081340 |
2 | 253 |
String data = "Use the source, Luke!"; |
254 |
||
255 |
File source = File.createTempFile("source", null); |
|
256 |
source.deleteOnExit(); |
|
257 |
File sink = File.createTempFile("sink", null); |
|
258 |
sink.deleteOnExit(); |
|
259 |
||
260 |
FileOutputStream fos = new FileOutputStream(source); |
|
261 |
fos.write(data.getBytes("ASCII")); |
|
262 |
fos.close(); |
|
263 |
||
264 |
FileChannel sourceChannel = |
|
265 |
new RandomAccessFile(source, "rw").getChannel(); |
|
266 |
sourceChannel.position(7); |
|
267 |
long remaining = sourceChannel.size() - sourceChannel.position(); |
|
268 |
FileChannel sinkChannel = |
|
269 |
new RandomAccessFile(sink, "rw").getChannel(); |
|
270 |
long n = sinkChannel.transferFrom(sourceChannel, 0L, |
|
271 |
sourceChannel.size()); // overflow |
|
272 |
if (n != remaining) |
|
273 |
throw new Exception("n == " + n + ", remaining == " + remaining); |
|
274 |
||
275 |
sinkChannel.close(); |
|
276 |
sourceChannel.close(); |
|
277 |
||
278 |
checkFileData(source, data); |
|
279 |
checkFileData(sink, data.substring(7,data.length())); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
280 |
|
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
281 |
source.delete(); |
2 | 282 |
} |
283 |
||
284 |
// Test transferTo to non-blocking socket channel |
|
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
285 |
@Test |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
286 |
public void xferTest07() throws Exception { // for bug 5103988 |
2 | 287 |
File source = File.createTempFile("source", null); |
288 |
source.deleteOnExit(); |
|
289 |
||
290 |
FileChannel sourceChannel = new RandomAccessFile(source, "rw") |
|
291 |
.getChannel(); |
|
292 |
sourceChannel.position(32000L) |
|
293 |
.write(ByteBuffer.wrap("The End".getBytes())); |
|
294 |
||
295 |
// The sink is a non-blocking socket channel |
|
296 |
ServerSocketChannel ssc = ServerSocketChannel.open(); |
|
297 |
ssc.socket().bind(new InetSocketAddress(0)); |
|
298 |
InetSocketAddress sa = new InetSocketAddress( |
|
299 |
InetAddress.getLocalHost(), ssc.socket().getLocalPort()); |
|
300 |
SocketChannel sink = SocketChannel.open(sa); |
|
301 |
sink.configureBlocking(false); |
|
302 |
SocketChannel other = ssc.accept(); |
|
303 |
||
304 |
long size = sourceChannel.size(); |
|
305 |
||
306 |
// keep sending until congested |
|
307 |
long n; |
|
308 |
do { |
|
309 |
n = sourceChannel.transferTo(0, size, sink); |
|
310 |
} while (n > 0); |
|
311 |
||
312 |
sourceChannel.close(); |
|
313 |
sink.close(); |
|
314 |
other.close(); |
|
315 |
ssc.close(); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
316 |
source.delete(); |
2 | 317 |
} |
318 |
||
44273
a2e3e08c5468
8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
44266
diff
changeset
|
319 |
// xferTest08() moved to TransferTo6GBFile.java |
2 | 320 |
|
6545
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
321 |
// Test that transferFrom with FileChannel source that is not readable |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
322 |
// throws NonReadableChannelException |
43708
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
323 |
@Test |
1f5975d73136
8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents:
30046
diff
changeset
|
324 |
public void xferTest09() throws Exception { // for bug 6984545 |
6545
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
325 |
File source = File.createTempFile("source", null); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
326 |
source.deleteOnExit(); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
327 |
|
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
328 |
File target = File.createTempFile("target", null); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
329 |
target.deleteOnExit(); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
330 |
|
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
331 |
FileChannel fc1 = new FileOutputStream(source).getChannel(); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
332 |
FileChannel fc2 = new RandomAccessFile(target, "rw").getChannel(); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
333 |
try { |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
334 |
fc2.transferFrom(fc1, 0L, 0); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
335 |
throw new RuntimeException("NonReadableChannelException expected"); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
336 |
} catch (NonReadableChannelException expected) { |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
337 |
} finally { |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
338 |
fc1.close(); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
339 |
fc2.close(); |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
340 |
} |
9d2efd6ddd0c
6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents:
5977
diff
changeset
|
341 |
} |
2 | 342 |
} |