程序员潇然 发表于 2022-7-30 14:43:53

java 使用 ByteArrayInputStream对字节数组分页 分组

对于数据进行分页、分组是经常会遇到的场景,随着Java技术的发展,对于List对象现在可以借助于Lambda表达式进行,相对来说变得简单了很多。

但是对于字节数组`byte[] `,如果想分页,手动的话就会比较麻烦

而Java中很多便利的工具,比如lambda 表达式,对于基本类型操作又不方便

如果byte转换为Byte的话,就需要遍历,利用自动装箱等方式进行处理,很麻烦。

而对于字节数组,java IO中提供了ByteArrayInputStream,可以比较方便的对字节数组进行处理。

```java
package com.crazybytex.fragment.bytearraypage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author 程序员潇然 https://crazybytex.com/
* @Date 2022/7/29 17:32
* @Description 有的时候可能需要对字节数组进行分页, 手动分页比较啰嗦
* 可以借助于ByteArrayInputStream 写入缓冲区的形式 对数组进行分页
**/
public class ByteArrayPage {
    public static void main(String[] args) throws IOException {


      Path path = Paths.get("C:\\Users\\crazybytex\\Desktop\\aaaa.jpg");
      byte[] contents = Files.readAllBytes(path);

      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(contents);
      //缓冲区大小 (每个报文数据区大小)
      byte[] flush = new byte;

      int pageNum = 0;

      while ((byteArrayInputStream.read(flush)) != -1) {
            pageNum++;
            //写入
            //对数组做一些事情 flush;
      }

    }
}

```

`while` 循环中的`byte[] flush`的数值,就是随着读取不断变化的数组

### 注意

不过需要注意实际使用时,最后一个分组数据未满的情况,那样将会有残留的数据

比如记录每次读取的length,使用的时候根据每次读取到的length处理数据

或者每次处理之后,都清空`flush`数组

```java
/**
   * Reads some number of bytes from the input stream and stores them into
   * the buffer array <code>b</code>. The number of bytes actually read is
   * returned as an integer.This method blocks until input data is
   * available, end of file is detected, or an exception is thrown.
   *
   * <p> If the length of <code>b</code> is zero, then no bytes are read and
   * <code>0</code> is returned; otherwise, there is an attempt to read at
   * least one byte. If no byte is available because the stream is at the
   * end of the file, the value <code>-1</code> is returned; otherwise, at
   * least one byte is read and stored into <code>b</code>.
   *
   * <p> The first byte read is stored into element <code>b</code>, the
   * next one into <code>b</code>, and so on. The number of bytes read is,
   * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
   * number of bytes actually read; these bytes will be stored in elements
   * <code>b</code> through <code>b[</code><i>k</i><code>-1]</code>,
   * leaving elements <code>b[</code><i>k</i><code>]</code> through
   * <code>b</code> unaffected.
   *
   * <p> The <code>read(b)</code> method for class <code>InputStream</code>
   * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
   *
   * @param      b   the buffer into which the data is read.
   * @return   the total number of bytes read into the buffer, or
   *             <code>-1</code> if there is no more data because the end of
   *             the stream has been reached.
   * @exceptionIOExceptionIf the first byte cannot be read for any reason
   * other than the end of the file, if the input stream has been closed, or
   * if some other I/O error occurs.
   * @exceptionNullPointerExceptionif <code>b</code> is <code>null</code>.
   * @see      java.io.InputStream#read(byte[], int, int)
   */
    public int read(byte b[]) throws IOException {
      return read(b, 0, b.length);
    }
```

注意:下面这一段说明:

> The first byte read is stored into element b, the next one into b, and so on.
>
> The number of bytes read is, at most, equal to the length of b.
>
> Let k be the number of bytes actually read;
>
> these bytes will be stored in elements b through b,
>
> leaving elements b through b unaffected.

简单说就是:

假设k(k<length)是实际读取到的字节数,那么数据会被保存在下标0到k-1

对于k到剩下的位置,是unaffected,这就是数据残留了

!(data/attachment/forum/202206/16/141330jha7st9soow8772i.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "common_log.png")
`转载务必注明出处:程序员潇然,疯狂的字节X,https://crazybytex.com/thread-102-1-1.html `
页: [1]
查看完整版本: java 使用 ByteArrayInputStream对字节数组分页 分组