[英]Why does getResourceAsStream() and reading file with FileInputStream return arrays of different length?
I want to read files as byte arrays and realised that amount of read bytes varies depending on the used method. 我想将文件读取为字节数组,并意识到读取的字节数取决于所使用的方法。 Here the relevant code:
这里相关代码:
public byte[] readResource() {
try (InputStream is = getClass().getClassLoader().getResourceAsStream(FILE_NAME)) {
int available = is.available();
byte[] result = new byte[available];
is.read(result, 0, available);
return result;
} catch (Exception e) {
log.error("Failed to load resource '{}'", FILE_NAME, e);
}
return new byte[0];
}
public byte[] readFile() {
File file = new File(FILE_PATH + FILE_NAME);
try (InputStream is = new FileInputStream(file)) {
int available = is.available();
byte[] result = new byte[available];
is.read(result, 0, available);
return result;
} catch (Exception e) {
log.error("Failed to load file '{}'", FILE_NAME, e);
}
return new byte[0];
}
Calling File.length() and reading with the FileInputStream returns the correct length of 21566 bytes for the given test file, though reading the file as a resources returns 21622 bytes. 调用File.length()并使用FileInputStream读取将为给定的测试文件返回正确的21566字节长度,尽管将文件作为资源读取会返回21622字节。
Does anyone know why I get different results and how to fix it so that readResource() returns the correct result? 有谁知道为什么我得到不同的结果以及如何解决它,以便readResource()返回正确的结果?
Why does getResourceAsStream() and reading file with FileInputStream return arrays of different length?
为什么getResourceAsStream()和使用FileInputStream读取文件会返回不同长度的数组?
Because you're misusing the available()
method in a way that is specifically warned against in the Javadoc : 因为你滥用
available()
在被打击的特别警告的方式方法的Javadoc :
"It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream."
“使用此方法的返回值分配旨在容纳该流中所有数据的缓冲区永远是不正确的。”
and 和
Does anyone know why I get different results and how to fix it so that readResource() returns the correct result?
有谁知道为什么我得到不同的结果以及如何解决它,以便readResource()返回正确的结果?
Read in a loop until end of stream. 循环读取直到流结束。
According to the the API docs of InputStream, InputStream.available()
does not return the size of the resource - it returns 根据InputStream的API文档 ,
InputStream.available()
不返回资源的大小-它返回
an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking
在不阻塞的情况下可以从此输入流读取(或跳过)的字节数的估计值
To get the size of a resource from a stream, you need to fully read the stream, and count the bytes read. 要从流中获取资源的大小,您需要完全读取流,并计算读取的字节数。
To read the stream and return the contents as a byte array, you could do something like this: 要读取流并以字节数组形式返回内容,可以执行以下操作:
try ( InputStream is = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
return bos.toByteArray();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.