Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
94298cd5
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
94298cd5
编写于
3月 11, 2008
作者:
A
alanb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6448457: (ch) Channels.newOutputStream().write() does not write all data
Reviewed-by: iris, sherman
上级
3d58d163
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
107 addition
and
5 deletion
+107
-5
src/share/classes/java/nio/channels/Channels.java
src/share/classes/java/nio/channels/Channels.java
+24
-5
test/java/nio/channels/Channels/ShortWrite.java
test/java/nio/channels/Channels/ShortWrite.java
+83
-0
未找到文件。
src/share/classes/java/nio/channels/Channels.java
浏览文件 @
94298cd5
...
...
@@ -66,7 +66,27 @@ public final class Channels {
private
Channels
()
{
}
// No instantiation
private
static
int
write
(
WritableByteChannel
ch
,
ByteBuffer
bb
)
/**
* Write all remaining bytes in buffer to the given channel.
* If the channel is selectable then it must be configured blocking.
*/
private
static
void
writeFullyImpl
(
WritableByteChannel
ch
,
ByteBuffer
bb
)
throws
IOException
{
while
(
bb
.
remaining
()
>
0
)
{
int
n
=
ch
.
write
(
bb
);
if
(
n
<=
0
)
throw
new
RuntimeException
(
"no bytes written"
);
}
}
/**
* Write all remaining bytes in buffer to the given channel.
*
* @throws IllegalBlockingException
* If the channel is selectable and configured non-blocking.
*/
private
static
void
writeFully
(
WritableByteChannel
ch
,
ByteBuffer
bb
)
throws
IOException
{
if
(
ch
instanceof
SelectableChannel
)
{
...
...
@@ -74,14 +94,13 @@ public final class Channels {
synchronized
(
sc
.
blockingLock
())
{
if
(!
sc
.
isBlocking
())
throw
new
IllegalBlockingModeException
();
return
ch
.
write
(
bb
);
writeFullyImpl
(
ch
,
bb
);
}
}
else
{
return
ch
.
write
(
bb
);
writeFullyImpl
(
ch
,
bb
);
}
}
// -- Byte streams from channels --
/**
...
...
@@ -148,7 +167,7 @@ public final class Channels {
bb
.
position
(
off
);
this
.
bb
=
bb
;
this
.
bs
=
bs
;
Channels
.
write
(
ch
,
bb
);
Channels
.
write
Fully
(
ch
,
bb
);
}
public
void
close
()
throws
IOException
{
...
...
test/java/nio/channels/Channels/ShortWrite.java
0 → 100644
浏览文件 @
94298cd5
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
* @bug 6448457
* @summary Test Channels.newOutputStream returns OutputStream that handles
* short writes from the underlying channel
*/
import
java.io.OutputStream
;
import
java.io.IOException
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.*
;
import
java.util.Random
;
public
class
ShortWrite
{
static
Random
rand
=
new
Random
();
static
int
bytesWritten
=
0
;
public
static
void
main
(
String
[]
args
)
throws
IOException
{
WritableByteChannel
wbc
=
new
WritableByteChannel
()
{
public
int
write
(
ByteBuffer
src
)
{
int
rem
=
src
.
remaining
();
if
(
rem
>
0
)
{
// short write
int
n
=
rand
.
nextInt
(
rem
)
+
1
;
src
.
position
(
src
.
position
()
+
n
);
bytesWritten
+=
n
;
return
n
;
}
else
{
return
0
;
}
}
public
void
close
()
throws
IOException
{
throw
new
RuntimeException
(
"not implemented"
);
}
public
boolean
isOpen
()
{
throw
new
RuntimeException
(
"not implemented"
);
}
};
// wrap Channel with OutputStream
OutputStream
out
=
Channels
.
newOutputStream
(
wbc
);
// write 100, 99, 98, ... 1
// and check that the expected number of bytes is written
int
expected
=
0
;
byte
[]
buf
=
new
byte
[
100
];
for
(
int
i
=
0
;
i
<
buf
.
length
;
i
++)
{
int
len
=
buf
.
length
-
i
;
out
.
write
(
buf
,
i
,
len
);
expected
+=
len
;
}
System
.
out
.
format
(
"Bytes written: %d, expected: %d\n"
,
bytesWritten
,
expected
);
if
(
bytesWritten
!=
expected
)
throw
new
RuntimeException
(
"incorrect number of bytes written"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录