Skip to content

Commit e125d44

Browse files
committed
fix: retain bounded writes on legacy subprocess output streams
1 parent 6f18022 commit e125d44

2 files changed

Lines changed: 42 additions & 7 deletions

File tree

‎src/main/java/io/github/easy4j/opencli/core/support/SubprocessExecutionSupport.java‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎src/test/java/io/github/easy4j/opencli/core/support/SubprocessExecutionSupportTest.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason;
44
import java.io.PrintWriter;
55
import java.io.StringWriter;
6+
import java.nio.charset.StandardCharsets;
67
import java.nio.file.Path;
78
import org.apache.commons.exec.CommandLine;
89
import org.junit.jupiter.api.io.TempDir;
@@ -11,6 +12,26 @@
1112

1213
class SubprocessExecutionSupportTest {
1314

15+
@Test
16+
void legacyOutputStreamsAllowBoundedWritesWithoutChangingExecutionDetails() throws Exception {
17+
CommandLine command = new CommandLine("/usr/bin/printf");
18+
command.addArgument("abc");
19+
SubprocessExecutionSupport.RunSession session = SubprocessExecutionSupport.execute(
20+
new SubprocessExecutionSupport.ExecutionRequest(command, null, null, 10_000L, 5L));
21+
22+
SubprocessExecutionSupport.BoundedOutputStream stdout = session.getStdout();
23+
SubprocessExecutionSupport.BoundedOutputStream stderr = session.getStderr();
24+
stdout.write('d');
25+
stdout.write(new byte[]{'x', 'e', 'f'}, 1, 2);
26+
stderr.write(new byte[]{'!'}, 0, 1);
27+
28+
assertArrayEquals("abcde".getBytes(StandardCharsets.UTF_8), stdout.toByteArray());
29+
assertTrue(stdout.isOverflowed());
30+
assertArrayEquals(new byte[]{'!'}, stderr.toByteArray());
31+
assertFalse(session.getExecutionDetails().isStdoutTruncated());
32+
assertFalse(session.getExecutionDetails().isStderrTruncated());
33+
}
34+
1435
@Test
1536
void shouldAllocateOutputStorageOnlyOnFirstNonemptyWrite() {
1637
SubprocessExecutionSupport.BoundedCapture capture = new SubprocessExecutionSupport.BoundedCapture(12_000);

0 commit comments

Comments
 (0)