java中未知长度的字节数组 [英] Byte array of unknown length in java
问题描述
我在java中构造一个字节数组,我不知道该数组有多长。
I am constructing an array of bytes in java and I don't know how long the array will be.
我想要一些像Java的StringBuffer这样的工具你可以调用.append(字节b)或.append(byte [] buf)并让它缓冲我所有的字节和我完成后,给我一个字节数组。是否有一个类用于字符串StringBuffer为字符串做什么?它看起来不像我正在寻找的ByteBuffer类。
I want some tool like Java's StringBuffer that you can just call .append(byte b) or .append(byte[] buf) and have it buffer all my bytes and return to me a byte array when I'm done. Is there a class that does for bytes what StringBuffer does for Strings? It does not look like the ByteBuffer class is what I'm looking for.
任何人都有一个很好的解决方案吗?
Anyone have a good solution?
推荐答案
尝试 ByteArrayOutputStream
。你可以使用 write(byte [])
,它会根据需要增长。
Try ByteArrayOutputStream
. You can use write( byte[] )
and it will grow as needed.
这篇关于java中未知长度的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!