请选择 进入手机版 | 继续访问电脑版

[代码段] java 使用 ByteArrayInputStream对字节数组分页 分组

技术实战 技术实战 5021 人阅读 | 0 人回复

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

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

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

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

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

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[1024];

        int pageNum = 0;

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

    }
}

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

注意

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

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

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

/**
     * 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[0]</code>, the
     * next one into <code>b[1]</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[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
     * leaving elements <code>b[</code><i>k</i><code>]</code> through
     * <code>b[b.length-1]</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.
     * @exception  IOException  If 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.
     * @exception  NullPointerException  if <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[0], the next one into b[1], 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[0] through b[k-1],

leaving elements b[k] through b[b.length-1] unaffected.

简单说就是:

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

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

common_log.png 转载务必注明出处:程序员潇然,疯狂的字节X,https://crazybytex.com/thread-102-1-1.html

关注下面的标签,发现更多相似文章
    黄小斜学Java

    疯狂的字节X

  • 目前专注于分享Java领域干货,公众号同步更新。原创以及收集整理,把最好的留下。
    包括但不限于JVM、计算机科学、算法、数据库、分布式、Spring全家桶、微服务、高并发、Docker容器、ELK、大数据等相关知识,一起进步,一起成长。
热门推荐
海康摄像头接入 wvp-GB28181-pro平台测试验
[md]### 简介 开箱即用的28181协议视频平台 `https://github.c
[若依]微服务springcloud版新建增添加一个
[md]若依框架是一个比较出名的后台管理系统,有多个不同版本。
[CXX1300] CMake '3.18.1' was not
[md][CXX1300] CMake '3.18.1' was not found in SDK, PATH, or