PageRenderTime 67ms CodeModel.GetById 38ms app.highlight 19ms RepoModel.GetById 5ms app.codeStats 0ms

/bundles/plugins-trunk/Console/console/StreamThread.java

#
Java | 228 lines | 159 code | 26 blank | 43 comment | 21 complexity | 73cff8633d8a3bc488ad37bf40bb3ede MD5 | raw file
  1/*
  2 * StreamThread.java - A running process
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2005 Slava Pestov, Alan Ezust, Marcelo Vanzin
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package console;
 24
 25// {{{ Imports
 26
 27import java.awt.Color;
 28import java.io.IOException;
 29import java.io.InputStream;
 30import java.io.InputStreamReader;
 31import java.io.UnsupportedEncodingException;
 32
 33import javax.swing.text.AttributeSet;
 34
 35import org.gjt.sp.jedit.jEdit;
 36import org.gjt.sp.util.Log;
 37
 38import errorlist.DefaultErrorSource;
 39
 40// }}}
 41
 42/**
 43 * Thread for handing output of running sub-processes.
 44 *
 45 * @version $Id: StreamThread.java 13506 2008-09-02 17:07:48Z ezust $
 46 */
 47
 48// {{{ class StreamThread
 49class StreamThread extends Thread
 50{
 51	// {{{ Private members
 52	private ConsoleProcess process;
 53
 54	private boolean aborted;
 55
 56	private InputStream in;
 57	CommandOutputParser copt = null;
 58
 59	private StringBuilder lineBuffer;
 60	private boolean pendingCr;
 61	private int uncoloredWritten;
 62	// }}}
 63
 64	// {{{ StreamThread constructor
 65	/**
 66	 * @param showStatus - prints the error status when the thread is finished.
 67	 */
 68	StreamThread(ConsoleProcess process, InputStream in, Color defaultColor)
 69	{
 70		this.process = process;
 71		this.in = in;
 72		String currentDirectory = process.getCurrentDirectory();
 73		Console console = process.getConsole();
 74		DefaultErrorSource es = console.getErrorSource();
 75		copt = new CommandOutputParser(console.getView(), es, defaultColor);
 76		copt.setDirectory(currentDirectory);
 77		lineBuffer = new StringBuilder(100);
 78		pendingCr = false;
 79		uncoloredWritten = 0;
 80	} // }}}
 81
 82	// {{{ run() method
 83	public void run()
 84	{
 85		InputStreamReader isr = null;
 86		try
 87		{
 88			 isr = new InputStreamReader(in, jEdit.getProperty("console.encoding") );
 89		}
 90		catch (UnsupportedEncodingException uee)
 91		{
 92			throw new RuntimeException(uee);
 93		}
 94
 95		Output output = process.getOutput();
 96		try
 97		{
 98			char[] input = new char[1024];
 99			while (!aborted)
100			{
101				int read = isr.read(input, 0, input.length);
102				if (aborted)
103				{
104					break;
105				}
106				else if (read == -1)
107				{
108					if (pendingCr)
109					{
110						flushLine(output, "\r");
111					}
112					else if (lineBuffer.length() > 0)
113					{
114						flushLine(output, "");
115					}
116					break;
117				}
118
119				for (int i = 0; i < read; i++)
120				{
121					char c = input[i];
122					if (c == '\n')
123					{
124						if (pendingCr)
125						{
126							flushLine(output, "\r\n");
127						}
128						else
129						{
130							flushLine(output, "\n");
131						}
132					}
133					else
134					{
135						if (pendingCr)
136						{
137							flushLine(output, "\r");
138						}
139		
140						if (c == '\r')
141						{
142							pendingCr = true;
143						}
144						else
145						{
146							lineBuffer.append(c);
147						}
148					}
149				}
150
151				// Following output shows unterminated lines
152				// such as prompt of interactive programs.
153				if (lineBuffer.length() > uncoloredWritten)
154				{
155					String tail = lineBuffer.substring(uncoloredWritten);
156					output.writeAttrs(null, tail);
157					uncoloredWritten += tail.length();
158				}
159			}
160		}
161		catch (Exception e)
162		{
163			if (!aborted)
164			{
165				Log.log(Log.ERROR, e, e);
166				Console console = process.getConsole();
167				Output error = process.getErrorOutput();
168				if (console != null)
169				{
170					String[] args = { e.toString() };
171					error.print(console.getErrorColor(), jEdit.getProperty(
172						"console.shell.error", args));
173				}
174			}
175		}
176		finally
177		{
178			copt.finishErrorParsing();
179			try
180			{
181				in.close();
182			}
183			catch (IOException e2)
184			{
185			}
186
187			process.threadDone();
188
189
190		}
191	} // }}}
192
193	// {{{ abort() method
194	void abort()
195	{
196		aborted = true;
197		interrupt();
198	} // }}}
199
200	// {{{ flushLine() method
201	private void flushLine(Output output, String eol)
202	{
203		// we need to write the line break to the output, but we
204		// can't pass it to the "processLine()" method or the
205		// regexps won't recognize anything.
206		String line = lineBuffer.toString();
207		copt.processLine(line);
208		AttributeSet color = ConsolePane.colorAttributes(copt.getColor());
209		if (uncoloredWritten > 0)
210		{
211			output.setAttrs(uncoloredWritten, color);
212			output.writeAttrs(color,
213				lineBuffer.substring(uncoloredWritten) + eol);
214		}
215		else try 
216		{
217			output.writeAttrs(color, line + eol);
218		} catch (Exception err) {
219			Log.log (Log.ERROR, this, "Can't Flush:", err);
220		}
221
222		lineBuffer.setLength(0);
223		pendingCr = false;
224		uncoloredWritten = 0;
225	} //}}}
226
227} // }}}
228