コード例 #1
0
  @Override
  public synchronized void run() {
    while (!quit) {
      //			filter_input();
      try {
        Thread.sleep(1); // poll at 1kHz
        // grab data from shared buffer and move it the local one
        ourBuffer.append(sharedBuffer.take());
        // System.out.print(ourBuffer);
        // create a matcher to find an open tag in the buffer
        int endCloseTag;
        int startOpenTag;
        Matcher closeTagMatcher = closeTagPattern.matcher(ourBuffer);

        // look or a closing tag in our buffer
        if (closeTagMatcher.find()) {
          endCloseTag = closeTagMatcher.end();
          openTag = "<" + ourBuffer.charAt(closeTagMatcher.start() + 2) + ">";
          // find corresponding opening tag
          startOpenTag = ourBuffer.indexOf(openTag);
          // send the input string to the GUI for processing
          if (startOpenTag >= 0 && startOpenTag < endCloseTag) {
            gui.incoming(ourBuffer.substring(startOpenTag, endCloseTag));
            // clear our buffer
            ourBuffer.delete(startOpenTag, endCloseTag);
          } else {
            ourBuffer.delete(0, endCloseTag);
          }
        }
      } catch (Exception e) {
        System.out.print(e + "\n");
      }
    }
  }