Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6246639

PipedInputStream.read() too slow - use System.arraycopy for bulk operations

XMLWordPrintable

    • b63
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      PipedInputStrea.read(byte[], int, int) copies one buffer into another, byte by byte, which is much slower than using System.arraycopy().

      JUSTIFICATION :
      This enhancement would improve the performance of reding from a piped input stream.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would like PipedInputStream.read(byte[], int, int) to use System.arraycopy for bulk copy operations.
      ACTUAL -
          public synchronized int read(byte b[], int off, int len) throws IOException {
      if (b == null) {
      throw new NullPointerException();
      } else if ((off < 0) || (off > b.length) || (len < 0) ||
      ((off + len) > b.length) || ((off + len) < 0)) {
      throw new IndexOutOfBoundsException();
      } else if (len == 0) {
      return 0;
      }

              /* possibly wait on the first character */
      int c = read();
      if (c < 0) {
      return -1;
      }
      b[off] = (byte) c;
      int rlen = 1;
      while ((in >= 0) && (--len > 0)) {
      b[off + rlen] = buffer[out++];
      rlen++;
      if (out >= buffer.length) {
      out = 0;
      }
      if (in == out) {
                      /* now empty */
      in = -1;
      }
      }
      return rlen;
          }

      CUSTOMER SUBMITTED WORKAROUND :
          public synchronized int read(byte b[], int off, int len) throws IOException {
      if (b == null) {
      throw new NullPointerException();
      } else if ((off < 0) || (off > b.length) || (len < 0) ||
      ((off + len) > b.length) || ((off + len) < 0)) {
      throw new IndexOutOfBoundsException();
      } else if (len == 0) {
      return 0;
      }

              /* possibly wait on the first character */
      int c = read();
      if (c < 0) {
      return -1;
      }
      b[off] = (byte) c;
      int rlen = 1;


      while (in >= 0 && len > 1) {

      int count;

      if (out < in) {
      count = Math.min(bufferFieldLen - out, in - out);
      } else {
      count = bufferFieldLen - out;
      }


      if (count > len - 1) {
      count = len - 1;
      }

      System.arraycopy(bufferField, out, b, off + rlen, count);

      out += count;
      rlen += count;
      len -= count;

      if (out >= bufferFieldLen) {
      out = 0;
      }

      if (in == out) {
                     // now empty
      in = -1;
      }
      }


      return rlen;
          }
      ###@###.### 2005-03-28 11:01:59 GMT

            jhangalsunw Jayalaxmi Hangal (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: