Skip to content
Advertisement

Byte array of unknown length in java

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[] 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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement