Byte array of unknown length in java

jbu picture jbu · Mar 20, 2009 · Viewed 60.4k times · Source

I am constructing an array of bytes in java and I don't know how long the array will be.

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?

Answer

Clint picture Clint · Mar 20, 2009

Try ByteArrayOutputStream. You can use write( byte[] ) and it will grow as needed.