refactor to use ByteArrayBuilder