Programming Tips - Java: Is there something like the StringBuilder class for bytes?

Date: 2010dec10 Language: Java Keywords: growable byte array Q. Java: Is there something like the StringBuilder class for bytes? (ie growable array) A. Use ByteArrayOutputStream. Despite the name it has nothing to do with output or streams. Its just a growable byte array.
ByteArrayOutputStream my_bytes = new ByteArrayOutputStream(); my_bytes.write(other_bytes); // Append my_bytes.size(); // How many? my_bytes.reset(); // clear, make empty
One problem with this class is that write() can throw an IOException so try-ing and catch-ing is a pain. And it should be called append() So here is our wrapper class:
ByteArray extends ByteArrayOutputStream { void append(byte[] more) { try { write(more); } catch(IOException e) { } } void clear() { reset(); } }