Java 8 - 将 List<byte[]> 合并到 byte[] 的最有效方法



我有一个库,它返回一些二进制数据作为二进制数组列表。这些byte[]需要合并到InputStream中。

这是我当前的实现:

public static InputStream foo(List<byte[]> binary) {
    byte[] streamArray = null;
    binary.forEach(bin -> {
        org.apache.commons.lang.ArrayUtils.addAll(streamArray, bin);
    });
    return new ByteArrayInputStream(streamArray);
}

,但这是相当密集的CPU。有没有更好的办法?

谢谢你的回答。我做了一个性能测试。这些是我的结果:

  • Function: 'NicolasFilotto' => 68,04 ms平均100次呼叫
  • Function: 'NicolasFilottoEstSize' => 65,100次调用平均24毫秒
  • 函数:'NicolasFilottoSequenceInputStream' => 63,09 ms平均100次调用
  • Function: 'Saka1029_1' => 63,06 ms平均100次调用
  • Function: 'Saka1029_2' => 100次调用平均0.79 ms
  • 函数:'Coco' => 541, 10次调用平均60毫秒

我不确定'Saka1029_2'是否测量正确…

这是执行函数:

private static double execute(Callable<InputStream> funct, int times) throws Exception {
    List<Long> executions = new ArrayList<>(times);
    for (int idx = 0; idx < times; idx++) {
        BufferedReader br = null;
        long startTime = System.currentTimeMillis();
        InputStream is = funct.call();
        br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while ((line = br.readLine()) != null) {}
        executions.add(System.currentTimeMillis() - startTime);
    }
    return calculateAverage(executions);
}

注意我读取了每个输入流

这些是使用的实现:

public static class NicolasFilotto implements Callable<InputStream> {
    private final List<byte[]> binary;
    public NicolasFilotto(List<byte[]> binary) {
        this.binary = binary;
    }
    @Override
    public InputStream call() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for (byte[] bytes : binary) {
            baos.write(bytes, 0, bytes.length);
        }
        return new ByteArrayInputStream(baos.toByteArray());
    }
}
public static class NicolasFilottoSequenceInputStream implements Callable<InputStream> {
    private final List<byte[]> binary;
    public NicolasFilottoSequenceInputStream(List<byte[]> binary) {
        this.binary = binary;
    }
    @Override
    public InputStream call() throws Exception {
        return new SequenceInputStream(
                Collections.enumeration(
                        binary.stream().map(ByteArrayInputStream::new).collect(Collectors.toList())));
    }
}
public static class NicolasFilottoEstSize implements Callable<InputStream> {
    private final List<byte[]> binary;
    private final int lineSize;
    public NicolasFilottoEstSize(List<byte[]> binary, int lineSize) {
        this.binary = binary;
        this.lineSize = lineSize;
    }
    @Override
    public InputStream call() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(binary.size() * lineSize);
        for (byte[] bytes : binary) {
            baos.write(bytes, 0, bytes.length);
        }
        return new ByteArrayInputStream(baos.toByteArray());
    }
}
public static class Saka1029_1 implements Callable<InputStream> {
    private final List<byte[]> binary;
    public Saka1029_1(List<byte[]> binary) {
        this.binary = binary;
    }
    @Override
    public InputStream call() throws Exception {
        byte[] all = new byte[binary.stream().mapToInt(a -> a.length).sum()];
        int pos = 0;
        for (byte[] bin : binary) {
            int length = bin.length;
            System.arraycopy(bin, 0, all, pos, length);
            pos += length;
        }
        return new ByteArrayInputStream(all);
    }
}
public static class Saka1029_2 implements Callable<InputStream> {
    private final List<byte[]> binary;
    public Saka1029_2(List<byte[]> binary) {
        this.binary = binary;
    }
    @Override
    public InputStream call() throws Exception {
        int size = binary.size();
        return new InputStream() {
            int i = 0, j = 0;
            @Override
            public int read() throws IOException {
                if (i >= size) return -1;
                if (j >= binary.get(i).length) {
                    ++i;
                    j = 0;
                }
                if (i >= size) return -1;
                return binary.get(i)[j++];
            }
        };
    }
}
public static class Coco implements Callable<InputStream> {
    private final List<byte[]> binary;
    public Coco(List<byte[]> binary) {
        this.binary = binary;
    }
    @Override
    public InputStream call() throws Exception {
        byte[] streamArray = new byte[0];
        for (byte[] bin : binary) {
            streamArray = org.apache.commons.lang.ArrayUtils.addAll(streamArray, bin);
        }
        return new ByteArrayInputStream(streamArray);
    }
}

您可以使用ByteArrayOutputStream来存储列表的每个字节数组的内容,但为了使其高效,我们需要创建ByteArrayOutputStream 的实例,其初始大小尽可能与目标大小相匹配,因此,如果您知道字节数组的大小或至少平均大小,您应该使用它,代码将是:

public static InputStream foo(List<byte[]> binary) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(ARRAY_SIZE * binary.size());
    for (byte[] bytes : binary) {
        baos.write(bytes, 0, bytes.length);
    }
    return new ByteArrayInputStream(baos.toByteArray());
}

另一种方法是使用SequenceInputStream,以便在逻辑上连接代表列表中一个元素的所有ByteArrayInputStream实例,如下所示:

public static InputStream foo(List<byte[]> binary) {
    return new SequenceInputStream(
        Collections.enumeration(
            binary.stream().map(ByteArrayInputStream::new).collect(Collectors.toList())
        )
    );
}

这种方法的有趣之处在于,您不需要复制任何东西,您只需创建ByteArrayInputStream的实例,该实例将使用字节数组的原样。

为了避免将结果收集为具有成本的List,特别是如果您的初始List很大,您可以直接调用iterator(),如所建议的@Holger,然后我们只需要将iterator转换为enumeration,这可以通过Apache Commons Collection中的IteratorUtils.asEnumeration(iterator)完成,最终代码将是:

public static InputStream foo(List<byte[]> binary) {
    return new SequenceInputStream(
        IteratorUtils.asEnumeration(
            binary.stream().map(ByteArrayInputStream::new).iterator()
        )
    );
}

试试这个

public static InputStream foo(List<byte[]> binary) {
    byte[] all = new byte[binary.stream().mapToInt(a -> a.length).sum()];
    int pos = 0;
    for (byte[] bin : binary) {
        int length = bin.length;
        System.arraycopy(bin, 0, all, pos, length);
        pos += length;
    }
    return new ByteArrayInputStream(all);
}

public static InputStream foo(List<byte[]> binary) {
    int size = binary.size();
    return new InputStream() {
        int i = 0, j = 0;
        @Override
        public int read() throws IOException {
            if (i >= size) return -1;
            if (j >= binary.get(i).length) {
                ++i;
                j = 0;
            }
            if (i >= size) return -1;
            return binary.get(i)[j++];
        }
    };
}

相关内容

  • 没有找到相关文章

最新更新