PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 1ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package console;
  23. // {{{ Imports
  24. import java.awt.Color;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.InputStreamReader;
  28. import java.io.UnsupportedEncodingException;
  29. import javax.swing.text.AttributeSet;
  30. import org.gjt.sp.jedit.jEdit;
  31. import org.gjt.sp.util.Log;
  32. import errorlist.DefaultErrorSource;
  33. // }}}
  34. /**
  35. * Thread for handing output of running sub-processes.
  36. *
  37. * @version $Id: StreamThread.java 13506 2008-09-02 17:07:48Z ezust $
  38. */
  39. // {{{ class StreamThread
  40. class StreamThread extends Thread
  41. {
  42. // {{{ Private members
  43. private ConsoleProcess process;
  44. private boolean aborted;
  45. private InputStream in;
  46. CommandOutputParser copt = null;
  47. private StringBuilder lineBuffer;
  48. private boolean pendingCr;
  49. private int uncoloredWritten;
  50. // }}}
  51. // {{{ StreamThread constructor
  52. /**
  53. * @param showStatus - prints the error status when the thread is finished.
  54. */
  55. StreamThread(ConsoleProcess process, InputStream in, Color defaultColor)
  56. {
  57. this.process = process;
  58. this.in = in;
  59. String currentDirectory = process.getCurrentDirectory();
  60. Console console = process.getConsole();
  61. DefaultErrorSource es = console.getErrorSource();
  62. copt = new CommandOutputParser(console.getView(), es, defaultColor);
  63. copt.setDirectory(currentDirectory);
  64. lineBuffer = new StringBuilder(100);
  65. pendingCr = false;
  66. uncoloredWritten = 0;
  67. } // }}}
  68. // {{{ run() method
  69. public void run()
  70. {
  71. InputStreamReader isr = null;
  72. try
  73. {
  74. isr = new InputStreamReader(in, jEdit.getProperty("console.encoding") );
  75. }
  76. catch (UnsupportedEncodingException uee)
  77. {
  78. throw new RuntimeException(uee);
  79. }
  80. Output output = process.getOutput();
  81. try
  82. {
  83. char[] input = new char[1024];
  84. while (!aborted)
  85. {
  86. int read = isr.read(input, 0, input.length);
  87. if (aborted)
  88. {
  89. break;
  90. }
  91. else if (read == -1)
  92. {
  93. if (pendingCr)
  94. {
  95. flushLine(output, "\r");
  96. }
  97. else if (lineBuffer.length() > 0)
  98. {
  99. flushLine(output, "");
  100. }
  101. break;
  102. }
  103. for (int i = 0; i < read; i++)
  104. {
  105. char c = input[i];
  106. if (c == '\n')
  107. {
  108. if (pendingCr)
  109. {
  110. flushLine(output, "\r\n");
  111. }
  112. else
  113. {
  114. flushLine(output, "\n");
  115. }
  116. }
  117. else
  118. {
  119. if (pendingCr)
  120. {
  121. flushLine(output, "\r");
  122. }
  123. if (c == '\r')
  124. {
  125. pendingCr = true;
  126. }
  127. else
  128. {
  129. lineBuffer.append(c);
  130. }
  131. }
  132. }
  133. // Following output shows unterminated lines
  134. // such as prompt of interactive programs.
  135. if (lineBuffer.length() > uncoloredWritten)
  136. {
  137. String tail = lineBuffer.substring(uncoloredWritten);
  138. output.writeAttrs(null, tail);
  139. uncoloredWritten += tail.length();
  140. }
  141. }
  142. }
  143. catch (Exception e)
  144. {
  145. if (!aborted)
  146. {
  147. Log.log(Log.ERROR, e, e);
  148. Console console = process.getConsole();
  149. Output error = process.getErrorOutput();
  150. if (console != null)
  151. {
  152. String[] args = { e.toString() };
  153. error.print(console.getErrorColor(), jEdit.getProperty(
  154. "console.shell.error", args));
  155. }
  156. }
  157. }
  158. finally
  159. {
  160. copt.finishErrorParsing();
  161. try
  162. {
  163. in.close();
  164. }
  165. catch (IOException e2)
  166. {
  167. }
  168. process.threadDone();
  169. }
  170. } // }}}
  171. // {{{ abort() method
  172. void abort()
  173. {
  174. aborted = true;
  175. interrupt();
  176. } // }}}
  177. // {{{ flushLine() method
  178. private void flushLine(Output output, String eol)
  179. {
  180. // we need to write the line break to the output, but we
  181. // can't pass it to the "processLine()" method or the
  182. // regexps won't recognize anything.
  183. String line = lineBuffer.toString();
  184. copt.processLine(line);
  185. AttributeSet color = ConsolePane.colorAttributes(copt.getColor());
  186. if (uncoloredWritten > 0)
  187. {
  188. output.setAttrs(uncoloredWritten, color);
  189. output.writeAttrs(color,
  190. lineBuffer.substring(uncoloredWritten) + eol);
  191. }
  192. else try
  193. {
  194. output.writeAttrs(color, line + eol);
  195. } catch (Exception err) {
  196. Log.log (Log.ERROR, this, "Can't Flush:", err);
  197. }
  198. lineBuffer.setLength(0);
  199. pendingCr = false;
  200. uncoloredWritten = 0;
  201. } //}}}
  202. } // }}}