Java ByteArrayInputStream 类

java.io包中的ByteArrayInputStream类可用于读取输入数据(以字节为单位)的数组

它扩展了InputStream抽象类。

The ByteArrayInputStream class extends the InputStream class.
Java ByteArrayInputStream

注意:在ByteArrayInputStream中,输入流是使用字节数组创建的。它包含一个内部数组来存储该特定字节数组的数据。


创建 ByteArrayInputStream

要创建字节数组输入流,我们必须先导入java.io.ByteArrayInputStream包。导入包后,我们就可以创建输入流了。

// Creates a ByteArrayInputStream that reads entire array
ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr);

在这里,我们创建了一个输入流,它从arr数组读取所有数据。但是,我们也可以创建仅从数组读取部分数据的输入流。

// Creates a ByteArrayInputStream that reads a portion of array
ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr, int start, int length);

这里输入流从 start 位置开始,从数组读取等于 length 的字节数。


ByteArrayInputStream 的方法

ByteArrayInputStream类提供了InputStream类中不同方法的实现。

read() 方法

  • read() - 从输入流中的数组读取单个字节
  • read(byte[] array) - 从输入流中读取字节并存储在指定的数组中
  • read(byte[] array, int start, int length) - 从流中读取等于 length 的字节数,并从 start 位置开始存储在指定的数组中

示例:使用 ByteArrayInputStream 读取数据

import java.io.ByteArrayInputStream;

public class Main {
  public static void main(String[] args) {

    // Creates an array of byte
    byte[] array = {1, 2, 3, 4};

    try {
      ByteArrayInputStream input = new ByteArrayInputStream(array);

      System.out.print("The bytes read from the input stream: ");

      for(int i= 0; i < array.length; i++) {

        // Reads the bytes
        int data = input.read();
        System.out.print(data + ", ");
      }
      input.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

输出

The bytes read from the input stream: 1, 2, 3, 4,

在上面的示例中,我们创建了一个名为input的字节数组输入流。

ByteArrayInputStream input = new ByteArrayInputStream(array);

这里,输入流包含来自指定数组的所有数据。要从输入流读取数据,我们使用了read()方法。


available() 方法

要获取输入流中可用字节的数量,我们可以使用available()方法。例如:

import java.io.ByteArrayInputStream;

public class Main {

  public static void main(String args[]) {

    // Creates an array of bytes
    byte[] array = { 1, 2, 3, 4 };

    try {
      ByteArrayInputStream input = new ByteArrayInputStream(array);

      // Returns the available number of bytes
      System.out.println("Available bytes at the beginning: " + input.available());

      // Reads 2 bytes from the input stream
      input.read();
      input.read();

      // Returns the available number of bytes
      System.out.println("Available bytes at the end: " + input.available());

      input.close();
    }

    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

输出

Available bytes at the beginning: 4
Available bytes at the end: 2

在上面的例子中:

  1. 我们使用available()方法来检查输入流中可用字节的数量。
  2. 然后我们使用read()方法读取了2次,从输入流中读取了2个字节。
  3. 现在,在读取了2个字节后,我们检查了可用字节。这次可用字节数减少了2。

skip() 方法

要丢弃并跳过指定数量的字节,我们可以使用skip()方法。例如:

import java.io.ByteArrayInputStream;

public class Main {

  public static void main(String args[]) {

    // Create an array of bytes
    byte[] array = { 1, 2, 3, 4 };
    try {
      ByteArrayInputStream input = new ByteArrayInputStream(array);

      // Using the skip() method
      input.skip(2);
      System.out.print("Input stream after skipping 2 bytes: ");

      int data = input.read();
      while (data != -1) {
        System.out.print(data + ", ");
        data = input.read();
      }

      // close() method
      input.close();
    }

    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

输出

Input stream after skipping 2 bytes: 3, 4,

在上面的示例中,我们使用 skip() 方法跳过了输入流中的2个字节数据。因此,输入流未读取 12


close() 方法

要关闭输入流,我们可以使用close()方法。

但是,close()方法在ByteArrayInputStream类中没有效果。即使在调用close()方法后,我们仍然可以使用此类的其他方法。


ByteArrayInputStream 的其他方法

方法 描述
finalize() 确保调用close()方法
mark() 标记输入流中已读取数据的为止
reset() 将控制返回到输入流中标记设置点
markSupported() 检查输入流是否支持mark()reset()

要了解更多信息,请访问Java ByteArrayInputStream (官方Java文档)

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战