コード例 #1
0
  public void run() {
    runs = true;

    // notification about starting the input process
    stream.postEvent(new ServerEvent(this, stream, ServerEvent.INPUT_START));

    changeState(new HeaderDetectionState(this, stream));

    byte[] buffer = new byte[65535];
    int offset = 0;
    int length = 0;
    while (runs && stream.running()) {

      try {

        // starting time of the transfer
        long transferStart = new Date().getTime();

        // reading data
        int red = input.read(buffer, offset, buffer.length - offset);

        // notification about the transfer
        stream.postEvent(
            new TransferEvent(
                this,
                stream,
                TransferEvent.STREAM_INPUT,
                red,
                new Date().getTime() - transferStart));

        if (red == -1) runs = false;
        length += red;

        int newOffset = currentState.processData(buffer, 0, length);
        if (newOffset < offset + length) {
          length = length - newOffset;
          System.arraycopy(buffer, newOffset, buffer, 0, length);
          offset = length;
        } else {
          length = 0;
          offset = 0;
        }

      } catch (SocketTimeoutException e) {
        continue;
      } catch (Exception e) {
        e.printStackTrace();
        runs = false;
      }
    }

    try {
      input.close();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    // notification about ending the input process
    stream.postEvent(new ServerEvent(this, stream, ServerEvent.INPUT_STOP));
  }