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位置开始放置(如果目标数组设置大小还比源数组小,那么源数组就被截取之后放置到目标数组了)。
- ByteArrayOutput类和ByteArrayInput类
- 数据流DataInput(Output)Stream 和 字节数组流 ByteArrayInput(Output) Stream
- Java之数据流DataInput(Output)Stream 和 字节数组流 ByteArrayInput(Output) Stream的嵌套
- 虚函数和抽象类和接口
- 类和对象的概念和区别
- 继承和组合、抽象类和接口
- final和抽象类和接口
- 类和对象、封装和继承
- 类和对象的属性和方法
- String和StringBuilder类方法和用法
- 接口和抽象类概念和区别
- 类和对象的定义和使用
- 定时器和BOM对象和Date类
- 类和类装入
- 类和元类
- 伪类和类
- 类和元类
- 类和派生类
- JS操作Json的经验
- iOS开发-简单图片背景替换(实现抠图效果)
- 我感觉自己很喜欢编程,也是一个勤于思考的人
- 线上多服务管理工具实例剖析
- 【翻译自mos文章】oracle数据库中 基本的表压缩和高级压缩之间的区别
- ByteArrayOutput类和ByteArrayInput类
- ScrollView+ViewPager(上下滚动+自动切换图片)
- IOS 字符串中去除特殊符号 stringByTrimmingCharactersInSet 应该用于账号登录等
- 记录过往点滴,成就未来汪洋——开博纪念
- 如何自定义RadioButton样式
- 数据分析的步骤有哪些
- ********走进 WatchKit Framework***********
- 创建mysql分区表及操作
- C# aspx页面动态加载ascx用户控件 及 利用反射调用其内方法