writer:bysocket (mud and brick pulp carpenter)
Micro-Blog: Bysocket
Watercress: Bysocket
FaceBook: Bysocket
Twitter: Bysocket
First, preface
the previous article " Java IO inputstream Source code ", said the InputStream. JDK1.0 has this traditional IO byte stream, which is InputStream and OutputStream. Comb the core of the two:
There are several read () methods in InputStream and several write () methods in OutputStream. They are one by one corresponding, while the core is the read () and write () methods. None of them are implemented, and all the essential calls are the two methods implemented by each implementation class.
Read () and write (), which corresponds to the input and output of the system, which is the input of the system.
Second, OutputStream
is also an abstract class, which represents the base class for all byte input stream implementation classes. Its function is to represent all the objects to be exported, such as common Fileoutstream, Filteroutputstream and so on. It implements the java.io.Closeable and java.io.Flushable two interfaces. Where null implements the Flush method, which has the function of refreshing the buffer byte array.
What about the output targets? For example:
1) byte array (does not represent a string class, but can be converted)
2) file
3) Pipelines (data sources in multithreaded environments)
Wait a minute
Filteroutputstream is provided for a variety of outputstream implementation Classes " Adorner mode "base class. Connect a property or a useful interface to the output stream.
Third, the core of the outputstream source of the fine solution
the same, first look at the source:
/** * base class for all byte output stream implementation classes */public abstract class Soutputstream implements Closeable, flushable {//writes the specified bytes to the output stream public a bstract void Write (int b) throws IOException; Writes all bytes of the specified byte array to the output stream public void write (byte b[]) throws IOException {write (b, 0, b.length); }//writes the specified byte array of len bytes starting from offset off to the output stream public void write (byte b[], int off, int len) throws IOException {if ( b = = null) {throw new NullPointerException (); } elseif ((Off < 0) | | (Off > B.length) | | (Len < 0) | | ((off + len) > b.length) | | ((off + len) < 0)) {throw new indexoutofboundsexception (); } elseif (len = = 0) {return; } for (int i = 0; i < len; i++) {write (B[off + i]); }}//refresh the output stream and force write out all buffered output bytes public void flush () throws IOException {}//closes the output stream and frees all resources related to the stream public V OID Close () throws IOException {}}
The three core write () methods correspond to the read () method of three inputstream:
1. Abstract void Write (int b) method
public abstract void Write (int b) throws IOException;
corresponding to the read () method of InputStream, this method is still an abstract method. Because subclasses must implement an implementation of this method. This is the key method of the input stream.
in both, this core method is called in the following two write methods.
It is visible that the core is how important the read () and write () methods are in traditional IO.
2. Void write (Byte b[]) method
public void Write (byte b[]) throws IOException
writes the bytes of the specified byte array to the output stream. The effect is actually implemented by the next write method, except that the amount of the call specifies the length:
3. Void write (byte b[], int off, int len) method
public void Write (byte b[], int off, int len) throws IOException
writes a specified byte array of len bytes starting at offset off to the output stream. The code detailed process is interpreted as follows:
A. If B is null, NullPointerException is thrown.
B. If off is negative, or Len is negative, or off+len is greater than the length of array B, indexoutofboundsexception is thrown.
C. writes some bytes in array b sequentially to the output stream; element B[off] is the first byte written by this operation, and B[off+len-1] is the last byte written by this operation.
Iv. Summary
important things to say three times:
OutputStream interpretation of InputStream look at the comparison! Note that a write corresponds to a read
OutputStream interpretation of InputStream look at the comparison! Note that a write corresponds to a read
OutputStream interpretation of InputStream look at the comparison! Note that a write corresponds to a read
Java IO OutputStream Source code