Example #1
0
  /**
   * @param operation
   * @return
   * @throws IOException
   */
  private FastStringBuffer read(IProgressMonitor monitor) throws IOException {
    synchronized (ioLock) {
      if (finishedForGood) {
        throw new RuntimeException(
            "Shells are already finished for good, so, it is an invalid state to try to read from it.");
      }
      if (inStart) {
        throw new RuntimeException(
            "The shell is still not completely started, so, it is an invalid state to try to read from it.");
      }
      if (!isConnected) {
        throw new RuntimeException(
            "The shell is still not connected, so, it is an invalid state to try to read from it.");
      }
      if (isInRead) {
        throw new RuntimeException(
            "The shell is already in read mode, so, it is an invalid state to try to read from it.");
      }
      if (isInWrite) {
        throw new RuntimeException(
            "The shell is already in write mode, so, it is an invalid state to try to read from it.");
      }

      isInRead = true;

      try {
        FastStringBuffer str = new FastStringBuffer(AbstractShell.BUFFER_SIZE);
        byte[] b = new byte[AbstractShell.BUFFER_SIZE];
        while (true) {

          int len = this.socket.getInputStream().read(b);
          if (len == 0) {
            break;
          }

          String s = new String(b, 0, len);
          str.append(s);

          if (str.indexOf("END@@") != -1) {
            break;
          } else {
            sleepALittle(10);
          }
        }

        str.replaceFirst("@@COMPLETIONS", "");
        // remove END@@
        try {
          if (str.indexOf("END@@") != -1) {
            str.setCount(str.indexOf("END@@"));
            return str;
          } else {
            throw new RuntimeException("Couldn't find END@@ on received string.");
          }
        } catch (RuntimeException e) {
          if (str.length() > 500) {
            str.setCount(499)
                .append("...(continued)..."); // if the string gets too big, it can crash Eclipse...
          }
          Log.log(IStatus.ERROR, ("ERROR WITH STRING:" + str), e);
          return new FastStringBuffer();
        }
      } finally {
        isInRead = false;
      }
    }
  }