一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - ByteArrayOutputStream簡介和使用_動力節點Java學院整理

ByteArrayOutputStream簡介和使用_動力節點Java學院整理

2020-09-27 15:35動力節點 Java教程

ByteArrayOutputStream 是字節數組輸出流。它繼承于OutputStream。這篇文章主要介紹了ByteArrayOutputStream簡介和使用,需要的朋友可以參考下

ByteArrayOutputStream 介紹

ByteArrayOutputStream 是字節數組輸出流。它繼承于OutputStream。

ByteArrayOutputStream 中的數據被寫入一個 byte 數組。緩沖區會隨著數據的不斷寫入而自動增長。可使用 toByteArray() 和 toString() 獲取數據。

OutputStream 函數列表

我們來看看ByteArrayOutputStream的父類OutputStream的函數接口。

java" id="highlighter_725665">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 構造函數
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) 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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) 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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): 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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();
    }
  }
}

運行結果:

?
1
2
3
4
5
baos=ABC
baos=ABCdefgh
size=8
str=ABCdefgh
baos2=ABCdefgh

以上所述是小編給大家介紹的ByteArrayOutputStream簡介和使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本wwxx| 美女逼逼喷水 | 国产一区二区免费视频 | 国产精品日韩欧美一区二区三区 | 久久久久久久电影 | 五月最新女厕所高跟嘘嘘 | 九九在线精品视频 | 2020年最新国产精品视频免费 | 午夜性爽视频男人的天堂在线 | 翁熄性放纵交换01 | 精品国产人妻国语 | 亚洲精品国产专区91在线 | a级亚洲片精品久久久久久久 | 秋葵丝瓜茄子草莓榴莲樱桃 | 桥本有菜在线四虎福利网 | 欧美高清片 | 出差上的少妇20p | 青春学堂在线观看 | 精品免费视频 | 天堂资源8中文最新版 | 亚洲乱人伦在线 | 黄 色 成 年人在线 幻女free性俄罗斯第一次摘花 | 暖暖 免费 高清 日本 在线1 | 国产精品二区高清在线 | 99ri在线视频网 | 久久99影院 | 黄a 大片a v 永久免费 | 欧美一级专区免费大片俄罗斯 | 校花被老头夺去第一次动图 | 日韩毛片免费 | 91亚洲精品丁香在线观看 | 日本花季传媒2020旧版安卓 | 激情五色月 | 欧美成人免费观看国产 | 精品亚洲欧美中文字幕在线看 | 狠狠做五月深爱婷婷天天综合 | 午夜神器老司机高清无码 | 国产成+人+综合+欧美 亚洲 | 精品久久日日躁夜夜躁AV | 国产v在线在线观看羞羞答答 | 欧美亚洲天堂网 |