當前位置: 首頁>>代碼示例>>Java>>正文


Java Redirect.to方法代碼示例

本文整理匯總了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();
  }
}
 
開發者ID:bazelbuild,項目名稱:bazel,代碼行數:26,代碼來源:JavaSubprocessFactory.java

示例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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:77,代碼來源:CommandExecutor.java

示例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());
}
 
開發者ID:srcdeps,項目名稱:srcdeps-core,代碼行數:8,代碼來源:RedirectsTest.java

示例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;
}
 
開發者ID:campolake,項目名稱:openjdk9,代碼行數:77,代碼來源:CommandExecutor.java

示例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));
    }
}
 
開發者ID:srcdeps,項目名稱:srcdeps-core,代碼行數:59,代碼來源:IoRedirects.java

示例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);
}
 
開發者ID:protobufel,項目名稱:protobuf-el,代碼行數:67,代碼來源:ExecMojo.java

示例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);
}
 
開發者ID:wildfly,項目名稱:wildfly-maven-plugin,代碼行數:62,代碼來源:StandardOutput.java

示例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;
}
 
開發者ID:wildfly,項目名稱:wildfly-core,代碼行數:14,代碼來源:Launcher.java

示例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;
}
 
開發者ID:wildfly,項目名稱:wildfly-core,代碼行數:14,代碼來源:Launcher.java


注:本文中的java.lang.ProcessBuilder.Redirect.to方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。