Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
a92d95ac
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看板
提交
a92d95ac
编写于
5月 29, 2013
作者:
A
alanb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8014928: (fs) Files.readAllBytes() copies content to new array when content completely read
Reviewed-by: martin
上级
56fc22d6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
17 addition
and
39 deletion
+17
-39
src/share/classes/java/nio/file/Files.java
src/share/classes/java/nio/file/Files.java
+17
-39
未找到文件。
src/share/classes/java/nio/file/Files.java
浏览文件 @
a92d95ac
...
...
@@ -25,9 +25,11 @@
package
java.nio.file
;
import
java.nio.ByteBuffer
;
import
java.nio.file.attribute.*
;
import
java.nio.file.spi.FileSystemProvider
;
import
java.nio.file.spi.FileTypeDetector
;
import
java.nio.channels.FileChannel
;
import
java.nio.channels.SeekableByteChannel
;
import
java.io.Closeable
;
import
java.io.InputStream
;
...
...
@@ -2944,40 +2946,6 @@ public final class Files {
}
}
/**
* Read all the bytes from an input stream. The {@code initialSize}
* parameter indicates the initial size of the byte[] to allocate.
*/
private
static
byte
[]
read
(
InputStream
source
,
int
initialSize
)
throws
IOException
{
int
capacity
=
initialSize
;
byte
[]
buf
=
new
byte
[
capacity
];
int
nread
=
0
;
int
rem
=
buf
.
length
;
int
n
;
// read to EOF which may read more or less than initialSize (eg: file
// is truncated while we are reading)
while
((
n
=
source
.
read
(
buf
,
nread
,
rem
))
>
0
)
{
nread
+=
n
;
rem
-=
n
;
assert
rem
>=
0
;
if
(
rem
==
0
)
{
// need larger buffer
int
newCapacity
=
capacity
<<
1
;
if
(
newCapacity
<
0
)
{
if
(
capacity
==
Integer
.
MAX_VALUE
)
throw
new
OutOfMemoryError
(
"Required array size too large"
);
newCapacity
=
Integer
.
MAX_VALUE
;
}
rem
=
newCapacity
-
capacity
;
buf
=
Arrays
.
copyOf
(
buf
,
newCapacity
);
capacity
=
newCapacity
;
}
}
return
(
capacity
==
nread
)
?
buf
:
Arrays
.
copyOf
(
buf
,
nread
);
}
/**
* Read all the bytes from a file. The method ensures that the file is
* closed when all bytes have been read or an I/O error, or other runtime
...
...
@@ -3003,12 +2971,22 @@ public final class Files {
* method is invoked to check read access to the file.
*/
public
static
byte
[]
readAllBytes
(
Path
path
)
throws
IOException
{
long
size
=
size
(
path
);
if
(
size
>
(
long
)
Integer
.
MAX_VALUE
)
throw
new
OutOfMemoryError
(
"Required array size too large"
);
try
(
FileChannel
fc
=
FileChannel
.
open
(
path
))
{
long
size
=
fc
.
size
();
if
(
size
>
(
long
)
Integer
.
MAX_VALUE
)
throw
new
OutOfMemoryError
(
"Required array size too large"
);
byte
[]
arr
=
new
byte
[(
int
)
size
];
ByteBuffer
bb
=
ByteBuffer
.
wrap
(
arr
);
while
(
bb
.
hasRemaining
())
{
if
(
fc
.
read
(
bb
)
<
0
)
{
// truncated
break
;
}
}
try
(
InputStream
in
=
newInputStream
(
path
))
{
return
read
(
in
,
(
int
)
size
);
int
nread
=
bb
.
position
();
return
(
nread
==
size
)
?
arr
:
Arrays
.
copyOf
(
arr
,
nread
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录