本文整理汇总了Java中java.lang.ProcessBuilder.Redirect.to方法的典型用法代码示例。如果您正苦于以下问题:Java Redirect.to方法的具体用法?Java Redirect.to怎么用?Java Redirect.to使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.ProcessBuilder.Redirect
的用法示例。
在下文中一共展示了Redirect.to方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getRedirect
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
/**
* 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();
}
}
示例2: check
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
/**
* 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;
}
示例3: err2out
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
@Test
public void err2out() {
IoRedirects rs = new IoRedirects(Redirect.from(file1), Redirect.to(file2), null);
Assert.assertTrue(rs.isErr2Out());
IoRedirects rs2 = new IoRedirects(Redirect.from(file1), Redirect.to(file2), Redirect.to(file3));
Assert.assertFalse(rs2.isErr2Out());
}
示例4: check
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
/**
* 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++) {
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.
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;
}
示例5: parseUri
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
/**
* Parses the given URI into a new {@link Redirect}. The URI is supposed to start with one of {@link RedirectScheme}
* prefixes. Examples of valid valid URIs: {@code read:/path/to/input-file.txt}, {@code write:/path/to/log.txt},
* {@code append:/path/to/log.txt}, {@code inherit} {@code err2out}.
*
* @param uri
* the URI to parse
* @return a new {@link Redirect}
* @throws IllegalArgumentException
* if the given {@code uri} is not in proper format
*/
public static Redirect parseUri(String uri) {
SrcdepsCoreUtils.assertArgNotNull(uri, "uri");
SrcdepsCoreUtils.assertArgNotEmptyString(uri, "uri");
int pos = uri.indexOf(':');
switch (pos) {
case -1:
pos = uri.length();
break;
case 0:
throw new IllegalArgumentException(String.format("Colon found at position 0 of URI string [%s]", uri));
default:
break;
}
final String redirectScheme = uri.substring(0, pos).toLowerCase(Locale.US);
final RedirectScheme scheme = RedirectScheme.valueOf(redirectScheme);
pos++;
final String path = pos < uri.length() ? uri.substring(pos) : null;
switch (scheme) {
case err2out:
if (path != null) {
throw new IllegalArgumentException(
String.format("Unexpected characters found after [err2out] in [%s]", uri));
}
return null;
case read:
return Redirect.from(new File(path));
case write:
return Redirect.to(new File(path));
case append:
return Redirect.appendTo(new File(path));
case inherit:
if (path != null) {
throw new IllegalArgumentException(
String.format("Unexpected characters found after [inherit] in [%s]", uri));
}
return Redirect.INHERIT;
default:
throw new IllegalStateException(String.format(
"Unexpected redirect type [%s] in redirect URI [%s] only [read], [write], [append], [inherit] are supported. In addition, you can use [err2out] for the error stream",
redirectScheme, uri));
}
}
示例6: execute
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
@Override
public void execute() throws MojoExecutionException {
//
// Say hello to the world, my little constructor injected component!
//
//component.hello();
final List<String> command = new ArrayList<>();
command.add(getCanonicalPath(_execLocation, "exec"));
getLog().debug(String.format("args=%s", _args));
if (!_args.isEmpty()) {
command.addAll(_args);
}
if (!_fileSets.isEmpty()) {
getLog().debug(String.format("fileSets=%s", _fileSets));
quoteList(getResourceFiles(_fileSets, _followLinks, _allowDuplicates, _allowFiles,
_allowDirs),
command);
}
if (_systemCommand) {
convertToSystemCommandArgs(command, _argQuote);
}
getLog().debug(String.format("command=%s", command));
Redirect errorRedirect;
// if (!getCanonicalPath(_errorFile, "_errorFile").isEmpty()) {
if (_errorFile != null) {
_errorFile = getCanonicalFile(_errorFile, "_errorFile");
errorRedirect = _errorAppend ? Redirect.appendTo(_errorFile) : Redirect.to(_errorFile);
} else if (!_errorProperty.isEmpty()) {
throw new MojoExecutionException("_errorProperty currently unsupported");
} else if (_errorInherit) {
errorRedirect = Redirect.INHERIT;
} else if (_errorPipe) {
errorRedirect = Redirect.PIPE;
} else {
throw new MojoExecutionException(
"must specify one of these: _errorProperty, _errorFile, _errorPipe, _errorInherit");
}
Redirect outRedirect;
// if (!getCanonicalPath(_outFile, "_outFile").isEmpty()) {
if (_outFile != null) {
_outFile = getCanonicalFile(_outFile, "_outFile");
outRedirect = _outAppend ? Redirect.appendTo(_outFile) : Redirect.to(_outFile);
} else if (_outInherit) {
outRedirect = Redirect.INHERIT;
} else if (_outPipe) {
outRedirect = Redirect.PIPE;
} else if (!_outProperty.isEmpty()) {
throw new MojoExecutionException("_outProperty currently unsupported");
} else {
throw new MojoExecutionException(
"must specify one of these: _outProperty, _outFile, _outPipe, _outInherit");
}
_exitCode =
createProcess(command, errorRedirect, outRedirect, _workDir, _environment,
_redirectErrorStream);
}
示例7: parse
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
/**
* 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);
}
示例8: redirectOutput
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
/**
* 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;
}
示例9: redirectError
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
/**
* 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;
}