Search code examples
javaarraysoutputstreambytearrayoutputstream

Returning big size byte array or using ByteArrayOutputStream


My question is with big size byte array (from 10k bytes), should I return direct the byte array value like this:

byte[] my_function() {
...
return value;
}

or use ByteArrayOutputStream/OutputStream as an param?

void my_function(ByteArrayOutputStream os) {
...
os.write(value);
}

Could you show me the advantage of using ByteArrayOutputStream/OutputStream against returning the result direct?


Solution

  • As the comment of njzk2:

    Plain OutputStream would leave the option to the caller of the function to use any other type of stream including, but not limited to, files, network, byte[],.. Taking an OutputStream as parameter gives the people using your function much more flexibility