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

Poor performance of Writer#append with CharBuffer

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 9
    • 6u14
    • core-libs
    • b96
    • x86
    • windows_vista

    Description

      FULL PRODUCT VERSION :
      java version "1.6.0_14"
      Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
      Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.0.6002]


      A DESCRIPTION OF THE PROBLEM :
      The Writer append methods for CharSequence have poor performance for CharBuffer. When call Writer#append(CharBuffer) the charbuffer is converted to a string (full copy of chars), then to a char array, then wrapped back into a CharBuffer inside the StreamEncoder.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The CharBuffer should be passed all the way through to the stream encoder

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      CharBufferdata = CharBuffer.allocate(4 * 1024);
          File file = new File("blah");
          FileWriter writer = new FileWriter(file);
          writer.append(data);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Note: this only works for FileWriter, ideally it should be done in Writer. This is a hack to get inner array. Will not work for some CharBuffers without arrays.

      private static class MyFileWriter extends FileWriter {

          public MyFileWriter(File file) throws IOException {
            super(file);
          }

          @Override
          public Writer append(CharSequence csq, int start, int end) throws IOException {
            if (csq instanceof CharBuffer) {
              CharBuffer buffer = (CharBuffer) csq;
              if (buffer.hasArray()) {
                char[] chars = buffer.array();
                int len = end - start;
                int off = start + buffer.arrayOffset();
                write(chars, off, len);
                return this;
              }
              
            }
            
            return super.append(csq, start, end);
          }

          @Override
          public Writer append(CharSequence csq) throws IOException {
            return append(csq, 0, csq.length());
          }
          
          
          
        }

      Attachments

        Activity

          People

            vtewari Vyom Tewari
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: