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

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

 更新時間:2017年05月08日 14:37:03   投稿:mrr  
ByteArrayInputStream 是字節(jié)數(shù)組輸入流。它繼承于InputStream。這篇文章主要介紹了ByteArrayInputStream簡介和使用_動力節(jié)點Java學(xué)院整理,需要的朋友可以參考下

ByteArrayInputStream 介紹

ByteArrayInputStream 是字節(jié)數(shù)組輸入流。它繼承于InputStream。

它包含一個內(nèi)部緩沖區(qū),該緩沖區(qū)包含從流中讀取的字節(jié);通俗點說,它的內(nèi)部緩沖區(qū)就是一個字節(jié)數(shù)組,而ByteArrayInputStream本質(zhì)就是通過字節(jié)數(shù)組來實現(xiàn)的。

我們都知道,InputStream通過read()向外提供接口,供它們來讀取字節(jié)數(shù)據(jù);而ByteArrayInputStream 的內(nèi)部額外的定義了一個計數(shù)器,它被用來跟蹤 read() 方法要讀取的下一個字節(jié)。

InputStream 函數(shù)列表

// 構(gòu)造函數(shù)
InputStream()

       int   available()
       void  close()
       void  mark(int readlimit)
       boolean markSupported()
       int   read(byte[] buffer)
abstract   int   read()
       int   read(byte[] buffer, int offset, int length)
synchronized void  reset()
       long  skip(long byteCount)

ByteArrayInputStream 函數(shù)列表

// 構(gòu)造函數(shù)
ByteArrayInputStream(byte[] buf)
ByteArrayInputStream(byte[] buf, int offset, int length)
synchronized int     available()
       void    close()
synchronized void    mark(int readlimit)
       boolean   markSupported()
synchronized int     read()
synchronized int     read(byte[] buffer, int offset, int length)
synchronized void    reset()
synchronized long    skip(long byteCount)

InputStream和ByteArrayInputStream源碼分析

InputStream是ByteArrayInputStream的父類,我們先看看InputStream的源碼,然后再學(xué)ByteArrayInputStream的源碼。

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

package java.io;
 public abstract class InputStream implements Closeable {
   // 能skip的大小
   private static final int MAX_SKIP_BUFFER_SIZE = ;
   // 從輸入流中讀取數(shù)據(jù)的下一個字節(jié)。
   public abstract int read() throws IOException;
   // 將數(shù)據(jù)從輸入流讀入 byte 數(shù)組。
   public int read(byte b[]) throws IOException {
     return read(b, 0, b.length);
   } 
   // 將最多 len 個數(shù)據(jù)字節(jié)從此輸入流讀入 byte 數(shù)組。
   public int read(byte b[], int off, int len) throws IOException {
     if (b == null) {
       throw new NullPointerException();
     } else if (off < 0 || len < 0 || len > b.length - off) {
      throw new IndexOutOfBoundsException();
     } else if (len == 0) {
       return 0;
     } 
     int c = read();
     if (c == -1) {
       return -1;
    }
    b[off] = (byte)c; 
     int i = 1;
     try {
       for (; i < len ; i++) {
         c = read();
         if (c == -) {
           break;
         }
         b[off + i] = (byte)c;
       }
     } catch (IOException ee) {
     }
     return i;
   }
   // 跳過輸入流中的n個字節(jié)
   public long skip(long n) throws IOException {
     long remaining = n;
     int nr; 
     if (n <= 0) {
       return 0;
     } 
     int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
     byte[] skipBuffer = new byte[size];
     while (remaining > 0) {
       nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
      if (nr < 0) {
         break;
       }
       remaining -= nr;
     }
     return n - remaining;
   }
   public int available() throws IOException {
    return 0;
   }
   public void close() throws IOException {}
   public synchronized void mark(int readlimit) {}
   public synchronized void reset() throws IOException {
     throw new IOException("mark/reset not supported");
   }
   public boolean markSupported() {
     return false;
   }
 }

2. ByteArrayInputStream.java源碼分析(基于jdk1.7.40) 

package java.io;
  public class ByteArrayInputStream extends InputStream {
    // 保存字節(jié)輸入流數(shù)據(jù)的字節(jié)數(shù)組
    protected byte buf[];
    // 下一個會被讀取的字節(jié)的索引
    protected int pos;
   // 標(biāo)記的索引
   protected int mark = 0;
   // 字節(jié)流的長度
   protected int count;
   // 構(gòu)造函數(shù):創(chuàng)建一個內(nèi)容為buf的字節(jié)流
   public ByteArrayInputStream(byte buf[]) {
     // 初始化“字節(jié)流對應(yīng)的字節(jié)數(shù)組為buf”
     this.buf = buf;
     // 初始化“下一個要被讀取的字節(jié)索引號為”
     this.pos = ;
     // 初始化“字節(jié)流的長度為buf的長度”
     this.count = buf.length;
   }
   // 構(gòu)造函數(shù):創(chuàng)建一個內(nèi)容為buf的字節(jié)流,并且是從offset開始讀取數(shù)據(jù),讀取的長度為length
   public ByteArrayInputStream(byte buf[], int offset, int length) {
     // 初始化“字節(jié)流對應(yīng)的字節(jié)數(shù)組為buf”
     this.buf = buf;
     // 初始化“下一個要被讀取的字節(jié)索引號為offset”
     this.pos = offset;
     // 初始化“字節(jié)流的長度”
     this.count = Math.min(offset + length, buf.length);
     // 初始化“標(biāo)記的字節(jié)流讀取位置”
     this.mark = offset;
   }
   // 讀取下一個字節(jié)
   public synchronized int read() {
     return (pos < count) ? (buf[pos++] & 0xff) : -1;
   }
   // 將“字節(jié)流的數(shù)據(jù)寫入到字節(jié)數(shù)組b中”
   // off是“字節(jié)數(shù)組b的偏移地址”,表示從數(shù)組b的off開始寫入數(shù)據(jù)
   // len是“寫入的字節(jié)長度”
   public synchronized int read(byte b[], int off, int len) {
     if (b == null) {
       throw new NullPointerException();
     } else if (off < 0 || len < 0 || len > b.length - off) {
       throw new IndexOutOfBoundsException();
     }
     if (pos >= count) {
      return -1;
     }
     int avail = count - pos;
     if (len > avail) {
       len = avail;
     }
     if (len <= 0) {
      return 0;
     }
     System.arraycopy(buf, pos, b, off, len);
     pos += len;
     return len;
   }
   // 跳過“字節(jié)流”中的n個字節(jié)。
   public synchronized long skip(long n) {
     long k = count - pos;
     if (n < k) {
       k = n < 0 ? 0 : n;
     }
     pos += k;
     return k;
   }
   // “能否讀取字節(jié)流的下一個字節(jié)”
   public synchronized int available() {
     return count - pos;
   }
   // 是否支持“標(biāo)簽”
   public boolean markSupported() {
     return true;
   }
   // 保存當(dāng)前位置。readAheadLimit在此處沒有任何實際意義
   public void mark(int readAheadLimit) {
     mark = pos;
   }
   // 重置“字節(jié)流的讀取索引”為“mark所標(biāo)記的位置”
   public synchronized void reset() {
     pos = mark;
   }
   public void close() throws IOException {
   }
 }

說明:

ByteArrayInputStream實際上是通過“字節(jié)數(shù)組”去保存數(shù)據(jù)。

(01) 通過ByteArrayInputStream(byte buf[]) 或 ByteArrayInputStream(byte buf[], int offset, int length) ,我們可以根據(jù)buf數(shù)組來創(chuàng)建字節(jié)流對象。

(02) read()的作用是從字節(jié)流中“讀取下一個字節(jié)”。

(03) read(byte[] buffer, int offset, int length)的作用是從字節(jié)流讀取字節(jié)數(shù)據(jù),并寫入到字節(jié)數(shù)組buffer中。offset是將字節(jié)寫入到buffer的起始位置,length是寫入的字節(jié)的長度。

(04) markSupported()是判斷字節(jié)流是否支持“標(biāo)記功能”。它一直返回true。

(05) mark(int readlimit)的作用是記錄標(biāo)記位置。記錄標(biāo)記位置之后,某一時刻調(diào)用reset()則將“字節(jié)流下一個被讀取的位置”重置到“mark(int readlimit)所標(biāo)記的位置”;也就是說,reset()之后再讀取字節(jié)流時,是從mark(int readlimit)所標(biāo)記的位置開始讀取。

示例代碼

關(guān)于ByteArrayInputStream中API的詳細(xì)用法,參考示例代碼(ByteArrayInputStreamTest.java):

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
 * ByteArrayInputStream 測試程序
 *
 */
public class ByteArrayInputStreamTest {
  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);
    tesByteArrayInputStream() ;
  }
  /**
   * ByteArrayInputStream的API測試函數(shù)
   */
  private static void tesByteArrayInputStream() {
    // 創(chuàng)建ByteArrayInputStream字節(jié)流,內(nèi)容是ArrayLetters數(shù)組
    ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);
    // 從字節(jié)流中讀取5個字節(jié)
    for (int i=0; i<LEN; i++) {
      // 若能繼續(xù)讀取下一個字節(jié),則讀取下一個字節(jié)
      if (bais.available() >= 0) {
        // 讀取“字節(jié)流的下一個字節(jié)”
        int tmp = bais.read();
        System.out.printf("%d : 0x%s\n", i, Integer.toHexString(tmp));
      }
    }
    // 若“該字節(jié)流”不支持標(biāo)記功能,則直接退出
    if (!bais.markSupported()) {
      System.out.println("make not supported!");
      return ;
    }
    // 標(biāo)記“字節(jié)流中下一個被讀取的位置”。即--標(biāo)記“0x66”,因為因為前面已經(jīng)讀取了5個字節(jié),所以下一個被讀取的位置是第6個字節(jié)”
    // (01), ByteArrayInputStream類的mark(0)函數(shù)中的“參數(shù)0”是沒有實際意義的。
    // (02), mark()與reset()是配套的,reset()會將“字節(jié)流中下一個被讀取的位置”重置為“mark()中所保存的位置”
    bais.mark(0);
    // 跳過5個字節(jié)。跳過5個字節(jié)后,字節(jié)流中下一個被讀取的值應(yīng)該是“0x6B”。
    bais.skip(5);
    // 從字節(jié)流中讀取5個數(shù)據(jù)。即讀取“0x6B, 0x6C, 0x6D, 0x6E, 0x6F”
    byte[] buf = new byte[LEN];
    bais.read(buf, 0, LEN);
    // 將buf轉(zhuǎn)換為String字符串?!?x6B, 0x6C, 0x6D, 0x6E, 0x6F”對應(yīng)字符是“klmno”
    String str1 = new String(buf);
    System.out.printf("str1=%s\n", str1);
    // 重置“字節(jié)流”:即,將“字節(jié)流中下一個被讀取的位置”重置到“mark()所標(biāo)記的位置”,即0x66。
    bais.reset();
    // 從“重置后的字節(jié)流”中讀取5個字節(jié)到buf中。即讀取“0x66, 0x67, 0x68, 0x69, 0x6A”
    bais.read(buf, 0, LEN);
    // 將buf轉(zhuǎn)換為String字符串?!?x66, 0x67, 0x68, 0x69, 0x6A”對應(yīng)字符是“fghij”
    String str2 = new String(buf);
    System.out.printf("str2=%s\n", str2);
  }
}

運行結(jié)果:

ArrayLetters=abcdefghijklmnopqrstuvwxyz
0 : 0x61
1 : 0x62
2 : 0x63
3 : 0x64
4 : 0x65
str1=klmno
str2=fghij

結(jié)果說明:

(01) ArrayLetters 是字節(jié)數(shù)組。0x61對應(yīng)的ASCII碼值是a,0x62對應(yīng)的ASCII碼值是b,依次類推...

(02) ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters); 這句話是創(chuàng)建“字節(jié)流bais”,它的內(nèi)容就是ArrayLetters。

(03) for (int i=0; i<LEN; i++) ; 這個for循環(huán)的作用就是從字節(jié)流中讀取5個字節(jié)。每次調(diào)用bais.read()就從字節(jié)流中讀取一個字節(jié)。

(04) bais.mark(0); 這句話就是“設(shè)置字節(jié)流的標(biāo)記”,此時標(biāo)記的位置對應(yīng)的值是0x66。

(05) bais.skip(5); 這句話是跳過5個字節(jié)。跳過5個字節(jié)后,對應(yīng)的字節(jié)流中下一個被讀取的字節(jié)的值是0x6B。

(06) bais.read(buf, 0, LEN); 這句話是“從字節(jié)流中讀取LEN個數(shù)據(jù)寫入到buf中,0表示從buf的第0個位置開始寫入”。

(07) bais.reset(); 這句話是將“字節(jié)流中下一個被讀取的位置”重置到“mark()所標(biāo)記的位置”,即0x66。

學(xué)完了ByteArrayInputStream輸入流。下面,我們學(xué)習(xí)與之對應(yīng)的輸出流ByteArrayOutputStream。

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

相關(guān)文章

  • Spring MVC 處理一個請求的流程

    Spring MVC 處理一個請求的流程

    Spring MVC是Spring系列框架中使用頻率最高的部分。不管是Spring Boot還是傳統(tǒng)的Spring項目,只要是Web項目都會使用到Spring MVC部分。因此程序員一定要熟練掌握MVC部分。本篇博客簡要分析Spring MVC處理一個請求的流程。
    2021-02-02
  • Java開發(fā)崗位面試被問到嵌套類怎么辦

    Java開發(fā)崗位面試被問到嵌套類怎么辦

    本篇文章主要介紹了深入理解Java嵌套類和內(nèi)部類,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-07-07
  • 探討Java驗證碼制作(下篇)

    探討Java驗證碼制作(下篇)

    這篇文章主要介紹了探討Java驗證碼制作(下篇)的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • 流讀取導(dǎo)致StringBuilder.toString()亂碼的問題及解決

    流讀取導(dǎo)致StringBuilder.toString()亂碼的問題及解決

    這篇文章主要介紹了流讀取導(dǎo)致StringBuilder.toString()亂碼的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 詳解Mybatis中的 ${} 和 #{}區(qū)別與用法

    詳解Mybatis中的 ${} 和 #{}區(qū)別與用法

    這篇文章主要介紹了Mybatis中的 ${} 和 #{}區(qū)別與用法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • sprintboot使用spring-security包,緩存內(nèi)存與redis共存方式

    sprintboot使用spring-security包,緩存內(nèi)存與redis共存方式

    這篇文章主要介紹了sprintboot使用spring-security包,緩存內(nèi)存與redis共存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot事務(wù)鉤子函數(shù)的使用方式

    SpringBoot事務(wù)鉤子函數(shù)的使用方式

    本文介紹了SpringBoot中事務(wù)鉤子函數(shù)的使用方式,包括常見場景、使用方式等,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • Java數(shù)據(jù)結(jié)構(gòu)之位圖的簡單實現(xiàn)和使用

    Java數(shù)據(jù)結(jié)構(gòu)之位圖的簡單實現(xiàn)和使用

    位圖,?是一種非常常見的結(jié)構(gòu),?它使用每個二進制位來存放一個值的狀態(tài),?就類似于?Java?當(dāng)中?HashSet?存儲元素的功能。本文主要來介紹一下位圖的簡單實現(xiàn)和使用,需要的可以參考一下
    2023-05-05
  • 一文弄懂Java中ThreadPoolExecutor

    一文弄懂Java中ThreadPoolExecutor

    ThreadPoolExecutor是Java中的一個線程池實現(xiàn),它可以管理和控制多個 Worker Threads,本文就詳細(xì)的介紹一下Java中ThreadPoolExecutor,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • 淺談Maven包沖突的原理及解決方法

    淺談Maven包沖突的原理及解決方法

    這篇文章主要介紹了淺談Maven包沖突的原理及解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評論

宁化县| 济宁市| 安国市| 云梦县| 鲁山县| 加查县| 昌都县| 启东市| 翁源县| 策勒县| 吉安县| 太湖县| 那坡县| 绥德县| 娄烦县| 二连浩特市| 仙游县| 富源县| 容城县| 麻江县| 红原县| 新丰县| 扎鲁特旗| 江永县| 桑日县| 花垣县| 肃宁县| 嘉定区| 义乌市| 合阳县| 富民县| 双城市| 乌兰浩特市| 吐鲁番市| 津南区| 会同县| 循化| 藁城市| 湖南省| 柘城县| 罗源县|