You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemStreamRedirectThread.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.swingdebug;
  23. import com.dmdirc.ui.messages.IRCDocument;
  24. import java.io.BufferedReader;
  25. import java.io.IOException;
  26. import java.io.InputStreamReader;
  27. import java.io.PipedInputStream;
  28. import java.io.PipedOutputStream;
  29. import java.io.PrintStream;
  30. /**
  31. * Simple utility class to redirect System streams to the specified Document.
  32. */
  33. public class SystemStreamRedirectThread implements Runnable {
  34. /** Is this thread running? */
  35. private boolean running = false;
  36. /** Reader to for the system stream. */
  37. private final BufferedReader reader;
  38. /** Stream identifier. */
  39. private final SystemStreamType stream;
  40. /** Document to output stream into. */
  41. private final IRCDocument document;
  42. /** Original System stream. */
  43. private PrintStream originalStream;
  44. /**
  45. * Constructs a new redirection thread.
  46. *
  47. * @param stream System stream to redirect
  48. * @param document Document to redirect stream into
  49. *
  50. * @throws IOException On error redirecting stream
  51. */
  52. public SystemStreamRedirectThread(final SystemStreamType stream,
  53. final IRCDocument document) throws IOException {
  54. super();
  55. this.stream = stream;
  56. this.document = document;
  57. final PipedInputStream in = new PipedInputStream();
  58. final PipedOutputStream out = new PipedOutputStream(in);
  59. reader = new BufferedReader(new InputStreamReader(in));
  60. switch (stream) {
  61. case Out:
  62. originalStream = System.out;
  63. System.setOut(new PrintStream(out));
  64. break;
  65. case Error:
  66. originalStream = System.err;
  67. System.setErr(new PrintStream(out));
  68. break;
  69. default:
  70. throw new IllegalArgumentException("Unknown stream type: "
  71. + stream);
  72. }
  73. }
  74. /** {@inheritDoc} */
  75. @Override
  76. public void run() {
  77. running = true;
  78. while (running) {
  79. try {
  80. if (reader.ready()) {
  81. document.addText(new String[]{reader.readLine(), });
  82. } else {
  83. try {
  84. Thread.sleep(500);
  85. } catch (InterruptedException ex) {
  86. //Ignore
  87. }
  88. }
  89. Thread.yield();
  90. } catch (IOException ex) {
  91. running = false;
  92. }
  93. }
  94. }
  95. /**
  96. * Starts the thread adding text to the document.
  97. */
  98. public void start() {
  99. final Thread thread = new Thread(this,
  100. "System stream redirector (" + stream + ")");
  101. thread.setDaemon(true);
  102. thread.start();
  103. }
  104. /**
  105. * Cancels the thread adding text to the document.
  106. */
  107. public void cancel() {
  108. running = false;
  109. switch (stream) {
  110. case Out:
  111. System.setOut(originalStream);
  112. break;
  113. case Error:
  114. System.setErr(originalStream);
  115. break;
  116. default:
  117. throw new IllegalArgumentException("Unknown stream type: "
  118. + stream);
  119. }
  120. }
  121. /**
  122. * Is this thread running?
  123. *
  124. * @return true iif running
  125. */
  126. public boolean isRunning() {
  127. return running;
  128. }
  129. }