PageRenderTime 29ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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