APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.jpackage.internal.util

public final Class CommandOutputControl

extends Object
Class Inheritance
Static Imports
java.util.stream.Collectors.joining

Runs commands and processes their stdout and stderr streams.

A command is either a subprocess represented by ProcessBuilder or a tool provided by ToolProvider.

A command is executed synchronously, and the result of its execution is stored in a Result instance which captures the exit code and any saved output streams.

Depending on the configuration, it can save the entire output stream, only the first line, or not save the output at all. Stdout and stderr streams can be configured independently.

Output streams can be treated as either byte streams or character streams.

The table below shows how different parameter combinations affect the content written to streams returned by dumpStdout() and dumpStderr() for subsequently executed tools, regardless of whether their output streams are saved, or for subprocesses when the output streams are saved:

discardStdout(false) and discardStderr(false) discardStdout(false) and discardStderr(true) discardStdout(true) and discardStderr(false)
redirectStderr(true) and dumpOutput(true)

dumpStdout(): STDOUT and STDERR interleaved

dumpStderr(): unchanged

dumpStdout(): STDOUT

dumpStderr(): unchanged

dumpStdout(): STDERR;

dumpStderr(): unchanged

redirectStderr(false) and dumpOutput(true)

dumpStdout(): STDOUT

dumpStderr(): STDERR

dumpStdout(): STDOUT

dumpStderr(): unchanged

dumpStdout(): unchanged

dumpStderr(): STDERR

The table below shows how different parameter combinations affect the content written to the native file descriptors associated with System#out and System#err for subsequently executed subprocesses when the output streams are not saved:

discardStdout(false) and discardStderr(false) discardStdout(false) and discardStderr(true) discardStdout(true) and discardStderr(false)
redirectStderr(true) and dumpOutput(true)

System.out: STDOUT and STDERR interleaved

System.err: unchanged

System.out: STDOUT

System.err: unchanged

System.out: STDERR;

The command's STDERR will be written to the stream referenced by dumpStdout() rather than to the underlying file descriptor associated with the Java process's STDOUT

System.err: unchanged

redirectStderr(false) and dumpOutput(true)

System.out: STDOUT

System.err: STDERR

System.out: STDOUT

System.err: unchanged

System.out: unchanged

System.err: STDERR

The table below shows how different parameter combinations affect the properties of Result objects returned by execute(ProcessBuilder, long) or execute(ToolProvider, long, String...) when processing character streams:

saveOutput(true) saveFirstLineOfOutput()
redirectStderr(true) and discardStdout(false) and discardStderr(false)

content(): STDOUT and STDERR interleaved

findStdout(): Optional.empty()

findStderr(): Optional.empty()

content(): a single-item list containing the first line of interleaved STDOUT and STDERR if the command produced any output; otherwise, an empty list

findStdout(): Optional.empty()

findStderr(): Optional.empty()

redirectStderr(false) and discardStdout(false) and discardStderr(false)

content(): STDOUT followed by STDERR

stdout(): STDOUT

stderr(): STDERR

content(): a list containing at most two items: the first line of STDOUT (if the command produced any), followed by the first line of STDERR (if the command produced any)

stdout(): The first line of STDOUT (if the command produced any); otherwise an empty list

findStderr(): The first line of STDERR (if the command produced any); otherwise an empty list

redirectStderr(true) and discardStdout(false) and discardStderr(true)

content(): STDOUT

stdout(): The same as content()

findStderr(): Optional.empty()

content(): The first line of STDOUT (if the command produced any); otherwise an empty list

stdout(): The same as content()

findStderr(): Optional.empty()

redirectStderr(false) and discardStdout(false) and discardStderr(true)

content(): STDOUT

stdout(): The same as content()

stderr(): an empty list

content(): The first line of STDOUT (if the command produced any); otherwise an empty list

stdout(): The same as content()

stderr(): an empty list

redirectStderr(true) and discardStdout(true) and discardStderr(false)

content(): STDERR

stdout(): The same as content()

findStderr(): Optional.empty()

content(): The first line of STDERR (if the command produced any); otherwise an empty list

stdout(): The same as content()

findStderr(): Optional.empty()

redirectStderr(false) and discardStdout(true) and discardStderr(false)

content(): STDERR

findStdout(): an empty list

stderr(): The same as content()

content(): The first line of STDERR (if the command produced any); otherwise an empty list

findStdout(): an empty list

stderr(): The same as content()

The table below shows how different parameter combinations affect the properties of Result objects returned by execute(ProcessBuilder, long) or execute(ToolProvider, long, String...) when processing byte streams:

saveOutput(true) or saveFirstLineOfOutput()
redirectStderr(true) and discardStdout(false) and discardStderr(false)

byteContent(): STDOUT and STDERR interleaved

findByteStdout(): Optional.empty()

findByteStderr(): Optional.empty()

redirectStderr(false) and discardStdout(false) and discardStderr(false)

byteContent(): STDOUT followed by STDERR

byteStdout(): STDOUT

byteStderr(): STDERR

redirectStderr(true) and discardStdout(false) and discardStderr(true)

byteContent(): STDOUT

byteStdout(): The same as byteContent()

findByteStderr(): Optional.empty()

redirectStderr(false) and discardStdout(false) and discardStderr(true)

byteContent(): STDOUT

byteStdout(): The same as byteContent()

byteStderr(): an empty array

redirectStderr(true) and discardStdout(true) and discardStderr(false)

byteContent(): STDERR

byteStdout(): The same as byteContent()

findByteStderr(): Optional.empty()

redirectStderr(false) and discardStdout(true) and discardStderr(false)

byteContent(): STDERR

findByteStdout(): an empty array

byteStderr(): The same as byteContent()

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record
private static record
private class
private static record
private static interface
public static interface
public static interface
private static enum
private static class
private static enum
private static record
public static record
private static record
public static record
private static record
public static record
private static record
public static class
public static class
public static class

Field Summary

Modifier and TypeField and Description
private Charset
private PrintStream
private PrintStream
public static CommandOutputControl.ExecutableAttributes
private int
private final Executor
private final CommandOutputControl.OutputStreamsControl
private Consumer<Process>

Constructor Summary

AccessConstructor and Description
public
private

Method Summary

Modifier and TypeMethod and Description
public CommandOutputControl

Returns:

this
binaryOutput
(boolean
true if this object will treat the output streams of subsequently executed commands as byte streams, and false otherwise
v
)

Specifies whether this object will treat output streams of subsequently executed commands as byte streams rather than character streams.

public CommandOutputControl

Returns:

this
charset
(Charset
character encoding for output streams of subsequently executed commands
v
)

Sets character encoding that will be applied to the stdout and the stderr streams of commands (subprocesses and ToolProvider-s) subsequently executed by this object.

public Charset

Returns:

the character encoding that will be applied to the stdout and stderr streams of commands subsequently executed by this object
charset
()

Returns the value passed in the last call of charset(Charset) method on this object, or StandardCharsets#UTF_8 if the method has not been called.

private static <T> CommandOutputControl.CommandOutput<T>
combine(Optional<? extends CommandOutputControl.Content<T>> out, Optional<? extends CommandOutputControl.Content<T>> err, boolean interleaved)

private void
public CommandOutputControl

Returns:

a deep copy of this object
copy
()

Returns a deep copy of this object.

public CommandOutputControl.Executable
public CommandOutputControl.Executable
public String
public CommandOutputControl

Returns:

this
discardStderr
(boolean
true if this object will discard stderr streams from commands subsequently executed by this object; false otherwise
v
)

Specifies whether stderr streams from commands subsequently executed by this object will be discarded.

public CommandOutputControl

Returns:

this
discardStdout
(boolean
true if this object will discard stdout streams from commands subsequently executed by this object; false otherwise
v
)

Specifies whether stdout streams from commands subsequently executed by this object will be discarded.

public CommandOutputControl

Returns:

this
dumpOutput
(boolean
if output streams from subsequently executed commands will be dumped into streams returned by dumpStdout() and dumpStderr() methods respectively
v
)

Specifies whether this object will dump the output streams from subsequently executed commands into the streams returned by dumpStdout() and dumpStdout() methods respectively.

public CommandOutputControl

Returns:

this
dumpStderr
(PrintStream
the stream where stderr streams from commands subsequently executed by this object will be dumped; null permitted
v
)

Specifies the stream where stderr streams from commands subsequently executed by this object will be dumped.

public PrintStream

Returns:

the stream where stderr streams from commands subsequently executed by this object will be dumped
dumpStderr
()

Returns the value passed in the last call of dumpStderr(PrintStream) method on this object, or System#err if the method has not been called.

public CommandOutputControl

Returns:

this
dumpStdout
(PrintStream
the stream where stdout streams from commands subsequently executed by this object will be dumped; null permitted
v
)

Specifies the stream where stdout streams from commands subsequently executed by this object will be dumped.

public PrintStream

Returns:

the stream where stdout streams from commands subsequently executed by this object will be dumped
dumpStdout
()

Returns the value passed in the last call of dumpStdout(PrintStream) method on this object, or System#out if the method has not been called.

private CommandOutputControl.Result
execute(ProcessBuilder pb, long timeoutMillis)

private CommandOutputControl.Result
execute(ToolProvider tp, long timeoutMillis, String... args)

private static Optional<Long>
public boolean

Returns:

the value passed in the last call of binaryOutput(boolean)
isBinaryOutput
()

Returns the value passed in the last call of binaryOutput(boolean) method on this object, or false if the method has not been called.

public boolean

Returns:

the value passed in the last call of discardStderr(boolean)
isDiscardStderr
()

Returns the value passed in the last call of discardStderr(boolean) method on this object, or false if the method has not been called.

public boolean

Returns:

the value passed in the last call of discardStdout(boolean)
isDiscardStdout
()

Returns the value passed in the last call of discardStdout(boolean) method on this object, or false if the method has not been called.

public boolean

Returns:

the value passed in the last call of dumpOutput(boolean)
isDumpOutput
()

Returns the value passed in the last call of dumpOutput(boolean) method on this object, or false if the method has not been called.

public boolean

Returns:

the value passed in the last call of redirectStderr(boolean)
isRedirectStderr
()

Returns the value passed in the last call of redirectStderr(boolean) method on this object, or false if the method has not been called.

public boolean

Returns:

true if this object will save the first line of the output of commands it executes subsequently; false otherwise
isSaveFirstLineOfOutput
()

Returns whether this object will save the first line of the output of commands subsequently executed.

public boolean

Returns:

true if this object will save the full output of commands it executes subsequently; false otherwise
isSaveOutput
()

Returns whether this object will save the complete output of commands subsequently executed.

public boolean

Returns:

the value passed in the last call of storeOutputInFiles(boolean)
isStoreOutputInFiles
()

Returns the value passed in the last call of storeOutputInFiles(boolean) method on this object, or false if the method has not been called.

private static <T> T
join(CompletableFuture<T> future, T cancelledValue)

private static void
private ProcessBuilder.Redirect
private static boolean
private static PrintStream
public CommandOutputControl

Returns:

this
processListener
(Consumer<Process>
the callback for notifying a subprocess being started or null
v
)

Sets the callback to be invoked when this object starts a subprocess from subsequent execute(ProcessBuilder, long) calls.

public Optional<Consumer<Process>>

Returns:

an Optional wrapping the value passed in the last call of processListener(Consumer)
processListener
()

Returns an Optional wrapping the value passed in the last call of processListener(Consumer) method on this object, or an empty Optional if the method has not been called or null was passed in the last call.

private static Optional<List<String>>
private static Optional<byte[]>
private boolean
redirectRetainedStderr()

Returns true if STDERR is not discarded and will be redirected to STDOUT, and false otherwise.

public CommandOutputControl

Returns:

this
redirectStderr
(boolean
true if the stderr stream of commands subsequently executed by this object will be redirected into the stdout stream; false otherwise
v
)

Specifies whether the stderr stream will be redirected into the stdout stream for commands subsequently executed by this object.

private boolean
replaceStdoutWithStderr()

Returns true if STDERR will replace STDOUT, and false otherwise.

public CommandOutputControl

Returns:

this
saveFirstLineOfOutput
()

Specifies whether the first line of the output, combined from the stdout and stderr streams of commands subsequently executed by this object, will be saved.

public CommandOutputControl

Returns:

this
saveOutput
(boolean
true to save the full stdout and stderr streams; false otherwise
v
)

Specifies whether the full output produced by commands subsequently executed by this object will be saved.

private CommandOutputControl
private CommandOutputControl
public CommandOutputControl

Returns:

this
storeOutputInFiles
(boolean
true if this object will use files to store saved output streams of subsequently executed subprocesses; false otherwise
v
)

Specifies whether stderr and stdout streams for subprocesses subsequently executed by this object will be stored in files.

private Optional<Path>
private static void
private static <T> void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait