Java IO (II)-InputStream, javaioinputstream

Source: Internet
Author: User

Java IO (II)-InputStream, javaioinputstream

The source code uses JDK1.8 as a reference.

Preface:
InputStream implements two interfaces: Closeable and AutoCloseable:

Closeable: introduced in JDK1.5, The Closeable interface has only one definition of the close () method:
Public void close () throws IOException;
Objects of classes that implement the Closeable interface can be closed, and stream classes all implement this interface to close the stream.
AutoCloseable: introduced in JDK1.7, which provides support for the resource-containing try statement introduced in JDK1.7. try can automatically shut down the resource. The AutoCloseable interface also defines only one close () method:
Void close () throws Exception;
Only objects of classes that implement the AutoCloseable interface can be managed using the try statement with resources. In JDK1.7, Closeable inherits the AutoCloseable interface, so the stream classes under the I/O package can be managed using the try statement with resources.

Source code analysis:
InputStream is relative to Java itself. It reads data in other ways, so it is called an input stream. InputStream is an abstract class and is the base class of the byte stream of java. io streams.
InputStream defines a series of methods,
Main Methods:
1. public abstract int read ():
The next byte that reads data from the input stream. Returns the int byte value between 0 and 255. If no byte is available because it has reached the end of the stream, the return value is-1. This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown.
Subclass must provide an implementation of this method.
2. public int read (byte B []):
Read a certain number of bytes from the input stream and store them in the buffer array B. Returns the actual number of bytes read as an integer.
3. public int read (byte B [], int off, int len ):
Reads a maximum of len data bytes from the input stream into the byte array (read from the off index of the byte array. Try to read len bytes, but the read bytes may be smaller than this value. Returns the actual number of bytes read as an integer.
Note:For the methods 2 and 3 in InputStream, the implementation relies on the method 1 to obtain a single byte, read the buffer array specified in 2 and 3.
4. public long skip (long n ):
The specified number of bytes is skipped in the input stream. n is skipped. However, InputStream defines the maximum length of skip MAX_SKIP_BUFFER_SIZE = 2048. n exceeds this value, only MAX_SKIP_BUFFER_SIZE bytes will be skipped. The source code is as follows:

public long skip(long n) throws IOException {        long remaining = n;        int nr;        if (n <= 0) {            return 0;        }        int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);        byte[] skipBuffer = new byte[size];        while (remaining > 0) {            nr = read(skipBuffer, 0, (int)Math.min(size, remaining));            if (nr < 0) {                break;            }            remaining -= nr;        }        return n - remaining;    }

It is also implemented based on the read series overload method. The principle is to directly read a specified number of bytes without retaining them to complete the skip operation.
5. public int available ():
Returns the data length that can be read by the InputStream next time without being blocked.
6. public void close ():
Disable InputStream stream
7. public synchronized void mark (int readlimit ):
Mark a position in the InputStream stream, which is implemented by its subclass.
8. public synchronized void reset ():
Locate the stream to the last time the mark method was called for the input stream.
9. public boolean markSupported ():
Check whether the stream supports the public synchronized void mark (int readlimit) method.

Total:To a certain extent, InputStream abstracts the input stream and defines the basic responsibilities of the input stream. However, it does not provide specific implementations, but only implements simple constraints rather than force rewriting, because InputStream is abstract modified, it does not have the ability to instantiate. It must use its subclass to perform various operations on the stream.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.