Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
3563fdc3
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看板
提交
3563fdc3
编写于
7月 29, 2013
作者:
I
igerasim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
Reviewed-by: alanb, chegar, martin, rriggs
上级
bf632342
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
83 addition
and
17 deletion
+83
-17
src/share/classes/java/nio/file/Files.java
src/share/classes/java/nio/file/Files.java
+62
-15
test/java/nio/file/Files/BytesAndLines.java
test/java/nio/file/Files/BytesAndLines.java
+21
-2
未找到文件。
src/share/classes/java/nio/file/Files.java
浏览文件 @
3563fdc3
...
...
@@ -25,10 +25,10 @@
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.Channels
;
import
java.nio.channels.FileChannel
;
import
java.nio.channels.SeekableByteChannel
;
import
java.io.Closeable
;
...
...
@@ -2965,7 +2965,63 @@ public final class Files {
}
/**
* Read all the bytes from a file. The method ensures that the file is
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private
static
final
int
MAX_BUFFER_SIZE
=
Integer
.
MAX_VALUE
-
8
;
/**
* Reads all the bytes from an input stream. Uses {@code initialSize} as a hint
* about how many bytes the stream will have.
*
* @param source
* the input stream to read from
* @param initialSize
* the initial size of the byte array to allocate
*
* @return a byte array containing the bytes read from the file
*
* @throws IOException
* if an I/O error occurs reading from the stream
* @throws OutOfMemoryError
* if an array of the required size cannot be allocated
*/
private
static
byte
[]
read
(
InputStream
source
,
int
initialSize
)
throws
IOException
{
int
capacity
=
initialSize
;
byte
[]
buf
=
new
byte
[
capacity
];
int
nread
=
0
;
int
n
;
for
(;;)
{
// 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
,
capacity
-
nread
))
>
0
)
nread
+=
n
;
// if last call to source.read() returned -1, we are done
// otherwise, try to read one more byte; if that failed we're done too
if
(
n
<
0
||
(
n
=
source
.
read
())
<
0
)
break
;
// one more byte was read; need to allocate a larger buffer
if
(
capacity
<=
MAX_BUFFER_SIZE
-
capacity
)
{
capacity
=
Math
.
max
(
capacity
<<
1
,
BUFFER_SIZE
);
}
else
{
if
(
capacity
==
MAX_BUFFER_SIZE
)
throw
new
OutOfMemoryError
(
"Required array size too large"
);
capacity
=
MAX_BUFFER_SIZE
;
}
buf
=
Arrays
.
copyOf
(
buf
,
capacity
);
buf
[
nread
++]
=
(
byte
)
n
;
}
return
(
capacity
==
nread
)
?
buf
:
Arrays
.
copyOf
(
buf
,
nread
);
}
/**
* Reads 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
* exception, is thrown.
*
...
...
@@ -2989,22 +3045,13 @@ public final class Files {
* method is invoked to check read access to the file.
*/
public
static
byte
[]
readAllBytes
(
Path
path
)
throws
IOException
{
try
(
FileChannel
fc
=
FileChannel
.
open
(
path
))
{
try
(
FileChannel
fc
=
FileChannel
.
open
(
path
);
InputStream
is
=
Channels
.
newInputStream
(
fc
))
{
long
size
=
fc
.
size
();
if
(
size
>
(
long
)
Integer
.
MAX_VALU
E
)
if
(
size
>
(
long
)
MAX_BUFFER_SIZ
E
)
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
;
}
}
int
nread
=
bb
.
position
();
return
(
nread
==
size
)
?
arr
:
Arrays
.
copyOf
(
arr
,
nread
);
return
read
(
is
,
(
int
)
size
);
}
}
...
...
test/java/nio/file/Files/BytesAndLines.java
浏览文件 @
3563fdc3
...
...
@@ -22,7 +22,7 @@
*/
/* @test
* @bug 7006126
* @bug 7006126
8020669
* @summary Unit test for methods for Files readAllBytes, readAllLines and
* and write methods.
*/
...
...
@@ -82,6 +82,16 @@ public class BytesAndLines {
write
(
file
,
lines
,
Charset
.
defaultCharset
(),
opts
);
throw
new
RuntimeException
(
"NullPointerException expected"
);
}
catch
(
NullPointerException
ignore
)
{
}
// read from procfs
if
(
System
.
getProperty
(
"os.name"
).
equals
(
"Linux"
))
{
// Refer to the Linux proc(5) man page for details about /proc/self/stat file
// procfs reports it to be zero sized, even though data can be read from it
String
statFile
=
"/proc/self/stat"
;
Path
pathStat
=
Paths
.
get
(
statFile
);
byte
[]
data
=
Files
.
readAllBytes
(
pathStat
);
assertTrue
(
data
.
length
>
0
,
"Files.readAllBytes('"
+
statFile
+
"') failed to read"
);
}
}
...
...
@@ -174,6 +184,16 @@ public class BytesAndLines {
throw
new
RuntimeException
(
"NullPointerException expected"
);
}
catch
(
NullPointerException
ignore
)
{
}
// read from procfs
if
(
System
.
getProperty
(
"os.name"
).
equals
(
"Linux"
))
{
// Refer to the Linux proc(5) man page for details about /proc/self/status file
// procfs reports this file to be zero sized, even though data can be read from it
String
statusFile
=
"/proc/self/status"
;
Path
pathStatus
=
Paths
.
get
(
statusFile
);
lines
=
Files
.
readAllLines
(
pathStatus
,
US_ASCII
);
assertTrue
(
lines
.
size
()
>
0
,
"Files.readAllLines('"
+
pathStatus
+
"') failed to read"
);
}
}
finally
{
delete
(
tmpfile
);
}
...
...
@@ -242,7 +262,6 @@ public class BytesAndLines {
}
finally
{
delete
(
tmpfile
);
}
}
static
void
assertTrue
(
boolean
expr
,
String
errmsg
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录