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

ByteArrayOutputStream.toString is inefficient

XMLWordPrintable

    • 1.1.1
    • sparc
    • solaris_2.4
    • Not verified

      From ###@###.### Thu Oct 26 17:25:58 1995
      From: ###@###.### (Jonathan Payne)
      To: arthur.vanhoff@Eng, ###@###.###, Thomas.Ball@Eng, flar@Eng,
              ###@###.###, Sami.Shaio@Eng, lindholm@Eng, pavani@Eng
      Subject: unnecessary inefficiency

      In ByteArrayOutputStream:

          /**
           * Returns a copy of the input data.
           */
          public synchronized byte toByteArray()[] {
      byte newbuf[] = new byte[count];
      System.arraycopy(buf, 0, newbuf, 0, count);
      return newbuf;
          }

          /**
           * Converts input data to a string.
           * @return the string.
           */
          public String toString() {
      return new String(toByteArray(), 0);
          }


      There is no reason this toString() method has to make a copy of its
      existing byte array that's exactly the right size, and then pass that
      byte array to String, only to have String throw it away. String has a
      constructor which takes an offset and a length.

      The above could be:

          public String toString() {
      return new String(buf, 0, 0, count);
          }

      using the

          public String(byte ascii[], int hibyte, int offset, int count)

      constructor in String.

      I am finding myself very memory conscious now, with the stuff I am
      working on ...

            mr Mark Reinhold
            ahoffsunw Arthur Hoff (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: