[Android Pro] About inputstream.available () method gets the total size of the file

Source: Internet
Author: User

Reference to:http://hold-on.iteye.com/blog/1017449

If you use the available () method of the InputStream object to get the size of the data that can be read in the stream, we usually call this function to get the total size of the file when the file is downloaded or other processing is performed on the file.

In the past, when we were beginners to file and InputStream and OutputStream, it was necessary to copy files from one folder to another, when we used inputstream.available () to get the total size of the file, and it worked.

But when we want to download a file from the Web URL, we find that the resulting value is not the total size of the file that needs to be downloaded.

All right. Let's look at how the JDK is explained in the documentation.

Available
available ()              throws IOException
Returns the number of estimated bytes a method call can read (or skip) from this input stream without blocking. The next call may be the same thread or another thread. One read or skip this estimated number of bytes will not be blocked, but the number of bytes read or skipped may be less than that number.

Note that some InputStream implementations will return the total number of bytes in the stream, but there are many implementations that do not. An attempt to use the return value of this method to allocate a buffer to hold all data for this stream is incorrect.

If the method has been called close() to close the input stream, the subclass implementation of this method can optionally be thrown IOException .

InputStreamthe methods of the class are available always returned 0 .

This method should be overridden by subclasses.

return:The estimated number of bytes that can be read (or skipped) from this input stream without blocking, or if the end of the input stream is reached0。Thrown: IOException-If an I/O error occurs.

InputStream Source Code

    /*** Returns the number of bytes that is available before this stream would * block. This implementation always returns 0.         Subclasses should override * and indicate the correct number of bytes available. *          * @returnThe number of bytes available before blocking. * @throwsIOException * If an error occurs in this stream. * @sinceAndroid 1.0*/         Public intAvailable ()throwsIOException {return0; }

This returns a value of 0.

So to download files from the network, we know that the network is not stable, that is, when the network download, the Read () method is blocked, indicating that we use

Inputstream.available () does not get the total size of the file.

But when we copy files locally, we use fileinputstream.available (), is it going to first read the data from the hard drive into the stream first?

Then you get the total size of the file based on this method?

Okay, let's take a look at the FileInputStream source code.

    /*** Returns the number of bytes that is available before this stream would * block.         This method is always returns the size of the file minus the current * position. *          * @returnThe number of bytes available before blocking. * @throwsIOException * If an error occurs in this stream. * @sinceAndroid 1.0*/@Override Public intAvailable ()throwsIOException {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 .                 returnfilesystem.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}

This overrides the available () method in InputStream.

The key is:filesystem.ioctlavailable (fd.descriptor);

Called FileSystem this is a class that Java does not expose, and the JavaDoc API does not.
which

FileSystem is a Ifilesystem object, Ifilesystem is a class that is not exposed in Java, and is not in the JAVADOC API;

FD is a FileDescriptor object, that is, a file descriptor

This code should be a file descriptor to get the total size of the file, rather than the file data on the disk to read all the stream, and then get the total file size

Figure this out, but our main problem is not solved, how to get the total size of network files?

I think the principle should be the same, and it should be obtained by something similar to a file descriptor.

Network download Gets the total file size code:

HttpURLConnection httpconn = (httpurlconnection) url.openconnection ();  Httpconn.getcontentlength ();

Let's take a look at Httpconn.getcontentlength ();

/*** Gets The content length in bytes specified by the response header field * {@codeContent-length} or {@code-1} If this field was not set. *   * @returnThe value of the response header field {@codeContent-length}. * @sinceAndroid 1.0*/   Public intgetcontentlength () {returnGetheaderfieldint ("Content-length", 1);} 

Key:getheaderfieldint ("Content-length",-1);

Gets the value of the "Content-length" field from the HTTP pre-parsing header

It's also similar to getting the total size of a file from a file descriptor

[Android Pro] About inputstream.available () method gets the total size of the file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.