当前位置: 移动技术网 > IT编程>开发语言>Java > ByteArrayOutputStream简介和使用_动力节点Java学院整理

ByteArrayOutputStream简介和使用_动力节点Java学院整理

2019年07月22日  | 移动技术网IT编程  | 我要评论
bytearrayoutputstream 介绍 bytearrayoutputstream 是字节数组输出流。它继承于outputstream。 bytearrayo

bytearrayoutputstream 介绍

bytearrayoutputstream 是字节数组输出流。它继承于outputstream。

bytearrayoutputstream 中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 tobytearray() 和 tostring() 获取数据。

outputstream 函数列表

我们来看看bytearrayoutputstream的父类outputstream的函数接口。

// 构造函数
outputstream()
     void  close()
     void  flush()
     void  write(byte[] buffer, int offset, int count)
     void  write(byte[] buffer)
abstract void  write(int onebyte)
bytearrayoutputstream 函数列表
 // 构造函数
bytearrayoutputstream()
bytearrayoutputstream(int size)
       void  close()
synchronized void  reset()
       int   size()
synchronized byte[] tobytearray()
       string tostring(int hibyte)
       string tostring(string charsetname)
       string tostring()
synchronized void  write(byte[] buffer, int offset, int len)
synchronized void  write(int onebyte)
synchronized void  writeto(outputstream out)

outputstream和bytearrayoutputstream源码分析

outputstream是bytearrayoutputstream的父类,我们先看看outputstream的源码,然后再学bytearrayoutputstream的源码。

1. outputstream.java源码分析(基于jdk1.7.40) 

package java.io;
 public abstract class outputstream implements closeable, flushable {
   // 将字节b写入到“输出流”中。
   // 它在子类中实现!
   public abstract void write(int b) throws ioexception;
   // 写入字节数组b到“字节数组输出流”中。
   public void write(byte b[]) throws ioexception {
    write(b, 0, b.length);
   }
   // 写入字节数组b到“字节数组输出流”中,并且off是“数组b的起始位置”,len是写入的长度
   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]);
     }
   }
   public void flush() throws ioexception {
   }
   public void close() throws ioexception {
   }
 }

2. bytearrayoutputstream 源码分析(基于jdk1.7.40) 

 package java.io;
  import java.util.arrays;
  public class bytearrayoutputstream extends outputstream {
    // 保存“字节数组输出流”数据的数组
    protected byte buf[];
   // “字节数组输出流”的计数
   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];
   }
   // 确认“容量”。
   // 若“实际容量 < mincapacity”,则增加“字节数组输出流”的容量
   private void ensurecapacity(int mincapacity) {
     // overflow-conscious code
     if (mincapacity - buf.length > 0)
       grow(mincapacity);
   }
   // 增加“容量”。
   private void grow(int mincapacity) {
     int oldcapacity = buf.length;
     // “新容量”的初始化 = “旧容量”x2
     int newcapacity = oldcapacity << 1;
     // 比较“新容量”和“mincapacity”的大小,并选取其中较大的数为“新的容量”。
     if (newcapacity - mincapacity < 0)
      newcapacity = mincapacity;
     if (newcapacity < 0) {
       if (mincapacity < 0) // overflow
         throw new outofmemoryerror();
       newcapacity = integer.max_value;
     }
     buf = arrays.copyof(buf, newcapacity);
   }
   // 写入一个字节b到“字节数组输出流”中,并将计数+1
  public synchronized void write(int b) {
     ensurecapacity(count + 1);
     buf[count] = (byte) b;
     count += 1;
   }
   // 写入字节数组b到“字节数组输出流”中。off是“写入字节数组b的起始位置”,len是写入的长度
   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;
   }
   // 写入输出流outb到“字节数组输出流”中。
   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;
   }
   public synchronized string tostring() {
     return new string(buf, 0, count);
   }
  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);
   }
   public void close() throws ioexception {
   }
 }

说明:

bytearrayoutputstream实际上是将字节数据写入到“字节数组”中去。

(01) 通过bytearrayoutputstream()创建的“字节数组输出流”对应的字节数组大小是32。

(02) 通过bytearrayoutputstream(int size) 创建“字节数组输出流”,它对应的字节数组大小是size。

(03) write(int onebyte)的作用将int类型的onebyte换成byte类型,然后写入到输出流中。

(04) write(byte[] buffer, int offset, int len) 是将字节数组buffer写入到输出流中,offset是从buffer中读取数据的起始偏移位置,len是读取的长度。

(05) writeto(outputstream out) 将该“字节数组输出流”的数据全部写入到“输出流out”中。 

示例代码

关于bytearrayoutputstream中api的详细用法,参考示例代码(bytearrayoutputstreamtest.java): 

 import java.io.ioexception;
 import java.io.outputstream;
 import java.io.bytearrayoutputstream;
 import java.io.bytearrayinputstream;
 /**
  * bytearrayoutputstream 测试程序
  *
  * 
 */
 public class bytearrayoutputstreamtest {
   private static final int len = 5;
   // 对应英文字母“abcddefghijklmnopqrsttuvwxyz”
   private static final byte[] arrayletters = {
     0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
     0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a
   };
   public static void main(string[] args) {
     //string tmp = new string(arrayletters);
     //system.out.println("arrayletters="+tmp);
     tesbytearrayoutputstream() ;
   }
   /**
   * bytearrayoutputstream的api测试函数
   */
   private static void tesbytearrayoutputstream() {
     // 创建bytearrayoutputstream字节流
     bytearrayoutputstream baos = new bytearrayoutputstream();
     // 依次写入“a”、“b”、“c”三个字母。0x41对应a,0x42对应b,0x43对应c。
     baos.write(0x41);
     baos.write(0x42);
    baos.write(0x43);
    system.out.printf("baos=%s\n", baos);
    // 将arrayletters数组中从“3”开始的后5个字节写入到baos中。
    // 即对应写入“0x64, 0x65, 0x66, 0x67, 0x68”,即“defgh”
     baos.write(arrayletters, 3, 5);
    system.out.printf("baos=%s\n", baos);
     // 计算长度
     int size = baos.size();
     system.out.printf("size=%s\n", size);
     // 转换成byte[]数组
     byte[] buf = baos.tobytearray();
     string str = new string(buf);
     system.out.printf("str=%s\n", str);
     // 将baos写入到另一个输出流中
     try {
       bytearrayoutputstream baos2 = new bytearrayoutputstream();
       baos.writeto((outputstream)baos2);
       system.out.printf("baos2=%s\n", baos2);
     } catch (ioexception e) {
       e.printstacktrace();
     }
   }
 }

运行结果:

baos=abc
baos=abcdefgh
size=8
str=abcdefgh
baos2=abcdefgh

以上所述是小编给大家介绍的bytearrayoutputstream简介和使用,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网