Name: rmT116609 Date: 11/13/2003
A DESCRIPTION OF THE PROBLEM :
Documentation for toByteArray() states "Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it." Neither statement is correct. The first is incorrect in light of the evaluation give by Sun of bug 4336334 (ByteArrayOutputStream.toByteArray() does not clone byte array buffer - Closed, Will not be fixed.) The second sentence is incorrect, because the size of the byte array returned is not the same as the return value of size(). The size of the byte array is the size of the buffer, which can be greater than size() if the buffer was grown, then the stream reset() and reused. In the pathological case, if the ByteArrayOutputStream was written to, then reset(), size() is 0 but toByteArray().length is non-zero.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Returns the buffer where data is stored. Only the region of the byte array from index 0 to index size-1 are valid.
ACTUAL -
Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
URL OF FAULTY DOCUMENTATION :
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayOutputStream.html#toByteArray()
(Incident Review ID: 188373)
======================================================================