Java - ByteArrayInputStream read() 方法



描述

Java ByteArrayInputStream read(byte[] b, int off, int len) 方法從該輸入流讀取 len 個位元組的資料到一個位元組陣列中。read() 方法不會阻塞。

宣告

以下是 java.io.ByteArrayInputStream.read(byte[] b, int off, int len) 方法的宣告:

public int read(byte[] b, int off, int len)

引數

  • b - 資料將讀取到此緩衝區中

  • off - 目標陣列 b 的起始偏移量

  • len - 讀取的最大位元組數

返回值

讀取到緩衝區的位元組數。如果流已到達末尾,則返回 -1。

異常

  • NullPointerException - 如果 b 為 null。

  • IndexOutOfBoundsException - 如果 len 大於偏移量 off 後的輸入流長度,或者 off 為負數,或者 len 為負數。

示例 1

以下示例演示了 Java ByteArrayInputStream read() 方法的用法。我們建立了一個名為 buf 的 byte[] 變數,並初始化了一些位元組。我們建立了一個 ByteArrayInputStream 引用,然後用 buf 變數對其進行初始化。我們建立了一個 4 位元組的緩衝區,並使用 read() 方法將位元組陣列流讀取到緩衝區中。然後迭代緩衝區以列印讀取的位元組。

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteStreamTest {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
      
         // create buffer
         byte[] b = new byte[4];
         int num = bais.read(b, 2, 2);
         
         // number of bytes read
         System.out.println("Bytes read: "+num);
         
         // for each byte in a buffer
         for (byte s :b) {
         
            // covert byte to char
            char c = (char)s;
            
            // prints byte
            System.out.print(s);
            
            if(s == 0)
               
               // if byte is 0
               System.out.println(": Null");
            else
               
               // if byte is not 0
               System.out.println(": "+c);
         }
         
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }   
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Bytes read: 2
0: Null
0: Null
65: A
66: B

示例 2

以下示例演示了 Java ByteArrayInputStream available() 方法的用法。我們建立了一個名為 buf 的 byte[] 變數,並用空陣列對其進行初始化。我們建立了一個 ByteArrayInputStream 引用,然後用 buf 變數對其進行初始化。我們建立了一個 4 位元組的緩衝區,並使用 read() 方法將位元組陣列流讀取到緩衝區中。在 if 迴圈中,我們使用 available() 方法檢查流是否包含任何位元組,然後列印其結果。

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteStreamTest {
   public static void main(String[] args) throws IOException {
      byte[] buf = {};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         // create buffer
         byte[] b = new byte[4];
         int num = bais.read(b, 2, 2);
         
         // number of bytes read
         System.out.println("Bytes read: "+num);
         int value = 0;
         // read the stream
         if((value = bais.read())!=-1) {
            // convert byte to character
            char c = (char)value;
            
            // print
            System.out.println("byte :"+value+"; char : "+ c);
         }else{
            System.out.print("byte stream is empty");
         } 
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

Bytes read: -1
byte stream is empty
java_bytearrayinputstream.htm
廣告