Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8191441

(Process) add Readers and Writer access to java.lang.Process streams

    XMLWordPrintable

Details

    • Enhancement
    • Status: Resolved
    • P3
    • Resolution: Fixed
    • None
    • 17
    • core-libs
    • None
    • b26

    Description

      Java.lang.Process should provide Reader and Writer access to process standard and error output and input streams.
      The Readers and Writer streams convert from and to the byte streams using a character set.

      The Charset can be provided as an argument if known.
      If not supplied, the file descriptor of the corresponding byte stream, (stdout, stderr, or stdin) is consulted.
      If the file descriptor is a terminal (natively istty()), the charset associated with the terminal via `java.io.Console` is used,
      otherwise the charset is `Charset.defaultCharset()`.


      Methods added to java.lang.Process:

           public BufferedReader inputReader() {...}
           public BufferedReader inputReader(Charset charset) {...}

           public BufferedReader errorReader() {...}
           public BufferedReader errorReader(Charset charset) {...}

           public PrintWriter outputWriter() {...}
           public PrintWriter outputWriter(Charset charset) {...}


      Example Reading Lines:

          Process p = new ProcessBuilder("cat").start();
               // Write a line to the process
              try (PrintWriter writer = p.outputWriter()) {
                  writer.println("Now is the time: " + LocalDateTime.now());
              }
              // Read back all the lines
              try (BufferedReader reader = p.inputReader()) {
                  String line;
                  while ((line = reader.readLine()) != null) {
                      System.out.println(line);
                  }
              }

      Example Streaming Lines:

             Process p = new ProcessBuilder("cat", "x.tmp").start();
              // Read all the lines
              try (BufferedReader reader = p.inputReader()) {
                 reader.lines().forEach(s -> System.out.println(s));
              }

      Attachments

        Issue Links

          Activity

            People

              rriggs Roger Riggs
              rriggs Roger Riggs
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: