I am struggling to return multiple byte arrays using ByteArrayOutputStream. I have multiple string values that need to be converted to separate byte arrays and then return each one of them. My issue is that toByteArray() method does not work on ByteArrayOutputStream[], compiler shows following error "cannot invoke toByteArray() on the array type ByteArrayOutputStream[]". Here is the code:
int Count = getListsize.size(); <--- List has two elements of type String
ByteArrayOutputStream[] baos = new ByteArrayOutputStream[Count];
for (int i = 0; i < Count; i++) {
baos[i].write("string".getBytes());
}
return baos.toByteArray(); <--- Doesn't work with the ByteArrayOutputStream[]
You have to call
toByteArray()on each individualByteArrayOutputStreamin the array. You can create your ownbyte[][]to store that in. That said, of course, the code you've shown us has no reason to useByteArrayOutputStream, since it's just using it to copy the result of"string".getBytes().