()
| 64 | |
| 65 | |
| 66 | @Override |
| 67 | public void run() { |
| 68 | try { |
| 69 | // process data until we hit EOF; this will happily block |
| 70 | // (effectively sleeping the thread) until new data comes in. |
| 71 | // when the program is finally done, null will come through. |
| 72 | // |
| 73 | StringBuilder currentLine = new StringBuilder(); |
| 74 | long lineStartTime = 0; |
| 75 | while (canRun) { |
| 76 | // First, try to read as many characters as possible. Take care |
| 77 | // not to block when: |
| 78 | // 1. lineTimeout is nonzero, and |
| 79 | // 2. we have some characters buffered already |
| 80 | while (lineTimeout == 0 || currentLine.length() == 0 || streamReader.ready()) { |
| 81 | int c = streamReader.read(); |
| 82 | if (c == -1) |
| 83 | return; // EOF |
| 84 | if (!canRun) |
| 85 | return; |
| 86 | |
| 87 | // Keep track of the line start time |
| 88 | if (currentLine.length() == 0) |
| 89 | lineStartTime = System.nanoTime(); |
| 90 | |
| 91 | // Store the character line |
| 92 | currentLine.append((char)c); |
| 93 | |
| 94 | if (c == '\n') { |
| 95 | // We read a full line, pass it on |
| 96 | consumer.message(currentLine.toString()); |
| 97 | currentLine.setLength(0); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // No more characters available. Wait until lineTimeout |
| 102 | // milliseconds have passed since the start of the line and then |
| 103 | // try reading again. If the time has already passed, then just |
| 104 | // pass on the characters read so far. |
| 105 | long passed = (System.nanoTime() - lineStartTime) / 1000; |
| 106 | if (passed < this.lineTimeout) { |
| 107 | Thread.sleep(this.lineTimeout - passed); |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | consumer.message(currentLine.toString()); |
| 112 | currentLine.setLength(0); |
| 113 | } |
| 114 | //EditorConsole.systemOut.println("messaging thread done"); |
| 115 | } catch (NullPointerException npe) { |
| 116 | // Fairly common exception during shutdown |
| 117 | } catch (SocketException e) { |
| 118 | // socket has been close while we were wainting for data. nothing to see here, move along |
| 119 | } catch (Exception e) { |
| 120 | // On Linux and sometimes on Mac OS X, a "bad file descriptor" |
| 121 | // message comes up when closing an applet that's run externally. |
| 122 | // That message just gets supressed here.. |
| 123 | String mess = e.getMessage(); |
nothing calls this directly
no test coverage detected