如果用inputStream对象的available()方法获取流中可读取的数据大小,通常我们调用这个函数是在下载文件或者对文件进行其他处理时获取文件的总大小。
以前在我们初学File和inputStream和outputStream时,有需要将文件从一个文件夹复制到另一个文件夹中,这时候我们用的就是inputStream.available()来获取文件的总大小,而且屡试不爽。
但是当我们要从网络URL中下载一个文件时,我们发现得到的数值并不是需要下载的文件的总大小。
好吧。我们看看JDK文档中怎么解释。
available
public int available() throws IOException
返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。下一个调用可能是同一个线程,也可能是另一个线程。一次读取或跳过此估计数个字节不会受阻塞,但读取或跳过的字节数可能小于该数。
注意,有些 InputStream
的实现将返回流中的字节总数,但也有很多实现不会这样做。试图使用此方法的返回值分配缓冲区,以保存此流所有数据的做法是不正确的。
如果已经调用 close()
方法关闭了此输入流,那么此方法的子类实现可以选择抛出 IOException
。
类 InputStream
的 available
方法总是返回 0
。
此方法应该由子类重写。
返回:可以不受阻塞地从此输入流读取(或跳过)的估计字节数;如果到达输入流末尾,则返回 0
。抛出:IOException
- 如果发生 I/O 错误。
inputStream 源代码
- /**
- * Returns the number of bytes that are available before this stream will
- * block. This implementation always returns 0. Subclasses should override
- * and indicate the correct number of bytes available.
- *
- * @return the number of bytes available before blocking.
- * @throws IOException
- * if an error occurs in this stream.
- * @since Android 1.0
- */
- <SPAN style="COLOR: #ff0000"> public int available() throws IOException {
- return 0;
- }</SPAN>
/**
* Returns the number of bytes that are available before this stream will
* block. This implementation always returns 0. Subclasses should override
* and indicate the correct number of bytes available.
*
* @return the number of bytes available before blocking.
* @throws IOException
* if an error occurs in this stream.
* @since Android 1.0
*/
public int available() throws IOException {
return 0;
}
这里返回的是 0 值。
所以说要从网络中下载文件时,我们知道网络是不稳定的,也就是说网络下载时,read()方法是阻塞的,说明这时我们用
inputStream.available()获取不到文件的总大小。
但是从本地拷贝文件时,我们用的是FileInputStream.available(),难道它是将先将硬盘中的数据先全部读入流中?
然后才根据此方法得到文件的总大小?
好吧,我们来看看FileInputStream源代码吧
- /**
- * Returns the number of bytes that are available before this stream will
- * block. This method always returns the size of the file minus the current
- * position.
- *
- * @return the number of bytes available before blocking.
- * @throws IOException
- * if an error occurs in this stream.
- * @since Android 1.0
- */
- @Override
- public int available() throws IOException {
- openCheck();
- // BEGIN android-added
- // Android always uses the ioctl() method of determining bytes
- // available. See the long discussion in
- // org_apache_harmony_luni_platform_OSFileSystem.cpp about its
- // use.
- <SPAN style="COLOR: #ff0000">return fileSystem.ioctlAvailable(fd.descriptor);</SPAN>
- // END android-added
- // BEGIN android-deleted
- // synchronized (repositioningLock) {
- // // stdin requires special handling
- // if (fd == FileDescriptor.in) {
- // return (int) fileSystem.ttyAvailable();
- // }
- //
- // long currentPosition = fileSystem.seek(fd.descriptor, 0L,
- // IFileSystem.SEEK_CUR);
- // long endOfFilePosition = fileSystem.seek(fd.descriptor, 0L,
- // IFileSystem.SEEK_END);
- // fileSystem.seek(fd.descriptor, currentPosition,
- // IFileSystem.SEEK_SET);
- // return (int) (endOfFilePosition - currentPosition);
- // }
- // END android-deleted
- }
/**
* Returns the number of bytes that are available before this stream will
* block. This method always returns the size of the file minus the current
* position.
*
* @return the number of bytes available before blocking.
* @throws IOException
* if an error occurs in this stream.
* @since Android 1.0
*/
@Override
public int available() throws IOException {
openCheck();
// BEGIN android-added
// Android always uses the ioctl() method of determining bytes
// available. See the long discussion in
// org_apache_harmony_luni_platform_OSFileSystem.cpp about its
// use.
return fileSystem.ioctlAvailable(fd.descriptor);
// END android-added
// BEGIN android-deleted
// synchronized (repositioningLock) {
// // stdin requires special handling
// if (fd == FileDescriptor.in) {
// return (int) fileSystem.ttyAvailable();
// }
//
// long currentPosition = fileSystem.seek(fd.descriptor, 0L,
// IFileSystem.SEEK_CUR);
// long endOfFilePosition = fileSystem.seek(fd.descriptor, 0L,
// IFileSystem.SEEK_END);
// fileSystem.seek(fd.descriptor, currentPosition,
// IFileSystem.SEEK_SET);
// return (int) (endOfFilePosition - currentPosition);
// }
// END android-deleted
}
这里重写了inputStream中的available()方法
关键是:fileSystem.ioctlAvailable(fd.descriptor);
调用了FileSystem这是java没有公开的一个类,JavaDoc API没有。
其中
fileSystem 是一个IFileSystem对象,IFileSySTEM是java没有公开的一个类,JavaDoc API中没有;
fd是一个FileDescriptor对象,即文件描述符
说明这句代码应该是通过文件描述符获取文件的总大小,而并不是事先将磁盘上的文件数据全部读入流中,再获取文件总大小
搞清楚了这些,但是我们的主要问题没有解决,怎么获得网络文件的总大小?
我想原理应该都差不多,应该也是通过一个类似文件描述符的东西来获取。
网络下载获取文件总大小的代码:
- <SPAN style="COLOR: #ff0000">HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
- httpconn.getContentLength();</SPAN>
HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
httpconn.getContentLength();
我们再来看看httpconn.getContentLength();
- /**
- * Gets the content length in bytes specified by the response header field
- * {@code content-length} or {@code -1} if this field is not set.
- *
- * @return the value of the response header field {@code content-length}.
- * @since Android 1.0
- */
- public int getContentLength() {
- <SPAN style="COLOR: #ff0000">return getHeaderFieldInt("Content-Length", -1);</SPAN> //$NON-NLS-1$
- }
/**
* Gets the content length in bytes specified by the response header field
* {@code content-length} or {@code -1} if this field is not set.
*
* @return the value of the response header field {@code content-length}.
* @since Android 1.0
*/
public int getContentLength() {
return getHeaderFieldInt("Content-Length", -1); //$NON-NLS-1$
}
关键:getHeaderFieldInt("Content-Length", -1);
意思是从http预解析头中获取“Content-length”字段的值
其实也是类似从文件描述符中获取文件的总大小
相关推荐
NULL 博文链接:https://superherosk123.iteye.com/blog/611933
原理讲解-ServletInputStream.readLine(byte[] b, int off, int len) 方法原理讲解-ServletInputStream.readLine(byte[] b, int off, int len) 方法
(1)方法1:InputStream.available()得到字节数,然后一次读取完。 代码如下: private String readUserAgreementFromAsset(String assetName) { String content =””; try { InputStream is= getAssets().open...
NULL 博文链接:https://shihuan830619.iteye.com/blog/2094068
关于InputStream和OutputStream关于InputStream和OutputStream关于InputStream和OutputStream关于InputStream和OutputStream
springboot 解决InputStream只能读取一次的问题
Java中InputStream类.pdf 学习资料 复习资料 教学资源
读取网络字节流 php stream 二进制
NULL 博文链接:https://cuisuqiang.iteye.com/blog/1434416
Blob、InputStream、byte 互转
深入剖析tomcat第三章SocketInputStream源码 * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/StringManager.java,v 1.2 2001/07/22 20:25:14 pier Exp $ * $Revision: 1.2...
inputstream读取数据的问题,对你的file的帮组是很大的。
InputStream与OutputStream及File间互转
Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝InputStream类Java SE程序 文件的拷贝...
InputStream的用法2 --- 马克-to-win java视频 输入流
InputStream的用法1 ---马克-to-win java视频 输入流
通过jsoup爬取网上图片,其中包括输入流InputStream的用法
IO流文档,相关IO流的说明文档.............
java.io.Reader 和 java.io.InputStream 的区别
主要介绍了Java实现inputstream流的复制代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下