@@ -475,6 +475,18 @@ public synchronized void write(byte[] bytes, int offset, int length) {
475475 synchronized boolean isTruncated () { return truncated ; }
476476 synchronized long observed () { return observed ; }
477477 synchronized void freeze () { frozen = true ; }
478+
479+ private synchronized BoundedCapture writable () {
480+ if (!frozen ) {
481+ return this ;
482+ }
483+ BoundedCapture copy = new BoundedCapture (limit );
484+ copy .buf = Arrays .copyOf (buf , count );
485+ copy .count = count ;
486+ copy .observed = observed ;
487+ copy .truncated = truncated ;
488+ return copy ;
489+ }
478490 }
479491
480492 private static IOException readerFailure (Reader stdout , Reader stderr ) {
@@ -579,33 +591,35 @@ private ExecutionRequest(CommandLine commandLine, File workingDirectory,
579591 }
580592 }
581593
582- /** Bounded stdout or stderr snapshot returned by the low-level execution API . */
594+ /** Legacy output stream. The first caller write detaches from the recorded process output . */
583595 public static final class BoundedOutputStream extends OutputStream {
584- private final BoundedCapture capture ;
596+ private BoundedCapture capture ;
585597
586598 private BoundedOutputStream (BoundedCapture capture ) {
587599 this .capture = capture ;
588600 }
589601
590602 @ Override
591- public void write (int value ) {
603+ public synchronized void write (int value ) {
604+ capture = capture .writable ();
592605 capture .write (value );
593606 }
594607
595608 @ Override
596- public void write (byte [] bytes , int offset , int length ) {
609+ public synchronized void write (byte [] bytes , int offset , int length ) {
610+ capture = capture .writable ();
597611 capture .write (bytes , offset , length );
598612 }
599613
600- public byte [] toByteArray () {
614+ public synchronized byte [] toByteArray () {
601615 return capture .toByteArray ();
602616 }
603617
604- public boolean isOverflowed () {
618+ public synchronized boolean isOverflowed () {
605619 return capture .isTruncated ();
606620 }
607621
608- int bufferCapacity () {
622+ synchronized int bufferCapacity () {
609623 return capture .bufferCapacity ();
610624 }
611625 }
0 commit comments