获取Java InputStream后面文件的字节数 [英] Get the number of bytes of a file behind a Java InputStream
问题描述
正如标题所述,我需要知道在InputStream后面的文件有多少字节。
我不想下载所有字节和计数(需要很长的时间)。我只需要知道文件有多少字节即可。
As the title says, I need to know how many bytes the file has that's "behind" an InputStream. I don't want to download all bytes and count (takes to long). I just need to know how many bytes the file has.
就像这样:
int numberOfBytes = countBytes(inputStream);
所以,我需要一个countBytes(InputStream inputStream)的实现
So, I need an implementation for countBytes(InputStream inputStream)
推荐答案
除了消耗整个流并计算字节数之外,您不能(没有API)。
Other than by consuming the entire stream and counting the bytes, you can't (there's no API for it).
有 available()
方法,但它显然没有按照您的要求进行操作:
There's the available()
method, but it quite explicitly doesn't do what you're asking:
请注意,尽管某些InputStream实现将返回流中的字节总数,但许多不会。
如果 InputStream
与文件(而不是套接字)相关联,也许您可以使用其他要获取其大小的API?
If the InputStream
is associated with a file (and not, say, a socket), perhaps you could use a different API to get its size?
这篇关于获取Java InputStream后面文件的字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!