亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java基礎(chǔ)知識之ByteArrayInputStream流的使用

 更新時間:2021年12月13日 15:00:31   作者:咕嚕是個大胖子  
這篇文章主要介紹了Java基礎(chǔ)知識之ByteArrayInputStream流的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Java ByteArrayInputStream流

一、ByteArrayInputStream流定義

API說明:ByteArrayInputStream包含一個內(nèi)部緩沖區(qū),其中包含可以從流中讀取的字節(jié),內(nèi)部計數(shù)器跟蹤read方法提供的下一個字節(jié),關(guān)閉ByteArrayInputStream流無效,關(guān)閉流后調(diào)用類的方法不會有異常產(chǎn)生

二、ByteArrayInputStream流實例域

 /**
     * 字節(jié)數(shù)組緩沖區(qū),buf[0]到buf[count-1]是可以從流中讀取的字節(jié),buf[pos]是讀取的下一字節(jié)
     */
    protected byte buf[];
 
    /**
     *讀取字節(jié)的索引
     */
    protected int pos;
 
    /**
     * 流中當(dāng)前標(biāo)記的位置,默認(rèn)標(biāo)記為0,可以通過mark方法設(shè)置新的標(biāo)記點,而后通過reset方法將當(dāng)前位置設(shè)置為標(biāo)記點
     * 從標(biāo)記點開始讀取數(shù)據(jù)
     *
     * @since   JDK1.1
     */
    protected int mark = 0;
 
    /**
     * 索引結(jié)束位置+1,不大于緩沖區(qū)的長度
     */
    protected int count;

三、ByteArrayInputStream流構(gòu)造函數(shù)

 /**
     * 使用指定字節(jié)數(shù)組創(chuàng)建ByteArrayInputStream流,字節(jié)數(shù)組為流的緩沖區(qū),
     * 當(dāng)前位置索引pos初始值是0,索引結(jié)束位置count的是buf的長度
     */
    public ByteArrayInputStream(byte buf[]) {
        this.buf = buf;
        this.pos = 0;
        this.count = buf.length;
    } 
    /**
     * 使用指定的數(shù)組創(chuàng)建ByteArrayInputStream流
     * 目標(biāo)數(shù)組為流的緩沖區(qū)數(shù)組
     * 緩沖區(qū)當(dāng)前起始位置變量值為off
     * 緩沖區(qū)的索引結(jié)束位置為:buf.length和off+length的最小值
     */
    public ByteArrayInputStream(byte buf[], int offset, int length) {
        this.buf = buf;
        this.pos = offset;
        this.count = Math.min(offset + length, buf.length);
        this.mark = offset;
    }

四、ByteArrayInputStream流方法

1)read():從此輸入流中讀取下一個字節(jié)并返回,當(dāng)流到達(dá)末尾時,返回-1

 /**
     * 從此輸入流中讀取下一個字節(jié)并返回
     * 當(dāng)流到達(dá)末尾時,返回-1
     * 注意& 0xff是字節(jié)的補碼操作,暫時不用理會
     */
    public synchronized int read() {
        return (pos < count) ? (buf[pos++] & 0xff) : -1;
    }

2)read(byte b[], int off, int len):從輸入流中讀取最多l(xiāng)en個字節(jié)到目標(biāo)數(shù)組中,返回實際讀取的字節(jié)數(shù)

   /**
     * 從輸入流中讀取最多l(xiāng)en個字節(jié)到目標(biāo)數(shù)組中,返回實際讀取的字節(jié)數(shù)
     * 當(dāng)緩沖區(qū)中剩余字符數(shù)小于len個字節(jié)時,讀取緩沖區(qū)剩余字符數(shù)
     * 當(dāng)剩余字符數(shù)大于len個字節(jié)時,讀取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;
    }

3)close():關(guān)閉流無效,關(guān)閉后調(diào)用其它方法不會有異常

    /**
     * 關(guān)閉流無效,關(guān)閉后調(diào)用其它方法不會有異常
     */
    public void close() throws IOException {
    }

五、ByteArrayInputStream流的作用

暫時不理解具體作用,不清楚什么時候會用到該流,因為實際項目暫未用到,故先了解其功能即可

六、ByteArrayInputStream的用法解析

看下面這個程序,看懂了就會了

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
public class Test4 {
 //ByteArrayInputStream本身操作的是一個數(shù)組,并沒有打開文件描述之類的,所有不需要關(guān)閉流
 
 public static void main(String[] args) {
  ByteArrayInputStream bais=null;
  StringBuilder sb=new StringBuilder();
  int temp=0;
  int num=0;
  long date1=System.currentTimeMillis();
  try{
   byte[] b="abcdefghijklmnopqstuvxyz".getBytes();
   //從字符數(shù)組b中讀取數(shù)據(jù),從下標(biāo)為2開始計數(shù)讀8個
   bais=new ByteArrayInputStream(b,2,8);
   while((temp=bais.read())!=-1){
    sb.append((char)temp);
    num++;
   }
      System.out.println(sb);
      System.out.println("讀取的字節(jié)數(shù):"+num);
  }finally{
   try{
    bais.close();//不需要關(guān)閉流的,但是調(diào)用close沒有任何影響,close不做任何事情
   }catch(IOException e){
    e.printStackTrace();
   }
   new File("d:"+File.separator+"a.txt");//File.separator是一個文件分隔符,在windows和linux平臺下運行都沒有問題
  }
  long date2=System.currentTimeMillis();
  System.out.println("耗時:"+(date2-date1)); 
 } 
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論