本文整理汇总了Java中java.lang.ProcessBuilder.Redirect.appendTo方法的典型用法代码示例。如果您正苦于以下问题:Java Redirect.appendTo方法的具体用法?Java Redirect.appendTo怎么用?Java Redirect.appendTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.ProcessBuilder.Redirect
的用法示例。
在下文中一共展示了Redirect.appendTo方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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;
}
示例4: 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));
}
}
示例5: 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);
}
示例6: exec
import java.lang.ProcessBuilder.Redirect; //导入方法依赖的package包/类
/**
* Runs the process redirecting both its error and output streams to the
* specified file.
* @param outputFile the file to redirect the process's streams to.
* @return the process's exit status.
* @throws IOException if an I/O error occurs while running the process.
* @throws InterruptedException if the current thread is interrupted before
* the spawned process terminates.
*/
public int exec(Path outputFile) throws IOException, InterruptedException {
requireNonNull(outputFile, "outputFile");
Redirect withOutputFile = Redirect.appendTo(outputFile.toFile());
return startProcess(withOutputFile).waitFor();
}