author | alanb |
Fri, 14 Sep 2018 16:56:09 +0100 | |
changeset 51745 | 90c1dcdebc64 |
parent 48252 | 77b88d8f8380 |
child 54569 | 7689e1cc56fe |
permissions | -rw-r--r-- |
2 | 1 |
/* |
47494
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
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 |
|
3955
a585e5ca5e4a
6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents:
1639
diff
changeset
|
25 |
* @bug 4417152 4481572 6248930 6725399 6884800 |
2 | 26 |
* @summary Test Channels basic functionality |
27 |
*/ |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.nio.*; |
|
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
31 |
import java.nio.charset.*; |
2 | 32 |
import java.nio.channels.*; |
33 |
||
34 |
||
35 |
public class Basic { |
|
36 |
||
37 |
static String message; |
|
38 |
||
39 |
static String encoding; |
|
40 |
||
41 |
static File blah; |
|
42 |
||
43 |
static int ITERATIONS = 500; |
|
44 |
||
45 |
public static void main(String[] args) throws Exception { |
|
46 |
message = "ascii data for a test"; |
|
47 |
encoding = "ISO-8859-1"; |
|
48 |
test(); |
|
49 |
message = "\ucafe\ubabe\ucafe\ubabe\ucafe\ubabe"; |
|
50 |
encoding = "UTF-8"; |
|
51 |
test(); |
|
52 |
} |
|
53 |
||
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
54 |
static void failNpeExpected() { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
55 |
throw new RuntimeException("Did not get the expected NullPointerException."); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
56 |
} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
57 |
|
2 | 58 |
private static void test() throws Exception { |
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
59 |
//Test if methods of Channels throw NPE with null argument(s) |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
60 |
try { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
61 |
Channels.newInputStream((ReadableByteChannel)null); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
62 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
63 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
64 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
65 |
try { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
66 |
Channels.newOutputStream((WritableByteChannel)null); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
67 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
68 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
69 |
|
2 | 70 |
try { |
71 |
ReadableByteChannel channel = Channels.newChannel((InputStream)null); |
|
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
72 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
73 |
} catch (NullPointerException ne) {} // OK. As expected. |
2 | 74 |
|
75 |
try { |
|
76 |
WritableByteChannel channel = Channels.newChannel((OutputStream)null); |
|
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
77 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
78 |
} catch (NullPointerException ne) {} // OK. As expected. |
2 | 79 |
|
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
80 |
WritableByteChannel wbc = new WritableByteChannel() { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
81 |
public int write(ByteBuffer src) { return 0; } |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
82 |
public void close() throws IOException { } |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
83 |
public boolean isOpen() { return true; } |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
84 |
}; |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
85 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
86 |
ReadableByteChannel rbc = new ReadableByteChannel() { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
87 |
public int read(ByteBuffer dst) { return 0; } |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
88 |
public void close() {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
89 |
public boolean isOpen() { return true; } |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
90 |
}; |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
91 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
92 |
try { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
93 |
Channels.newReader((ReadableByteChannel)null, |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
94 |
Charset.defaultCharset().newDecoder(), |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
95 |
-1); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
96 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
97 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
98 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
99 |
try { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
100 |
Channels.newReader(rbc, (CharsetDecoder)null, -1); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
101 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
102 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
103 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
104 |
try { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
105 |
Channels.newReader((ReadableByteChannel)null, |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
106 |
Charset.defaultCharset().name()); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
107 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
108 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
109 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
110 |
try { |
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
111 |
Channels.newReader(rbc, (String)null); |
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
112 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
113 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
114 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
115 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
116 |
try { |
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
117 |
Channels.newReader(null, (String)null); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
118 |
failNpeExpected(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
119 |
} catch (NullPointerException npe) {} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
120 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
121 |
try { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
122 |
Channels.newReader(rbc, (Charset)null); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
123 |
failNpeExpected(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
124 |
} catch (NullPointerException npe) {} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
125 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
126 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
127 |
try { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
128 |
Channels.newReader(null, (Charset)null); |
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
129 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
130 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
131 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
132 |
try { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
133 |
Channels.newWriter((WritableByteChannel)null, |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
134 |
Charset.defaultCharset().newEncoder(), |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
135 |
-1); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
136 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
137 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
138 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
139 |
try { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
140 |
Channels.newWriter(null, null, -1); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
141 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
142 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
143 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
144 |
try { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
145 |
Channels.newWriter(wbc, null, -1); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
146 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
147 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
148 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
149 |
try { |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
150 |
Channels.newWriter((WritableByteChannel)null, |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
151 |
Charset.defaultCharset().name()); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
152 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
153 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
154 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
155 |
try { |
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
156 |
Channels.newWriter(wbc, (String)null); |
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
157 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
158 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
159 |
|
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
160 |
try { |
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
161 |
Channels.newWriter(null, (String)null); |
1637
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
162 |
failNpeExpected(); |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
163 |
} catch (NullPointerException npe) {} |
bfa39b25f0fc
6725399: (ch) Channels.newInputStream should check for null
sherman
parents:
2
diff
changeset
|
164 |
|
48252
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
165 |
try { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
166 |
Channels.newWriter(wbc, (Charset)null); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
167 |
failNpeExpected(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
168 |
} catch (NullPointerException npe) {} |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
169 |
|
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
170 |
try { |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
171 |
Channels.newWriter(null, (Charset)null); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
172 |
failNpeExpected(); |
77b88d8f8380
8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
47494
diff
changeset
|
173 |
} catch (NullPointerException npe) {} |
2 | 174 |
|
175 |
try { |
|
176 |
blah = File.createTempFile("blah", null); |
|
177 |
||
178 |
testNewOutputStream(blah); |
|
179 |
readAndCheck(blah); |
|
180 |
blah.delete(); |
|
181 |
||
182 |
writeOut(blah, ITERATIONS); |
|
183 |
testNewInputStream(blah); |
|
184 |
blah.delete(); |
|
185 |
||
186 |
testNewChannelOut(blah); |
|
187 |
readAndCheck(blah); |
|
188 |
blah.delete(); |
|
189 |
||
47494
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
190 |
testNewChannelWriteAfterClose(blah); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
191 |
|
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
192 |
testNewChannelReadAfterClose(blah); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
193 |
blah.delete(); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
194 |
|
2 | 195 |
writeOut(blah, ITERATIONS); |
196 |
testNewChannelIn(blah); |
|
197 |
test4481572(blah); |
|
198 |
blah.delete(); |
|
199 |
||
200 |
testNewWriter(blah); |
|
201 |
readAndCheck(blah); |
|
202 |
blah.delete(); |
|
203 |
||
204 |
writeOut(blah, ITERATIONS); |
|
205 |
testNewReader(blah); |
|
206 |
||
207 |
} finally { |
|
208 |
blah.delete(); |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
private static void readAndCheck(File blah) throws Exception { |
|
213 |
FileInputStream fis = new FileInputStream(blah); |
|
214 |
int messageSize = message.length() * ITERATIONS * 3 + 1; |
|
215 |
byte bb[] = new byte[messageSize]; |
|
216 |
int bytesRead = 0; |
|
217 |
int totalRead = 0; |
|
218 |
while (bytesRead != -1) { |
|
219 |
totalRead += bytesRead; |
|
220 |
bytesRead = fis.read(bb, totalRead, messageSize - totalRead); |
|
221 |
} |
|
222 |
String result = new String(bb, 0, totalRead, encoding); |
|
223 |
int len = message.length(); |
|
224 |
for (int i=0; i<ITERATIONS; i++) { |
|
225 |
String segment = result.substring(i++ * len, i * len); |
|
226 |
if (!segment.equals(message)) |
|
227 |
throw new RuntimeException("Test failed"); |
|
228 |
} |
|
229 |
fis.close(); |
|
230 |
} |
|
231 |
||
232 |
private static void writeOut(File blah, int limit) throws Exception { |
|
233 |
FileOutputStream fos = new FileOutputStream(blah); |
|
234 |
for (int i=0; i<limit; i++) |
|
235 |
fos.write(message.getBytes(encoding)); |
|
236 |
fos.close(); |
|
237 |
} |
|
238 |
||
239 |
private static void testNewOutputStream(File blah) throws Exception { |
|
240 |
FileOutputStream fos = new FileOutputStream(blah); |
|
241 |
FileChannel fc = fos.getChannel(); |
|
242 |
WritableByteChannel wbc = (WritableByteChannel)fc; |
|
243 |
OutputStream os = Channels.newOutputStream(wbc); |
|
244 |
for (int i=0; i<ITERATIONS; i++) |
|
245 |
os.write(message.getBytes(encoding)); |
|
246 |
os.close(); |
|
247 |
fos.close(); |
|
248 |
} |
|
249 |
||
250 |
private static void testNewInputStream(File blah) throws Exception { |
|
251 |
FileInputStream fis = new FileInputStream(blah); |
|
252 |
FileChannel fc = fis.getChannel(); |
|
3955
a585e5ca5e4a
6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents:
1639
diff
changeset
|
253 |
InputStream is = Channels.newInputStream(fc); |
2 | 254 |
int messageSize = message.length() * ITERATIONS * 3 + 1; |
255 |
byte bb[] = new byte[messageSize]; |
|
256 |
||
257 |
int bytesRead = 0; |
|
258 |
int totalRead = 0; |
|
259 |
while (bytesRead != -1) { |
|
260 |
totalRead += bytesRead; |
|
3955
a585e5ca5e4a
6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents:
1639
diff
changeset
|
261 |
long rem = Math.min(fc.size() - totalRead, (long)Integer.MAX_VALUE); |
a585e5ca5e4a
6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents:
1639
diff
changeset
|
262 |
if (is.available() != (int)rem) |
a585e5ca5e4a
6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents:
1639
diff
changeset
|
263 |
throw new RuntimeException("available not useful or not maximally useful"); |
2 | 264 |
bytesRead = is.read(bb, totalRead, messageSize - totalRead); |
265 |
} |
|
3955
a585e5ca5e4a
6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents:
1639
diff
changeset
|
266 |
if (is.available() != 0) |
a585e5ca5e4a
6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents:
1639
diff
changeset
|
267 |
throw new RuntimeException("available() should return 0 at EOF"); |
2 | 268 |
|
269 |
String result = new String(bb, 0, totalRead, encoding); |
|
270 |
int len = message.length(); |
|
271 |
for (int i=0; i<ITERATIONS; i++) { |
|
272 |
String segment = result.substring(i++ * len, i * len); |
|
273 |
if (!segment.equals(message)) |
|
274 |
throw new RuntimeException("Test failed"); |
|
275 |
} |
|
276 |
is.close(); |
|
277 |
fis.close(); |
|
278 |
} |
|
279 |
||
280 |
private static void testNewChannelOut(File blah) throws Exception { |
|
281 |
ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah); |
|
282 |
WritableByteChannel wbc = Channels.newChannel(fos); |
|
47494
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
283 |
|
2 | 284 |
for (int i=0; i<ITERATIONS; i++) |
285 |
wbc.write(ByteBuffer.wrap(message.getBytes(encoding))); |
|
286 |
wbc.close(); |
|
287 |
fos.close(); |
|
288 |
} |
|
289 |
||
290 |
private static void testNewChannelIn(File blah) throws Exception { |
|
291 |
ExtendedFileInputStream fis = new ExtendedFileInputStream(blah); |
|
292 |
ReadableByteChannel rbc = Channels.newChannel(fis); |
|
293 |
||
294 |
int messageSize = message.length() * ITERATIONS * 3; |
|
295 |
byte data[] = new byte[messageSize+1]; |
|
296 |
ByteBuffer bb = ByteBuffer.wrap(data); |
|
297 |
||
298 |
int bytesRead = 0; |
|
299 |
int totalRead = 0; |
|
300 |
while (bytesRead != -1) { |
|
301 |
totalRead += bytesRead; |
|
302 |
bytesRead = rbc.read(bb); |
|
303 |
} |
|
304 |
||
305 |
String result = new String(data, 0, totalRead, encoding); |
|
306 |
int len = message.length(); |
|
307 |
for (int i=0; i<ITERATIONS; i++) { |
|
308 |
String segment = result.substring(i++ * len, i * len); |
|
309 |
if (!segment.equals(message)) |
|
310 |
throw new RuntimeException("Test failed"); |
|
311 |
} |
|
312 |
rbc.close(); |
|
313 |
fis.close(); |
|
314 |
} |
|
315 |
||
47494
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
316 |
private static void testNewChannelWriteAfterClose(File blah) |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
317 |
throws Exception { |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
318 |
try (ExtendedFileOutputStream fos = |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
319 |
new ExtendedFileOutputStream(blah)) { |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
320 |
WritableByteChannel wbc = Channels.newChannel(fos); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
321 |
|
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
322 |
wbc.close(); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
323 |
try { |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
324 |
wbc.write(ByteBuffer.allocate(0)); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
325 |
throw new RuntimeException |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
326 |
("No ClosedChannelException on WritableByteChannel::write"); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
327 |
} catch (ClosedChannelException expected) { |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
328 |
} |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
329 |
} |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
330 |
} |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
331 |
|
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
332 |
private static void testNewChannelReadAfterClose(File blah) |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
333 |
throws Exception { |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
334 |
try (ExtendedFileInputStream fis = new ExtendedFileInputStream(blah)) { |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
335 |
ReadableByteChannel rbc = Channels.newChannel(fis); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
336 |
|
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
337 |
rbc.close(); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
338 |
try { |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
339 |
rbc.read(ByteBuffer.allocate(0)); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
340 |
throw new RuntimeException |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
341 |
("No ClosedChannelException on ReadableByteChannel::read"); |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
342 |
} catch (ClosedChannelException expected) { |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
343 |
} |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
344 |
} |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
345 |
} |
24e43fd1ad69
8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents:
47216
diff
changeset
|
346 |
|
2 | 347 |
// Causes BufferOverflowException if bug 4481572 is present. |
348 |
private static void test4481572(File blah) throws Exception { |
|
349 |
ExtendedFileInputStream fis = new ExtendedFileInputStream(blah); |
|
350 |
ReadableByteChannel rbc = Channels.newChannel(fis); |
|
351 |
||
352 |
byte data[] = new byte[9000]; |
|
353 |
ByteBuffer bb = ByteBuffer.wrap(data); |
|
354 |
||
355 |
int bytesRead = 1; |
|
356 |
int totalRead = 0; |
|
357 |
while (bytesRead > 0) { |
|
358 |
totalRead += bytesRead; |
|
359 |
bytesRead = rbc.read(bb); |
|
360 |
} |
|
361 |
rbc.close(); |
|
362 |
fis.close(); |
|
363 |
} |
|
364 |
||
365 |
private static void testNewWriter(File blah) throws Exception { |
|
366 |
FileOutputStream fos = new FileOutputStream(blah); |
|
367 |
WritableByteChannel wbc = (WritableByteChannel)fos.getChannel(); |
|
368 |
Writer w = Channels.newWriter(wbc, encoding); |
|
369 |
char data[] = new char[40]; |
|
370 |
message.getChars(0, message.length(), data, 0); |
|
371 |
for (int i=0; i<ITERATIONS; i++) |
|
372 |
w.write(data, 0, message.length()); |
|
373 |
w.flush(); |
|
374 |
w.close(); |
|
375 |
fos.close(); |
|
376 |
} |
|
377 |
||
378 |
private static void testNewReader(File blah) throws Exception { |
|
379 |
FileInputStream fis = new FileInputStream(blah); |
|
380 |
ReadableByteChannel rbc = (ReadableByteChannel)fis.getChannel(); |
|
381 |
Reader r = Channels.newReader(rbc, encoding); |
|
382 |
||
383 |
int messageSize = message.length() * ITERATIONS; |
|
384 |
char data[] = new char[messageSize]; |
|
385 |
||
386 |
int totalRead = 0; |
|
387 |
int charsRead = 0; |
|
388 |
while (totalRead < messageSize) { |
|
389 |
totalRead += charsRead; |
|
390 |
charsRead = r.read(data, totalRead, messageSize - totalRead); |
|
391 |
} |
|
392 |
String result = new String(data, 0, totalRead); |
|
393 |
int len = message.length(); |
|
394 |
for (int i=0; i<ITERATIONS; i++) { |
|
395 |
String segment = result.substring(i++ * len, i * len); |
|
396 |
if (!segment.equals(message)) |
|
397 |
throw new RuntimeException("Test failed"); |
|
398 |
} |
|
399 |
r.close(); |
|
400 |
fis.close(); |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
class ExtendedFileInputStream extends java.io.FileInputStream { |
|
405 |
ExtendedFileInputStream(File file) throws FileNotFoundException { |
|
406 |
super(file); |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
class ExtendedFileOutputStream extends java.io.FileOutputStream { |
|
411 |
ExtendedFileOutputStream(File file) throws FileNotFoundException { |
|
412 |
super(file); |
|
413 |
} |
|
414 |
} |