Java io包 ByteArrayInputStream&ByteArrayOutStream

package java.io;

 

/**

 * A <code>ByteArrayInputStream</code> contains

 * an internal buffer that contains bytes that

 * may be read from the stream. An internal

 * counter keeps track of the next byte to

 * be supplied by the <code>read</code> method.

 * <p>

 * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in

 * this class can be called after the stream has been closed without

 * generating an <tt>IOException</tt>.

 *

 * @Author Arthur van Hoff

 * @see     java.io.StringBufferInputStream

 * @since   JDK1.0

 */

public class ByteArrayInputStream extends InputStream {

 

    /**

     * An array of bytes that was provided

     * by the creator of the stream. Elements <code>buf[0]</code>

     * through <code>buf[count-1]</code> are the

     * only bytes that can ever be read from the

     * stream;  element <code>buf[pos]</code> is

     * the next byte to be read.

     */

    // byte array input stream

protected byte buf[];

 

    // input stream current location

    protected int pos;

 

    // input stream marker location

    protected int mark = 0;

 

    // maximum position input stream can be read plus 1

    protected int count;

 

    //Constructor

    public ByteArrayInputStream(byte buf[]) {

        this.buf = buf;

        this.pos = 0;

        this.count = buf.length;

    }

 

    //Constructor

    public ByteArrayInputStream(byte buf[], int offset, int length) {

        this.buf = buf;

        this.pos = offset;

        this.count = Math.min(offset + length, buf.length);

        this.mark = offset;

    }

 

    // return a byte of the byte value (0 to 255) is read from the input stream

    // read in the end the return -1

    public synchronized int read() {

        return (pos < count) ? (buf[pos++] & 0xff) : -1;

    }

 

    // read the array specifies the length and position from the input stream

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

        if (b == null) {

            throw new NullPointerException();

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

            throw new IndexOutOfBoundsException();

        }

 

        if (pos >= count) {

            return -1;

        }

 

        int avail = count - pos;

        if (len > avail) {

            len = avail;

        }

        if (len <= 0) {

            return 0;

        }

        // native method

        System.arraycopy(buf, pos, b, off, len);

        pos + = only;

        return len;

    }

 

    // Skip the specified byte

    public synchronized long skip(long n) {

        long k = count - pos;

        if (n < k) {

            k = n < 0 ? 0 : n;

        }

 

        pos += k;

        return k;

    }

 

    // returns the maximum byte length that can be read

    public synchronized int available() {

        return count - pos;

    }

 

    public boolean markSupported() {

        return true;

    }

   

    public void mark(int readAheadLimit) {

        mark = pos;

    }

  

    public synchronized void reset() {

        pos = mark;

    }

 

    public void close() throws IOException {

    }

 

}

 

 

分析ByteArrayOutputStream

package java.io;

 

import java.util.Arrays;

 

/**

 * This class implements an output stream in which the data is

 * written into a byte array. The buffer automatically grows as data

 * is written to it.

 * The data can be retrieved using <code>toByteArray()</code> and

 * <code>toString()</code>.

 * <p>

 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in

 * this class can be called after the stream has been closed without

 * generating an <tt>IOException</tt>.

 *

 * @Author Arthur van Hoff

 * @since   JDK1.0

 */

 

public class ByteArrayOutputStream extends OutputStream {

 

// byte array output stream

    protected byte buf[];

   

    // maximum position input stream can be read plus 1

    protected int count;

 

    // default constructor

    public ByteArrayOutputStream() {

        this(32);

    }

 

    //Constructor

    public ByteArrayOutputStream(int size) {

        if (size < 0) {

            throw new IllegalArgumentException("Negative initial size: "

                                               + size);

        }

        buf = new byte[size];

    }

 

    // If the output stream to ensure that capacity is not enough to byte array expansion

    private void ensureCapacity(int minCapacity) {

        // overflow-conscious code

        if (minCapacity - buf.length > 0)

            grow(minCapacity);

    }

 

    // maximum capacity of the output stream byte array

    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

 

    // Array Expansion

    private void grow(int minCapacity) {

        // overflow-conscious code

        int oldCapacity = buf.length;

        int newCapacity = oldCapacity << 1;

        if (newCapacity - minCapacity < 0)

            newCapacity = minCapacity;

        if (newCapacity - MAX_ARRAY_SIZE > 0)

            newCapacity = hugeCapacity(minCapacity);

        buf = Arrays.copyOf(buf, newCapacity);

    }

   

    // handling of expansion beyond the maximum limit of

    private static int hugeCapacity(int minCapacity) {

        if (minCapacity < 0) // overflow

            throw new OutOfMemoryError();

        return (minCapacity > MAX_ARRAY_SIZE) ?

            Integer.MAX_VALUE :

            MAX_ARRAY_SIZE;

    }

 

    // output stream writing a byte

    public synchronized void write(int b) {

        ensureCapacity(count + 1);

        buf[count] = (byte) b;

        count += 1;

    }

 

    // byte array output stream

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

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

            ((off + len) - b.length > 0)) {

            throw new IndexOutOfBoundsException();

        }

        ensureCapacity(count + len);

        System.arraycopy(b, off, buf, count, len);

        count + = only;

    }

 

    // write data to the other output stream

    public synchronized void writeTo(OutputStream out) throws IOException {

        out.write(buf, 0, count);

    }

 

    public synchronized void reset() {

        count = 0;

    }

   

    // output stream into a byte array

    public synchronized byte toByteArray()[] {

        return Arrays.copyOf(buf, count);

    }

 

    // output stream size

    public synchronized int size() {

        return count;

    }

 

    // override the toString ()

    public synchronized String toString() {

        return new String(buf, 0, count);

    }

 

    // output stream according to the coding format to a String

    public synchronized String toString(String charsetName)

        throws UnsupportedEncodingException

    {

        return new String(buf, 0, count, charsetName);

    }

 

    // waste

    @Deprecated

    public synchronized String toString(int hibyte) {

        return new String(buf, hibyte, 0, count);

    }

 

    /**

     * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in

     * this class can be called after the stream has been closed without

     * generating an <tt>IOException</tt>.

     */

    public void close() throws IOException {

    }

 

}

 

Guess you like

Origin www.cnblogs.com/shineyoung/p/11369095.html