public int available() {
   try {
     return reader.available();
   } catch (Exception e) {
     return -1;
   }
 }
Пример #2
0
  public synchronized void run() {

    byte[] buffer = new byte[BUFFER_SIZE];

    for (; ; ) {
      try {
        this.wait(100);
      } catch (InterruptedException ie) {
      }

      int len = 0;
      try {
        int noBytes = pin.available();

        if (noBytes > 0) {
          len = pin.read(buffer, 0, Math.min(noBytes, BUFFER_SIZE));
          if (len > 0) {
            jTextArea.append(new String(buffer, 0, len));
            jTextArea.setCaretPosition(jTextArea.getText().length());
          }
        }
      } catch (IOException ioe) {
        throw new UIError("Unable to read from input stream! " + ioe.getMessage());
      }
    }
  }
Пример #3
0
 public synchronized String readLine(PipedInputStream in) throws IOException {
   String input = "";
   do {
     int available = in.available();
     if (available == 0) {
       break;
     }
     byte b[] = new byte[available];
     in.read(b);
     input = input + new String(b, 0, b.length);
   } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
   return input;
 }
  /**
   * ************************************************************************* CONCRETE METHOD::
   * ReadFilterInputPort Purpose: This method reads data from the input port one byte at a time.
   *
   * <p>Arguments: void
   *
   * <p>Returns: byte of data read from the input port of the filter.
   *
   * <p>Exceptions: IOExecption, EndOfStreamException (rethrown)
   *
   * <p>**************************************************************************
   */
  byte ReadFilterInputPort() throws EndOfStreamException {
    byte datum = 0;

    /**
     * ********************************************************************* Since delays are
     * possible on upstream filters, we first wait until there is data available on the input port.
     * We check,... if no data is available on the input port we wait for a quarter of a second and
     * check again. Note there is no timeout enforced here at all and if upstream filters are
     * deadlocked, then this can result in infinite waits in this loop. It is necessary to check to
     * see if we are at the end of stream in the wait loop because it is possible that the upstream
     * filter completes while we are waiting. If this happens and we do not check for the end of
     * stream, then we could wait forever on an upstream pipe that is long gone. Unfortunately Java
     * pipes do not throw exceptions when the input pipe is broken.
     * *********************************************************************
     */
    try {
      while (InputReadPort.available() == 0) {
        if (EndOfInputStream()) {
          throw new EndOfStreamException("End of input stream reached");
        } // if

        sleep(250);
      } // while

    } // try
    catch (EndOfStreamException Error) {
      throw Error;

    } // catch
    catch (Exception Error) {
      System.out.println("\n" + this.getName() + " Error in read port wait loop::" + Error);
    } // catch

    /**
     * ********************************************************************* If at least one byte of
     * data is available on the input pipe we can read it. We read and write one byte to and from
     * ports. *********************************************************************
     */
    try {
      datum = (byte) InputReadPort.read();
      return datum;

    } // try
    catch (Exception Error) {
      System.out.println("\n" + this.getName() + " Pipe read error::" + Error);
      return datum;
    } // catch
  } // ReadFilterPort
Пример #5
0
  /**
   * **************************************************
   *
   * <p>private int joinWithPossibleTimeout(ProcStarter proc, final TaskListener listener,
   * StringBuffer strBuf, AbstractBuild currentBuild) throws IOException, InterruptedException
   *
   * <p>**************************************************
   */
  protected int joinWithPossibleTimeout(
      ProcStarter proc,
      final TaskListener listener,
      StringBuffer strBuf,
      AbstractBuild currentBuild,
      String stringToHide)
      throws IOException, InterruptedException {
    boolean useTimeout = configuration.isUseTimeout();
    long timeoutValue = configuration.getTimeoutValue();

    int result = -1;

    try {
      PipedInputStream pis = null;
      if (strBuf != null) {
        PipedOutputStream pos = new PipedOutputStream();
        pis = new PipedInputStream(pos, 1000000);
        proc = proc.stdout(pos);
      }

      hudson.Proc procStarted = proc.start();
      if (useTimeout) {
        result = procStarted.joinWithTimeout(timeoutValue, TimeUnit.SECONDS, listener);
      } else {
        result = procStarted.join();
      }

      if (strBuf != null) {
        byte[] stdoutDataArr = new byte[pis.available()];
        pis.read(stdoutDataArr, 0, stdoutDataArr.length);
        String stdoutStr = new String(stdoutDataArr);
        if (stringToHide != null) {
          stdoutStr = stdoutStr.replaceAll(stringToHide, "****");
        }
        strBuf.append(stdoutStr);
        PrintStream output = listener.getLogger();
        output.println(stdoutStr);
      }
    } catch (InterruptedException e) {
      throw e;
    } catch (Exception e) {
      if (listener != null) {
        listener.error("Exception caught in joinWithPossibleTimeout: " + e);
      }
    }

    return result;
  } // End: joinWithPossibleTimeout(...)