88import org .apache .commons .exec .PumpStreamHandler ;
99
1010import java .io .ByteArrayOutputStream ;
11+ import java .io .OutputStream ;
1112import java .io .File ;
1213import java .io .IOException ;
1314import java .time .Duration ;
@@ -71,8 +72,8 @@ public static RunSession execute(ExecutionRequest request) throws IOException, I
7172
7273 private static RunSession executeWithinLimit (ExecutionRequest request ) throws IOException , InterruptedException {
7374 long timeoutMs = Math .max (1L , request .getTimeoutMillis ());
74- ByteArrayOutputStream out = new ByteArrayOutputStream ( );
75- ByteArrayOutputStream err = new ByteArrayOutputStream ( );
75+ BoundedOutputStream out = new BoundedOutputStream ( request . getMaxOutputBytes () );
76+ BoundedOutputStream err = new BoundedOutputStream ( request . getMaxOutputBytes () );
7677
7778 DefaultExecutor .Builder builder = DefaultExecutor .builder ();
7879 if (request .getWorkingDirectory () != null ) {
@@ -122,32 +123,43 @@ public static final class ExecutionRequest {
122123 private final File workingDirectory ;
123124 private final Map <String , String > environment ;
124125 private final long timeoutMillis ;
126+ private final long maxOutputBytes ;
125127
126128 public ExecutionRequest (
127129 CommandLine commandLine ,
128130 File workingDirectory ,
129131 Map <String , String > environment ,
130132 long timeoutMillis ) {
131- this .commandLine = Objects .requireNonNull (commandLine , "commandLine" );
132- this .workingDirectory = workingDirectory ;
133- this .environment = environment ;
134- this .timeoutMillis = timeoutMillis ;
135- }
133+ this (commandLine , workingDirectory , environment , timeoutMillis , 0L );
134+ }
135+
136+ public ExecutionRequest (
137+ CommandLine commandLine ,
138+ File workingDirectory ,
139+ Map <String , String > environment ,
140+ long timeoutMillis ,
141+ long maxOutputBytes ) {
142+ this .commandLine = Objects .requireNonNull (commandLine , "commandLine" );
143+ this .workingDirectory = workingDirectory ;
144+ this .environment = environment ;
145+ this .timeoutMillis = timeoutMillis ;
146+ this .maxOutputBytes = maxOutputBytes ;
147+ }
136148 }
137149
138150 @ Getter
139151 public static final class RunSession {
140152
141- private final ByteArrayOutputStream stdout ;
142- private final ByteArrayOutputStream stderr ;
153+ private final BoundedOutputStream stdout ;
154+ private final BoundedOutputStream stderr ;
143155 private final DefaultExecuteResultHandler handler ;
144156 private final ExecuteWatchdog watchdog ;
145157 private final long timeoutMillis ;
146158 private final boolean waitTimedOut ;
147159
148160 RunSession (
149- ByteArrayOutputStream stdout ,
150- ByteArrayOutputStream stderr ,
161+ BoundedOutputStream stdout ,
162+ BoundedOutputStream stderr ,
151163 DefaultExecuteResultHandler handler ,
152164 ExecuteWatchdog watchdog ,
153165 long timeoutMillis ,
@@ -163,5 +175,61 @@ public static final class RunSession {
163175 public boolean timedOut () {
164176 return waitTimedOut || watchdog .killedProcess ();
165177 }
178+
179+ boolean isStdoutOverflowed () {
180+ return stdout .isOverflowed ();
181+ }
182+
183+ boolean isStderrOverflowed () {
184+ return stderr .isOverflowed ();
185+ }
186+ }
187+
188+ /**
189+ * 有界内存输出流:超过上限的字节直接丢弃(保留前 maxBytes 字节),
190+ * 并置溢出标志供调用方追加截断标记。close 为空操作(纯内存流)。
191+ */
192+ public static final class BoundedOutputStream extends OutputStream {
193+
194+ private final ByteArrayOutputStream delegate = new ByteArrayOutputStream ();
195+ private final long maxBytes ;
196+ private boolean overflowed ;
197+
198+ BoundedOutputStream (long maxBytes ) {
199+ // maxBytes <= 0 视为不限制
200+ this .maxBytes = Math .max (0L , maxBytes );
201+ }
202+
203+ @ Override
204+ public synchronized void write (int b ) {
205+ write (new byte [] {(byte ) b }, 0 , 1 );
206+ }
207+
208+ @ Override
209+ public synchronized void write (byte [] b , int off , int len ) {
210+ if (maxBytes == 0L ) {
211+ delegate .write (b , off , len );
212+ return ;
213+ }
214+ long remaining = maxBytes - delegate .size ();
215+ if (remaining <= 0L ) {
216+ overflowed = true ;
217+ return ;
218+ }
219+ if (len > remaining ) {
220+ delegate .write (b , off , (int ) remaining );
221+ overflowed = true ;
222+ } else {
223+ delegate .write (b , off , len );
224+ }
225+ }
226+
227+ public synchronized byte [] toByteArray () {
228+ return delegate .toByteArray ();
229+ }
230+
231+ public synchronized boolean isOverflowed () {
232+ return overflowed ;
233+ }
166234 }
167235}
0 commit comments