如何从字节数组创建IntStream? [英] How can I make an IntStream from a byte array?

查看:137
本文介绍了如何从字节数组创建IntStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道只有 IntStream LongStream 。如何从字节数组中创建 IntStream

I already know there are only IntStream and LongStream. How can I make an IntStream from a byte array?

目前我打算这样做。

static int[] bytesToInts(final byte[] bytes) {
    final int[] ints = new int[bytes.length];
    for (int i = 0; i < ints.length; i++) {
        ints[i] = bytes[i] & 0xFF;
    }
    return ints;
}

static IntStream bytesToIntStream(final byte[] bytes) {
    return IntStream.of(bytesToInt(bytes));
}

有没有更容易或更快的方法呢?

Is there any easier or faster way to do this?

推荐答案

Radiodef答案的变体:

A variant of Radiodef's answer:

static IntStream bytesToIntStream(byte[] bytes) {
    return IntStream.range(0, bytes.length)
        .map(i -> bytes[i] & 0xFF)
    ;
}

更容易保证并行化。

这篇关于如何从字节数组创建IntStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆