/src/org/ooc/utils/ProcessUtils.java

http://github.com/nddrylliog/ooc · Java · 133 lines · 55 code · 33 blank · 45 comment · 4 complexity · f418078cd98deecf1a66c23fc3ea86a6 MD5 · raw file

  1. package org.ooc.utils;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.io.Reader;
  7. import java.io.Writer;
  8. /**
  9. * Utilities about relaying I/O between processes launched from Java
  10. * and streams, writers, loggers, etc.
  11. *
  12. * @author Amos Wenger
  13. */
  14. public class ProcessUtils {
  15. /** The size, in bytes or chars, of a buffer used by a relay */
  16. public static int BUFFER_SIZE = 4096;
  17. /**
  18. * A stream relay pipes data from an input stream to an output stream
  19. * @author Amos Wenger
  20. */
  21. public static class StreamRelay {
  22. protected final InputStream inStream;
  23. protected final OutputStream outStream;
  24. /**
  25. * Create a new relay between an input and an output stream.
  26. * @param inStream
  27. * @param outStream
  28. */
  29. public StreamRelay(InputStream inStream, OutputStream outStream) {
  30. this.inStream = inStream;
  31. this.outStream = outStream;
  32. }
  33. /**
  34. * Update the relay
  35. * @return
  36. * @throws IOException
  37. */
  38. public boolean update() throws IOException {
  39. byte[] buffer = new byte[BUFFER_SIZE];
  40. int numRead;
  41. if((numRead = inStream.read(buffer)) != -1) {
  42. outStream.write(buffer, 0, numRead);
  43. return true;
  44. }
  45. return false;
  46. }
  47. }
  48. /**
  49. * A stream relay pipes data from an input stream to an output stream
  50. * @author Amos Wenger
  51. */
  52. public static class CharRelay {
  53. protected final Reader reader;
  54. protected final Writer writer;
  55. /**
  56. * Create a new relay between an input stream and a writer.
  57. * @param inStream
  58. * @param outStream
  59. */
  60. public CharRelay(InputStream inStream, Writer writer) {
  61. this.reader = new InputStreamReader(inStream);
  62. this.writer = writer;
  63. }
  64. /**
  65. * Start the char relay
  66. */
  67. public void start() {
  68. try {
  69. char[] buffer = new char[BUFFER_SIZE];
  70. int numRead;
  71. while((numRead = reader.read(buffer)) != -1) {
  72. writer.write(buffer, 0, numRead);
  73. }
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. }
  79. /**
  80. * Bind standard output/error stream to a process's output/error stream
  81. * @param process
  82. * @throws IOException
  83. */
  84. public static void redirectIO(final Process process) throws IOException {
  85. StreamRelay relay1 = new StreamRelay(process.getInputStream(), System.out);
  86. StreamRelay relay2 = new StreamRelay(process.getErrorStream(), System.err);
  87. while(relay2.update() || relay1.update()) {
  88. // enjoy =)
  89. }
  90. }
  91. /**
  92. * Bind a writer to a process's output stream
  93. * @param process
  94. * @param writer a Writer to which output the specified process input/error stream.
  95. * If null, the standard output/error streams are used
  96. */
  97. public static void redirectIO(final Process process, final Writer writer) {
  98. new CharRelay(process.getInputStream(), writer).start();
  99. }
  100. }