Java IO (ii) InputStream

Source: Internet
Author: User

The source code is JDK1.8 as reference

Preface:
The InputStream implements two interfaces closeable and autocloseable:

Introduced in closeable:jdk1.5, there is only one definition of the close () method in the Closeable interface:
public void Close () throws IOException;
The object of the class that implements the Closeable interface can be closed, and the stream class implements the interface to achieve the purpose of closing the stream.
Introduced in autocloseable:jdk1.7, provides support for the resource-enabled try statement introduced in JDK1.7, and the try can automatically close the resource. The Autocloseable interface also defines only the definition of a close () method:
void Close () throws Exception;
Only objects with classes that implement the Autocloseable interface can be managed using a try statement with a resource. Closeable inherits the Autocloseable interface in JDK1.7, so the stream classes under IO Packets can be managed using a try statement with a resource.

Source Analysis:
InputStream is in contrast to Java itself, which reads data inward from other sources, so it is called an input stream. InputStream is an abstract class, which is the base class for java.io stream byte streams.
A series of methods are defined in InputStream,
Main methods:
1.public abstract int Read ():
Reads the next byte of data from the input stream. Returns an int byte value in the range 0 to 255. If no bytes are available since the end of the stream has been reached, the return value-1. This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown.
Subclasses must provide an implementation of this method.
2.public int Read (byte b[]):
Reads a certain number of bytes from the input stream and stores it in buffer array B. Returns the number of bytes actually read in integer form.
3.public int Read (byte b[], int off, int len):
Reads an array of up to Len data bytes in the input stream into byte, starting at the off index of a byte array. An attempt was to read Len bytes, but the bytes read could also be less than that value. Returns the number of bytes actually read in integer form.
Note: for the 2 and 3 methods in InputStream, their implementations are implemented by the 1 method, which takes a single byte through the 1 method and reads into the buffer array specified in 2 and 3.
4. Public long Skip (long N):
Skips the read of the specified number of bytes in the input stream, the number of bytes skipped is N, but InputStream defines the maximum length of skip max_skip_buffer_size=2048,n exceeds this value and will only skip Max_skip_buffer_ Size bytes of reading, the source code is as follows:

 Public Long Skip(LongNthrowsIOException {Longremaining = n;intNrif(N <=0) {return 0; }intSize = (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; }returnn-remaining; }

Also based on the Read series overload method implementation, the principle is to directly speak the specified number of bytes read, but do not hold to complete the skip operation.
5. public int available ():
Returns the length of the data that the InputStream can read the next time without being blocked.
6. public void Close ():
Close InputStream Stream
7. Public synchronized void mark (int readlimit):
Marks a location in the InputStream stream, which is implemented specifically by its subclasses.
8. Public synchronized void Reset ():
Repositions this stream to the last position when the mark method is called on this input stream.
9. Public boolean marksupported ():
Checks whether this stream supports the public synchronized void mark (int readlimit) method that is implemented specifically.

Total: InputStream to a certain extent, an abstraction of the input stream, defines the basic responsibilities of the input stream, but does not provide a concrete implementation, but do simple constraints, not mandatory rewrite, because InputStream is an abstract modification, so it does not have the ability to instantiate, To implement various operations on a stream entirely with its subclasses.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java IO (ii) InputStream

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.