Java IO InputStream Source code

Source: Internet
Author: User

writer:bysocket (mud and brick pulp carpenter)

Micro-Blog: Bysocket

Watercress: Bysocket

FaceBook: Bysocket

Twitter: Bysocket

First, InputStream

InputStream is an abstract class that represents the base class for all byte input stream implementation classes. Its function is to represent all the classes that generate input from different data sources, such as common FileInputStream, FilterInputStream, and so on. What about the data sources? For example:

1) byte array (does not represent a string class, but can be converted)

2) String Object

3) file

4) Serialization of a different kind of flow (common in distributed systems)

5) Pipelines (data sources in multithreaded environments)

wait

Note that it is part of the byte stream, not the character stream (java.io in Reader\writer, as described below).

FilterInputStream is provided for a variety of inputstream implementation Classes " Adorner mode "base class. Therefore, it can be divided into the original byte stream and the "decoration" over the function encapsulates the byte stream.

Second, the core of the InputStream source of the fine solution

The source code is as follows:

/** * base class for all byte input stream implementation classes */publicabstractclasssinputstream {//buffer byte array maximum privatestaticfinalintmax_skip_buffer_size = 2     048;     Reads the next byte of data from the input stream, returning Publicabstractintread () throwsioexception with int;    Reads a certain number of bytes of data from the input stream and stores it in the cache array B publicintread (byteb[]) throwsioexception {returnread (b, 0, b.length);            }//Read data from the input stream up to len bytes and stored in cache array B publicintread (byteb[], Intoff, Intlen) throwsioexception {if (b = = null) {        Thrownewnullpointerexception ();        } elseif (Off < 0| | Len < 0| | len > B.length-off) {thrownewindexoutofboundsexception ();        } elseif (len = = 0) {return0;        } INTC = Read ();        if (c = =-1) {return-1;         } B[off] = (byte) c;        Inti = 1;                try{for (; i < Len; i++) {c = read ();                if (c = =-1) {break;            } B[off + i] = (byte) c; }} catch (Ioexception ee) {} Returni;        }//skips n bytes of data in the input stream Publiclongskip (LONGN) throwsioexception {longremaining = n;         INTNR;        if (n <= 0) {return0;        } intsize = (int) math.min (max_skip_buffer_size, remaining);        byte[] Skipbuffer = newbyte[size];            while (Remaining > 0) {nr = read (Skipbuffer, 0, (int) math.min (size, remaining));            if (NR < 0) {break;        } remaining-= NR;    } returnn-remaining;    }//Returns the estimated number of bytes of the next method call that can be read (or skipped) from this point without blocking publicintavailable () throwsioexception {return0; }//closes this input stream and frees all resources associated with it Publicvoidclose () throwsioexception {}//mark the current position in this output stream Publicsynchronizedvoidmark (    Intreadlimit) {}//relocate this stream to the last position when the mark method is called on this input stream.    Publicsynchronizedvoidreset () throwsioexception {thrownewioexception ("Mark/reset not supported"); }//test whether this input stream supports the mark and reset method publicbooleanmarksupported () {returnfalse; } }




among them, inputstream the following three read methods is the Core method:

public abstract int Read ()


abstract method, no specific implementation. Because subclasses must implement an implementation of this method. This is the key method of the input stream.

both, it is seen that the following two read () methods have called the implementation of this method subclass to complete the function.


public int read (byteb[])


the method is to represent a certain number of bytes of data being read from the input stream and stored in the cache byte array B. The effect is equivalent to invoking the implementation of the following method:

Read (b, 0, B.length)


If the length of B is 0, no bytes are read and 0 is returned, otherwise an attempt to read at least 1 bytes is attempted. If no bytes are available because the stream is at the end of the file, a value of 1 is returned, or at least one byte is read and stored in B.

thinking: At this time, no wonder many times, b!=–1 or b! = EOF


public int read (byteb[], Intoff, Intlen)


This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown.


the method checks first and then verifies that the next byte is empty. If the validation passes,
The following code:

int i = 1;try{for    (; i < Len; i++) {        c = read ();        if (c = =-1) {break            ;        }        B[off + i] = (byte) c;    }} catch (IOException ee) {}


The first byte to be read is stored in the element B[off], the next one is stored in b[off+1], and so on. The number of bytes read is at most equal to Len. Set K to the actual number of bytes read, these bytes will be stored in the elements of B[off] to b[off+K-1], without affecting the elements b[off+K] to b[off+len-1].

because there are two implementations of the above read, InputStream is designed as abstract classes here.  

Third, summary

1. The inputsream corresponds to the OutputStream

2. Look at the source code is to enjoy the people write the show how

3. Masons Learn the code on GitHub (synchronous osc git), welcome to Point Star, suggestions, progress together. Address: https://github.com/JeffLi1993

Java IO InputStream Source code

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.