下面列出了java.lang.ProcessBuilder.Redirect#to ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns a {@link ProcessBuilder.Redirect} appropriate for the parameters. If a file redirected
* to exists, deletes the file before redirecting to it.
*/
private Redirect getRedirect(StreamAction action, File file) {
switch (action) {
case DISCARD:
return Redirect.to(new File("/dev/null"));
case REDIRECT:
// We need to use Redirect.appendTo() here, because on older Linux kernels writes are
// otherwise not atomic and might result in lost log messages:
// https://lkml.org/lkml/2014/3/3/308
if (file.exists()) {
file.delete();
}
return Redirect.appendTo(file);
case STREAM:
return Redirect.PIPE;
default:
throw new IllegalStateException();
}
}
/**
* Returns a {@link java.lang.ProcessBuilder.Redirect} appropriate for the parameters. If a file
* redirected to exists, deletes the file before redirecting to it.
*/
private Redirect getRedirect(StreamAction action, File file) {
switch (action) {
case DISCARD:
return Redirect.to(new File("/dev/null"));
case REDIRECT:
// We need to use Redirect.appendTo() here, because on older Linux kernels writes are
// otherwise not atomic and might result in lost log messages:
// https://lkml.org/lkml/2014/3/3/308
if (file.exists()) {
file.delete();
}
return Redirect.appendTo(file);
case STREAM:
return Redirect.PIPE;
default:
throw new IllegalStateException();
}
}
/**
* check - tests to see if the current token contains a redirect
* @param token current command line token
* @param iterator current command line iterator
* @param cwd current working directory
* @return true if token is consumed
*/
boolean check(String token, final Iterator<String> iterator, final String cwd) {
// Iterate through redirect prefixes to file a match.
for (int i = 0; i < redirectPrefixes.length; i++) {
final String prefix = redirectPrefixes[i];
// If a match is found.
if (token.startsWith(prefix)) {
// Indicate we have at least one redirect (efficiency.)
hasRedirects = true;
// Map prefix to RedirectType.
final RedirectType redirect = redirects[i];
// Strip prefix from token
token = token.substring(prefix.length());
// Get file from either current or next token.
File file = null;
if (redirect != REDIRECT_ERROR_TO_OUTPUT) {
// Nothing left of current token.
if (token.length() == 0) {
if (iterator.hasNext()) {
// Use next token.
token = iterator.next();
} else {
// Send to null device if not provided.
token = IS_WINDOWS ? "NUL:" : "/dev/null";
}
}
// Redirect file.
file = resolvePath(cwd, token).toFile();
}
// Define redirect based on prefix.
switch (redirect) {
case REDIRECT_INPUT:
inputRedirect = Redirect.from(file);
break;
case REDIRECT_OUTPUT:
outputRedirect = Redirect.to(file);
break;
case REDIRECT_OUTPUT_APPEND:
outputRedirect = Redirect.appendTo(file);
break;
case REDIRECT_ERROR:
errorRedirect = Redirect.to(file);
break;
case REDIRECT_ERROR_APPEND:
errorRedirect = Redirect.appendTo(file);
break;
case REDIRECT_OUTPUT_ERROR_APPEND:
outputRedirect = Redirect.to(file);
errorRedirect = Redirect.to(file);
mergeError = true;
break;
case REDIRECT_ERROR_TO_OUTPUT:
mergeError = true;
break;
default:
return false;
}
// Indicate token is consumed.
return true;
}
}
// No redirect found.
return false;
}
/**
* Parses the string and attempts to determine where the data for the stream should be written. The following are
* the options for the value:
* <ul>
* <li>{@code none} indicates the data for this stream will be consumed and {@link #toString()} will return the
* data of the {@code discardNone} parameter is {@code false}, otherwise the data will be discarded</li>
* <li>{@code System.out} or {@code System.err} to write to the respective stream</li>
* <li>Any other value is assumed to be the path to a file and the data will written to the file</li>
* </ul>
*
* @param stdout the value to be parsed
* @param discardNone {@code true} if the {@code stdout} value is {@code none} and the data should be discarded,
* otherwise the data will be consumed if the {@code stdout} value is {@code none} and will be
* available via {@link #toString()}
*
* @return a new output stream
*
* @throws IOException if there is an error creating the stream
*/
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public static StandardOutput parse(final String stdout, final boolean discardNone) throws IOException {
if (stdout == null) {
return new StandardOutput(Target.INHERIT, null, Redirect.INHERIT, null);
}
final Target target;
Path stdoutPath = null;
final OutputStream out;
final String value = stdout.trim().toLowerCase(Locale.ENGLISH);
if ("system.out".equals(value)) {
target = Target.SYSTEM_OUT;
out = System.out;
} else if ("system.err".equals(value)) {
target = Target.SYSTEM_ERR;
out = System.err;
} else if ("none".equals(value)) {
if (discardNone) {
target = Target.DISCARDING;
out = DISCARDING;
} else {
target = Target.COLLECTING;
out = new ByteArrayOutputStream();
}
} else {
// Attempt to create a file
stdoutPath = Paths.get(stdout.trim());
if (Files.notExists(stdoutPath)) {
final Path parent = stdoutPath.getParent();
if (parent != null) {
Files.createDirectories(parent);
}
Files.createFile(stdoutPath);
}
target = Target.FILE;
out = null;
}
Redirect destination = null;
if (stdoutPath != null) {
destination = Redirect.to(stdoutPath.toFile());
}
return new StandardOutput(target, out, destination, stdoutPath);
}
/**
* Redirects the output of the process to a file.
*
* @param file the file to redirect the output to
*
* @return the launcher
*
* @see java.lang.ProcessBuilder.Redirect#to(java.io.File)
*/
public Launcher redirectOutput(final File file) {
outputDestination = Redirect.to(file);
return this;
}
/**
* Redirects the error stream of the process to a file.
*
* @param file the file to redirect the error stream to
*
* @return the launcher
*
* @see java.lang.ProcessBuilder.Redirect#to(java.io.File)
*/
public Launcher redirectError(final File file) {
errorDestination = Redirect.to(file);
return this;
}