author | alanb |
Fri, 14 Sep 2018 16:56:09 +0100 | |
changeset 51745 | 90c1dcdebc64 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
66
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
1 |
/* |
5506 | 2 |
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. |
66
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
4 |
* |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
8 |
* |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
14 |
* |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
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. |
|
66
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
22 |
*/ |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
23 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
24 |
/* @test |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
25 |
* @bug 6448457 |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
26 |
* @summary Test Channels.newOutputStream returns OutputStream that handles |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
27 |
* short writes from the underlying channel |
30046 | 28 |
* @key randomness |
66
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
29 |
*/ |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
30 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
31 |
import java.io.OutputStream; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
32 |
import java.io.IOException; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
33 |
import java.nio.ByteBuffer; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
34 |
import java.nio.channels.*; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
35 |
import java.util.Random; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
36 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
37 |
public class ShortWrite { |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
38 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
39 |
static Random rand = new Random(); |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
40 |
static int bytesWritten = 0; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
41 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
42 |
public static void main(String[] args) throws IOException { |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
43 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
44 |
WritableByteChannel wbc = new WritableByteChannel() { |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
45 |
public int write(ByteBuffer src) { |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
46 |
int rem = src.remaining(); |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
47 |
if (rem > 0) { |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
48 |
// short write |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
49 |
int n = rand.nextInt(rem) + 1; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
50 |
src.position(src.position() + n); |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
51 |
bytesWritten += n; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
52 |
return n; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
53 |
} else { |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
54 |
return 0; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
55 |
} |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
56 |
} |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
57 |
public void close() throws IOException { |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
58 |
throw new RuntimeException("not implemented"); |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
59 |
} |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
60 |
public boolean isOpen() { |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
61 |
throw new RuntimeException("not implemented"); |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
62 |
} |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
63 |
}; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
64 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
65 |
// wrap Channel with OutputStream |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
66 |
OutputStream out = Channels.newOutputStream(wbc); |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
67 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
68 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
69 |
// write 100, 99, 98, ... 1 |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
70 |
// and check that the expected number of bytes is written |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
71 |
int expected = 0; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
72 |
byte[] buf = new byte[100]; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
73 |
for (int i=0; i<buf.length; i++) { |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
74 |
int len = buf.length-i; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
75 |
out.write(buf, i, len); |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
76 |
expected += len; |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
77 |
} |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
78 |
System.out.format("Bytes written: %d, expected: %d\n", bytesWritten, |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
79 |
expected); |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
80 |
if (bytesWritten != expected) |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
81 |
throw new RuntimeException("incorrect number of bytes written"); |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
82 |
|
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
83 |
} |
eac97080ce1e
6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents:
diff
changeset
|
84 |
} |