Java ByteArrayInputStream 类详细用法及实例

本教程通过示例介绍 Java ByteArrayInputStream 类以及使用方法。

本教程通过示例介绍 Java ByteArrayInputStream 类以及使用方法。

java.io 包的 ByteArrayInputStream 类可用于从字节数组中读取数据。

ByteArrayInputStream 类扩展了 InputStream 抽象类。

ByteArrayInputStream 是 Java InputStream 的子类。
Java ByteArrayInputStream 类

ByteArrayInputStream 中,输入流是使用字节数组创建的。它内部使用数组存储给定的字节数组的数据。

创建 ByteArrayInputStream

ByteArrayInputStream 类位于 java.io 包中,ByteArrayInputStream 类提供了构造方法创建对象。如下:

ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr);

在这里,我们创建了一个从 arr 数组中读取全部数据的输入流。但是,我们也可以创建从数组中读取部分数据的输入流。如下:

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) - 读取从 start 指定的开始的长度为 length 的字节数据到指定的数组中

示例:ByteArrayInputStream 读取数据

import java.io.ByteArrayInputStream;

public class Main {
  public static void main(String[] args) {
    // 字节数据
    byte[] array = {1, 2, 3, 4};

    try {
      ByteArrayInputStream input = new ByteArrayInputStream(array);
      System.out.print("从输入流中读取的字节数据: ");

      for(int i= 0; i < array.length; i++) {
        // 读取字节
        int data = input.read();
        System.out.print(data + ", ");
      }
      input.close();
    } catch(Exception e) {
      e.getStackTrace();
    }
  }
}

输出

从输入流中读取的字节数据: 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[]) {
    byte[] array = { 1, 2, 3, 4 };

    try {
      ByteArrayInputStream input = new ByteArrayInputStream(array);
      System.out.println("Available bytes at the beginning: " + input.available());

      input.read();
      input.read();

      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. 然后,我们使用了 2 次 read() 方法从输入流中读取 2 个字节。
  3. 最后,我们再次检查了可用字节。这次可用字节减少了 2 个。

skip() 方法

skip() 方法跳过指定数量的字节。例如,

import java.io.ByteArrayInputStream;

public class Main {
  public static void main(String args[]) {
    byte[] array = { 1, 2, 3, 4 };
    try {
      ByteArrayInputStream input = new ByteArrayInputStream(array);

      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();
      }

      input.close();
    }

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

输出

Input stream after skipping 2 bytes: 3、4、

在上面的例子中,我们使用了 skip() 方法从输入流的当前位置中跳过 2 个字节的数据。因此在输入流中读取不到 1 和 2 了。

close() 方法

ByteArrayInputStream 类的 close() 方法是个空方法,没有任何实际操作。因此,调用 close() 方法之后,我们仍然可以使用这个类的方法对输入流进行操作。

ByteArrayInputStream 的其他方法

方法 说明
finalize() 确保销毁对象的时候 close() 方法被调用
mark() 标记输入流中已读取数据的位置
reset() 将控制返回到输入流中设置标记的点
markSupported() 检查输入流是否支持 mark()reset()