最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ByteArrayOutputStream簡介和使用_動力節(jié)點Java學(xué)院整理

 更新時間:2017年05月08日 14:48:24   投稿:mrr  
ByteArrayOutputStream 是字節(jié)數(shù)組輸出流。它繼承于OutputStream。這篇文章主要介紹了ByteArrayOutputStream簡介和使用,需要的朋友可以參考下

ByteArrayOutputStream 介紹

ByteArrayOutputStream 是字節(jié)數(shù)組輸出流。它繼承于OutputStream。

ByteArrayOutputStream 中的數(shù)據(jù)被寫入一個 byte 數(shù)組。緩沖區(qū)會隨著數(shù)據(jù)的不斷寫入而自動增長??墒褂?toByteArray() 和 toString() 獲取數(shù)據(jù)。

OutputStream 函數(shù)列表

我們來看看ByteArrayOutputStream的父類OutputStream的函數(shù)接口。

// 構(gòu)造函數(shù)
OutputStream()
     void  close()
     void  flush()
     void  write(byte[] buffer, int offset, int count)
     void  write(byte[] buffer)
abstract void  write(int oneByte)
ByteArrayOutputStream 函數(shù)列表
 // 構(gòu)造函數(shù)
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的源碼,然后再學(xué)ByteArrayOutputStream的源碼。

1. OutputStream.java源碼分析(基于jdk1.7.40) 

package java.io;
 public abstract class OutputStream implements Closeable, Flushable {
   // 將字節(jié)b寫入到“輸出流”中。
   // 它在子類中實現(xiàn)!
   public abstract void write(int b) throws IOException;
   // 寫入字節(jié)數(shù)組b到“字節(jié)數(shù)組輸出流”中。
   public void write(byte b[]) throws IOException {
    write(b, 0, b.length);
   }
   // 寫入字節(jié)數(shù)組b到“字節(jié)數(shù)組輸出流”中,并且off是“數(shù)組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 {
    // 保存“字節(jié)數(shù)組輸出流”數(shù)據(jù)的數(shù)組
    protected byte buf[];
   // “字節(jié)數(shù)組輸出流”的計數(shù)
   protected int count;
   // 構(gòu)造函數(shù):默認創(chuàng)建的字節(jié)數(shù)組大小是。
   public ByteArrayOutputStream() {
     this(32);
   }
   // 構(gòu)造函數(shù):創(chuàng)建指定數(shù)組大小的“字節(jié)數(shù)組輸出流”
   public ByteArrayOutputStream(int size) {
     if (size < 0) {
       throw new IllegalArgumentException("Negative initial size: "
                         + size);
     }
     buf = new byte[size];
   }
   // 確認“容量”。
   // 若“實際容量 < minCapacity”,則增加“字節(jié)數(shù)組輸出流”的容量
   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”的大小,并選取其中較大的數(shù)為“新的容量”。
     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);
   }
   // 寫入一個字節(jié)b到“字節(jié)數(shù)組輸出流”中,并將計數(shù)+1
  public synchronized void write(int b) {
     ensureCapacity(count + 1);
     buf[count] = (byte) b;
     count += 1;
   }
   // 寫入字節(jié)數(shù)組b到“字節(jié)數(shù)組輸出流”中。off是“寫入字節(jié)數(shù)組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到“字節(jié)數(shù)組輸出流”中。
   public synchronized void writeTo(OutputStream out) throws IOException {
     out.write(buf, 0, count);
   }
   // 重置“字節(jié)數(shù)組輸出流”的計數(shù)。
   public synchronized void reset() {
     count = 0;
   }
   // 將“字節(jié)數(shù)組輸出流”轉(zhuǎn)換成字節(jié)數(shù)組。
   public synchronized byte toByteArray()[] {
     return Arrays.copyOf(buf, count);
   }
   // 返回“字節(jié)數(shù)組輸出流”當(dāng)前計數(shù)值
   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實際上是將字節(jié)數(shù)據(jù)寫入到“字節(jié)數(shù)組”中去。

(01) 通過ByteArrayOutputStream()創(chuàng)建的“字節(jié)數(shù)組輸出流”對應(yīng)的字節(jié)數(shù)組大小是32。

(02) 通過ByteArrayOutputStream(int size) 創(chuàng)建“字節(jié)數(shù)組輸出流”,它對應(yīng)的字節(jié)數(shù)組大小是size。

(03) write(int oneByte)的作用將int類型的oneByte換成byte類型,然后寫入到輸出流中。

(04) write(byte[] buffer, int offset, int len) 是將字節(jié)數(shù)組buffer寫入到輸出流中,offset是從buffer中讀取數(shù)據(jù)的起始偏移位置,len是讀取的長度。

(05) writeTo(OutputStream out) 將該“字節(jié)數(shù)組輸出流”的數(shù)據(jù)全部寫入到“輸出流out”中。 

示例代碼

關(guān)于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;
   // 對應(yīng)英文字母“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測試函數(shù)
   */
   private static void tesByteArrayOutputStream() {
     // 創(chuàng)建ByteArrayOutputStream字節(jié)流
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     // 依次寫入“A”、“B”、“C”三個字母。0x41對應(yīng)A,0x42對應(yīng)B,0x43對應(yīng)C。
     baos.write(0x41);
     baos.write(0x42);
    baos.write(0x43);
    System.out.printf("baos=%s\n", baos);
    // 將ArrayLetters數(shù)組中從“3”開始的后5個字節(jié)寫入到baos中。
    // 即對應(yīng)寫入“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);
     // 轉(zhuǎn)換成byte[]數(shù)組
     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();
     }
   }
 }

運行結(jié)果:

baos=ABC
baos=ABCdefgh
size=8
str=ABCdefgh
baos2=ABCdefgh

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

相關(guān)文章

  • Springboot?對接支付寶實現(xiàn)掃碼支付功能

    Springboot?對接支付寶實現(xiàn)掃碼支付功能

    本文介紹了如何在Spring?Boot項目中實現(xiàn)支付寶支付功能,包括沙箱環(huán)境配置、依賴引入、配置參數(shù)、訂單類定義、測試接口編寫等步驟,通過不同場景下的請求方式(PC端、二維碼、回調(diào)處理、定時查詢支付結(jié)果),展示了如何與支付寶API進行交互,感興趣的朋友一起看看吧
    2025-03-03
  • 詳解SpringMVC的url-pattern配置及原理剖析

    詳解SpringMVC的url-pattern配置及原理剖析

    這篇文章主要介紹了SpringMVC的url-pattern配置及原理剖析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Mybatis plus關(guān)閉駝峰命名的四種方法(防止出現(xiàn)查詢?yōu)镹ull)

    Mybatis plus關(guān)閉駝峰命名的四種方法(防止出現(xiàn)查詢?yōu)镹ull)

    這篇文章主要介紹了Mybatis plus關(guān)閉駝峰命名的四種方法(防止出現(xiàn)查詢?yōu)镹ull),數(shù)據(jù)庫的字段命名方式為使用下劃線連接,對應(yīng)的實體類應(yīng)該是駝峰命名方式,而我使用的是和數(shù)據(jù)庫同樣的命名方式,需要的朋友可以參考下
    2022-01-01
  • spring應(yīng)用中多次讀取http post方法中的流遇到的問題

    spring應(yīng)用中多次讀取http post方法中的流遇到的問題

    這篇文章主要介紹了spring應(yīng)用中多次讀取http post方法中的流,文中給大家列舉處理問題描述及解決方法,需要的朋友可以參考下
    2018-11-11
  • Spring Security中successHandler無效問題及解決

    Spring Security中successHandler無效問題及解決

    這篇文章主要介紹了Spring Security中successHandler無效問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • springboot如何配置定時任務(wù)

    springboot如何配置定時任務(wù)

    這篇文章主要介紹了springboot如何配置定時任務(wù),幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • SpringBoot對靜態(tài)資源的映射規(guī)則詳解解讀

    SpringBoot對靜態(tài)資源的映射規(guī)則詳解解讀

    這篇文章主要介紹了SpringBoot對靜態(tài)資源的映射規(guī)則詳解解讀,在Spring Boot中,映射規(guī)則是用來定義URL與控制器方法之間的映射關(guān)系的,通過映射規(guī)則,可以將特定的URL請求映射到相應(yīng)的控制器方法上,從而實現(xiàn)請求的處理和響應(yīng)的返回,需要的朋友可以參考下
    2023-10-10
  • Java8新特性之Collectors.joining()實例詳解

    Java8新特性之Collectors.joining()實例詳解

    在項目中我們常常要對list集合的數(shù)據(jù)做一些字符串拼接/處理等相關(guān)操作,下面這篇文章主要給大家介紹了關(guān)于Java8新特性之Collectors.joining()的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • Java出現(xiàn)中文亂碼問題分析及解決方案

    Java出現(xiàn)中文亂碼問題分析及解決方案

    在Java開發(fā)中,處理中文亂碼是一個常見的問題,由于字符集和編碼的復(fù)雜性,開發(fā)者可能面臨各種導(dǎo)致亂碼的情況,正確地處理中文字符集對于確保應(yīng)用程序的可靠性和國際化至關(guān)重要,本文給大家介紹了Java中文亂碼分析及解決方案,需要的朋友可以參考下
    2024-02-02
  • 詳細講述Java中的對象轉(zhuǎn)型

    詳細講述Java中的對象轉(zhuǎn)型

    在本篇文章里我們給大家詳細分享了關(guān)于Java中的對象轉(zhuǎn)型的知識點內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2018-10-10

最新評論

渭南市| 青冈县| 怀宁县| 芷江| 花莲县| 舞钢市| 红河县| 扶沟县| 喀什市| 元朗区| 谢通门县| 富宁县| 砚山县| 桂东县| 秦安县| 唐河县| 固安县| 友谊县| 灵丘县| 密云县| 东源县| 安溪县| 中江县| 灵宝市| 怀柔区| 安吉县| 盐池县| 揭阳市| 延川县| 桑植县| 嘉定区| 阳朔县| 陇西县| 旅游| 曲阳县| 海宁市| 镇康县| 巴里| 芦山县| 陆丰市| 固镇县|