Hej Ivan, IIRC I wrote the first iteration of this code.
I would expect that almost always either the buffer will be empty or the call to available() is accurate and the read will actually read everything in one chunk, so the byte array will be perfectly allocated with no need for re-allocation in practice. Do you have evidence it would be otherwise in practice? But suppose available() incorrectly claims that a million bytes are available, but you only read 10. Then there's a tradeoff - the cost of reallocation versus the risk that the mostly empty backing array will be retained for a long time if the Process object is not garbage collected. I'm leaning towards the status quo. On Mon, Jun 4, 2018 at 9:26 PM, Ivan Gerasimov <ivan.gerasi...@oracle.com> wrote: > Hello! > > When closing a Process, its stdout and stderr are drained: The remaining > bytes are copied into an array, and then the out/err stream is replaced > with ByteArrayInputStream(). > > In a case when a fewer number of bytes were read then the > Stream.available() suggested, the array is reallocated with Arrays.copyOf(). > > It is possible to avoid reallocation, and use three-args > ByteArrayInputStream() constructor to specify a portion of the array used. > > Would you please help review this trivial fix? > > BUGURL: https://bugs.openjdk.java.net/browse/JDK-8203768 > WEBREV: http://cr.openjdk.java.net/~igerasim/8203768/00/webrev/ > > All existing tests pass on all supported platforms. > > Thanks in advance! > > -- > With kind regards, > Ivan Gerasimov > >