Java io包 inputstream&outputstream

Analysis InputStream

/**

 * This abstract class is the superclass of all classes representing

 * an input stream of bytes.

 *

 * <p> Applications that need to define a subclass of <code>InputStream</code>

 * must always provide a method that returns the next byte of input.

 *

 * @Author Arthur van Hoff

 * @see     java.io.BufferedInputStream

 * @see     java.io.ByteArrayInputStream

 * @see     java.io.DataInputStream

 * @see     java.io.FilterInputStream

 * @see     java.io.InputStream#read()

 * @see     java.io.OutputStream

 * @see     java.io.PushbackInputStream

 * @since   JDK1.0

 */

public abstract class InputStream implements Closeable {

 

    // MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to

    // use when skipping.

    private static final int MAX_SKIP_BUFFER_SIZE = 2048;

   

    // abstract methods referred subclass implements an input stream of bytes read from a

    public abstract int read() throws IOException;

 

    // output stream of bytes read from the input stream to the byte b []

    public int read(byte b[]) throws IOException {

        return read(b, 0, b.length);

    }

 

    // output stream of bytes read from the input stream to the byte b []

    public int read(byte b[], int off, int len) throws IOException {

       

            // empty judgment data is zero if the array bounds checking directly returns zero length

            if (b == null) {

            throw new NullPointerException();

        } else if (off < 0 || len < 0 || len > b.length - off) {

            throw new IndexOutOfBoundsException();

        } else if (len == 0) {

            return 0;

        }

            

        // read a byte assignment

        int c = read();

        if (c == -1) {

            return -1;

        }

        b[off] = (byte)c;

 

        int i = 1;

        try {

            for (; i < len ; i++) {

                c = read();

                if (c == -1) {

                    break;

                }

                b[off + i] = (byte)c;

            }

        } catch (IOException ee) {

        }

        return i;

    }

 

    // skip specified byte actually read a specified byte discard bytes read off

    public long skip(long n) throws IOException {

 

        long remaining = n;

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

    }

 

    // In the case where the input stream is not blocked one can read data length

    public int available() throws IOException {

        return 0;

    }

 

    // close the input stream

    public void close() throws IOException {}

 

    // Mark the present position of the input stream

    public synchronized void mark(int readlimit) {}

 

    // reset the current position mark position input stream

    public synchronized void reset() throws IOException {

        throw new IOException("mark/reset not supported");

    }

 

    // whether to support the position mark

    public boolean markSupported() {

        return false;

    }

 

}

 

 

 

 

 

Analysis OutputStream

/**

 * This abstract class is the superclass of all classes representing

 * an output stream of bytes. An output stream accepts output bytes

 * and sends them to some sink.

 * <p>

 * Applications that need to define a subclass of

 * <code>OutputStream</code> must always provide at least a method

 * that writes one byte of output.

 *

 * @Author Arthur van Hoff

 * @see     java.io.BufferedOutputStream

 * @see     java.io.ByteArrayOutputStream

 * @see     java.io.DataOutputStream

 * @see     java.io.FilterOutputStream

 * @see     java.io.InputStream

 * @see     java.io.OutputStream#write(int)

 * @since   JDK1.0

 */

public abstract class OutputStream implements Closeable, Flushable {

   

// write a byte to the output stream

    public abstract void write(int b) throws IOException;

 

    /**

     * Writes <code>b.length</code> bytes from the specified byte array

     * to this output stream. The general contract for <code>write(b)</code>

     * is that it should have exactly the same effect as the call

     * <code>write(b, 0, b.length)</code>.

     *

     * @param      b   the data.

     * @exception  IOException  if an I/O error occurs.

     * @see        java.io.OutputStream#write(byte[], int, int)

     */

   

    // output stream to write to the specified array of bytes

    public void write(byte b[]) throws IOException {

        write(b, 0, b.length);

    }

 

    // output stream to write to the specified array of bytes

    public void write(byte b[], int off, int len) throws IOException {

        if (b == null) {

            throw new NullPointerException();

        } else if ((off < 0) || (off > b.length) || (len < 0) ||

                   ((off + len) > b.length) || ((off + len) < 0)) {

            throw new IndexOutOfBoundsException();

        } else if (len == 0) {

            return;

        }

        for (int i = 0 ; i < len ; i++) {

            write(b[off + i]);

        }

    }

   

    // immediately flush the write buffer data

    void flush() throws IOException {

    }

 

    // close the output stream

    public void close() throws IOException {

    }

 

}

 

Guess you like

Origin www.cnblogs.com/shineyoung/p/11368828.html
Recommended