ByteArrayOutput类和ByteArrayInput类

来源:互联网 发布:淘宝相关股票代码 编辑:程序博客网 时间:2024/06/01 07:11

ByteArrayOutput类

这个类所实现的是将输出字节流存入一个可自动增长的byte数组中,若想从该类对象中获取字节流数据,只需要用toByteArray和toString方法就可以了。

构造函数为byte数组分配初始大小,默认为32

 /**     * Creates a new byte array output stream. The buffer capacity is      * initially 32 bytes, though its size increases if necessary.      */    public ByteArrayOutputStream() {this(32);    }    /**     * Creates a new byte array output stream, with a buffer capacity of      * the specified size, in bytes.      *     * @param   size   the initial size.     * @exception  IllegalArgumentException if size is negative.     */    public ByteArrayOutputStream(int size) {        if (size < 0) {            throw new IllegalArgumentException("Negative initial size: "                                               + size);        }buf = new byte[size];    }




想要将数据,字节流等存入byte数组,可以使用的方法有三个:

1、write(int b)

2、write(byte b[], int off, int len)  参数off:数组b开始存入的位置  参数len:存入长度

3、writeTo(OutputStream out)

 /**     * Writes the specified byte to this byte array output stream.      *     * @param   b   the byte to be written.     */    public synchronized void write(int b) {int newcount = count + 1;if (newcount > buf.length) {            buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));}buf[count] = (byte)b;count = newcount;    }    /**     * Writes <code>len</code> bytes from the specified byte array      * starting at offset <code>off</code> to this byte array output stream.     *     * @param   b     the data.     * @param   off   the start offset in the data.     * @param   len   the number of bytes to write.     */    public synchronized void write(byte b[], int off, int len) {if ((off < 0) || (off > b.length) || (len < 0) ||            ((off + len) > b.length) || ((off + len) < 0)) {    throw new IndexOutOfBoundsException();} else if (len == 0) {    return;}        int newcount = count + len;        if (newcount > buf.length) {            buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));        }        System.arraycopy(b, off, buf, count, len);        count = newcount;    }    /**     * Writes the complete contents of this byte array output stream to      * the specified output stream argument, as if by calling the output      * stream's write method using <code>out.write(buf, 0, count)</code>.     *     * @param      out   the output stream to which to write the data.     * @exception  IOException  if an I/O error occurs.     */    public synchronized void writeTo(OutputStream out) throws IOException {out.write(buf, 0, count);    }
源码中很重要的一部分是关于数组自动增长的内容,也就是随着存入的字节数而增加数组的大小。

用了两个主要的方法:

1、Arrays.copyOf(T[] original, int newLength)

2、System.arraycopy(Object src,  int  srcPos, Object dest, int destPos,int length);


关于System.arraycopy(Object src,  int  srcPos, Object dest, int destPos,int length);

从一个源array那里复制一个起点为指定位置的array,放置到目标array的一个指定位置上

复制而来的array来源于src参数,目标array则是dest参数,复制而来的内容长度由参数length决定,

显而易见,【源array的srcPos到srcPos+length-1的内容】被复制到【目标array的destPos到destPos+length-1的位置】上.

作为学习笔记,列出几个可能常见的异常:

<p>     * Otherwise, if any of the following is true, an     * <code>IndexOutOfBoundsException</code> is     * thrown and the destination is not modified:     * <ul>     * <li>The <code>srcPos</code> argument is negative.     * <li>The <code>destPos</code> argument is negative.     * <li>The <code>length</code> argument is negative.     * <li><code>srcPos+length</code> is greater than     *     <code>src.length</code>, the length of the source array.     * <li><code>destPos+length</code> is greater than     *     <code>dest.length</code>, the length of the destination array.     * </ul>     * <p>


关于Arrays.copyOf(T[] original, int newLength)

  public static <T> T[] copyOf(T[] original, int newLength) {        return (T[]) copyOf(original, newLength, original.getClass());    }
 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {        T[] copy = ((Object)newType == (Object)Object[].class)            ? (T[]) new Object[newLength]            : (T[]) Array.newInstance(newType.getComponentType(), newLength);        System.arraycopy(original, 0, copy, 0,                         Math.min(original.length, newLength));        return copy;    }
由源码可知,Arrays.copyOf里是用到了System.arraycopy来扩展自己数组长度的

这个方法会按照newType参数来新建一个长度为newLength的T类型数组,并将源数组从目标数组的0位置开始放置(如果目标数组设置大小还比源数组小,那么源数组就被截取之后放置到目标数组了)。





0 0
原创粉丝点击