Java IO (ii) InputStream

Source: Internet
Author: User

The source code is JDK1.8 as a 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. Try to shut down resources on its own Initiative.

The Autocloseable interface also defines only the definition of a close () method:
void Close () throws Exception;
Only the object of the class that implements the Autocloseable interface is capable of being 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 Code 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. 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. Assume that there are no bytes available because the end of the stream has been reached. The return value-1.

Before the input data is available, detected at the end of the stream, or throws an Exception. This method has been blocked.
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 a maximum of Len data bytes in the input stream into a byte array, 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, The fact is now dependent on the 1 method to achieve, through the 1 method to get a single byte, read 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. Only Max_skip_buffer_size bytes of reading will be Skipped. Source code such as the Following:

 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; }

The same is true based on the Read series overload method, the principle is to directly speak the specified number of bytes Read. however, The skip operation is not Kept.
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 in detail by its subclasses.
8. Public synchronized void Reset ():
Positions this stream once again to the last time the Mark method is called on this input Stream.
9. Public Boolean marksupported ():
Checks whether this stream supports detailed implementation of the public synchronized void Mark (int Readlimit) method.

Total: InputStream an abstraction of the input stream to some Extent. Defines the basic responsibilities of the input stream, but does not provide a detailed implementation. It's just a simple constraint. is not a mandatory rewrite, because InputStream is an abstract adornment, so it does not have the ability to instantiate, to fully rely on its subclasses to implement various operations for the Stream.

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.