equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
31 import java.io.OutputStream; |
31 import java.io.OutputStream; |
32 import java.util.Arrays; |
32 import java.util.Arrays; |
33 import java.util.ArrayList; |
33 import java.util.ArrayList; |
34 import java.util.List; |
34 import java.util.List; |
35 import java.util.Map; |
35 import java.util.Map; |
36 |
36 import java.security.AccessController; |
|
37 import java.security.PrivilegedAction; |
37 /** |
38 /** |
38 * This class is used to create operating system processes. |
39 * This class is used to create operating system processes. |
39 * |
40 * |
40 * <p>Each {@code ProcessBuilder} instance manages a collection |
41 * <p>Each {@code ProcessBuilder} instance manages a collection |
41 * of process attributes. The {@link #start()} method creates a new |
42 * of process attributes. The {@link #start()} method creates a new |
443 * Each {@code Redirect} instance is one of the following: |
444 * Each {@code Redirect} instance is one of the following: |
444 * |
445 * |
445 * <ul> |
446 * <ul> |
446 * <li>the special value {@link #PIPE Redirect.PIPE} |
447 * <li>the special value {@link #PIPE Redirect.PIPE} |
447 * <li>the special value {@link #INHERIT Redirect.INHERIT} |
448 * <li>the special value {@link #INHERIT Redirect.INHERIT} |
|
449 * <li>the special value {@link #DISCARD Redirect.DISCARD} |
448 * <li>a redirection to read from a file, created by an invocation of |
450 * <li>a redirection to read from a file, created by an invocation of |
449 * {@link Redirect#from Redirect.from(File)} |
451 * {@link Redirect#from Redirect.from(File)} |
450 * <li>a redirection to write to a file, created by an invocation of |
452 * <li>a redirection to write to a file, created by an invocation of |
451 * {@link Redirect#to Redirect.to(File)} |
453 * {@link Redirect#to Redirect.to(File)} |
452 * <li>a redirection to append to a file, created by an invocation of |
454 * <li>a redirection to append to a file, created by an invocation of |
457 * {@link Type Type}. |
459 * {@link Type Type}. |
458 * |
460 * |
459 * @since 1.7 |
461 * @since 1.7 |
460 */ |
462 */ |
461 public abstract static class Redirect { |
463 public abstract static class Redirect { |
|
464 private static final File NULL_FILE = AccessController.doPrivileged( |
|
465 (PrivilegedAction<File>) () -> { |
|
466 return new File((System.getProperty("os.name") |
|
467 .startsWith("Windows") ? "NUL" : "/dev/null")); |
|
468 } |
|
469 ); |
|
470 |
462 /** |
471 /** |
463 * The type of a {@link Redirect}. |
472 * The type of a {@link Redirect}. |
464 */ |
473 */ |
465 public enum Type { |
474 public enum Type { |
466 /** |
475 /** |
526 * }</pre> |
535 * }</pre> |
527 */ |
536 */ |
528 public static final Redirect INHERIT = new Redirect() { |
537 public static final Redirect INHERIT = new Redirect() { |
529 public Type type() { return Type.INHERIT; } |
538 public Type type() { return Type.INHERIT; } |
530 public String toString() { return type().toString(); }}; |
539 public String toString() { return type().toString(); }}; |
|
540 |
|
541 |
|
542 /** |
|
543 * Indicates that subprocess output will be discarded. |
|
544 * A typical implementation discards the output by writing to |
|
545 * an operating system specific "null file". |
|
546 * |
|
547 * <p>It will always be true that |
|
548 * <pre> {@code |
|
549 * Redirect.DISCARD.file() the filename appropriate for the operating system |
|
550 * and may be null && |
|
551 * Redirect.DISCARD.type() == Redirect.Type.WRITE && |
|
552 * Redirect.DISCARD.append() == false |
|
553 * }</pre> |
|
554 * @since 9 |
|
555 */ |
|
556 public static final Redirect DISCARD = new Redirect() { |
|
557 public Type type() { return Type.WRITE; } |
|
558 public String toString() { return type().toString(); } |
|
559 public File file() { return NULL_FILE; } |
|
560 boolean append() { return false; } |
|
561 }; |
531 |
562 |
532 /** |
563 /** |
533 * Returns the {@link File} source or destination associated |
564 * Returns the {@link File} source or destination associated |
534 * with this redirect, or {@code null} if there is no such file. |
565 * with this redirect, or {@code null} if there is no such file. |
535 * |
566 * |