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.

     */

    //输入流字节数组

protected byte buf[];

 

    //输入流当前位置

    protected int pos;

 

    //输入流标记位置

    protected int mark = 0;

 

    //输入流可读取的最大位置加1

    protected int count;

 

    //构造函数

    public ByteArrayInputStream(byte buf[]) {

        this.buf = buf;

        this.pos = 0;

        this.count = buf.length;

    }

 

    //构造函数

    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;

    }

 

    //从输入流读取下一个字节 返回该字节值(0~255)

    //读到底了返回-1

    public synchronized int read() {

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

    }

 

    //从输入流中读取指定长度和位置的数组

    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 方法

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

        pos += len;

        return len;

    }

 

    //跳过指定字节

    public synchronized long skip(long n) {

        long k = count - pos;

        if (n < k) {

            k = n < 0 ? 0 : n;

        }

 

        pos += k;

        return k;

    }

 

    //返回可读取的最大字节长度

    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 {

 

//输出流字节数组

    protected byte buf[];

   

    //输入流可读取的最大位置加1

    protected int count;

 

    //默认构造函数

    public ByteArrayOutputStream() {

        this(32);

    }

 

    //构造函数

    public ByteArrayOutputStream(int size) {

        if (size < 0) {

            throw new IllegalArgumentException("Negative initial size: "

                                               + size);

        }

        buf = new byte[size];

    }

 

    //保证容量 如果输出流字节数组不够了就扩容

    private void ensureCapacity(int minCapacity) {

        // overflow-conscious code

        if (minCapacity - buf.length > 0)

            grow(minCapacity);

    }

 

    //最大输出流字节数组容量

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

 

    //数组扩容

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

    }

   

    //处理扩容超出最大限制的情况

    private static int hugeCapacity(int minCapacity) {

        if (minCapacity < 0) // overflow

            throw new OutOfMemoryError();

        return (minCapacity > MAX_ARRAY_SIZE) ?

            Integer.MAX_VALUE :

            MAX_ARRAY_SIZE;

    }

 

    //输出流写入一个字节

    public synchronized void write(int b) {

        ensureCapacity(count + 1);

        buf[count] = (byte) b;

        count += 1;

    }

 

    //输出流写入字节数组

    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 += len;

    }

 

    //向其他输出流写入数据

    public synchronized void writeTo(OutputStream out) throws IOException {

        out.write(buf, 0, count);

    }

 

    public synchronized void reset() {

        count = 0;

    }

   

    //将输出流转换为字节数组

    public synchronized byte toByteArray()[] {

        return Arrays.copyOf(buf, count);

    }

 

    //输出流大小

    public synchronized int size() {

        return count;

    }

 

    //重写toString()

    public synchronized String toString() {

        return new String(buf, 0, count);

    }

 

    //根据编码格式将输出流转换为String

    public synchronized String toString(String charsetName)

        throws UnsupportedEncodingException

    {

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

    }

 

    //废弃

    @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 {

    }

 

}

相关文章
相关标签/搜索