Uploaded image for project: 'Commons CSV'
  1. Commons CSV
  2. CSV-259

CSVFormat.printWithEscapes throws StringIndexOutOfBoundsException when value is Reader

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.8
    • Fix Version/s: 1.9.0
    • Component/s: Printer
    • Labels:
      None
    • Environment:

      windows 10

      Description

      //代码占位符
      @Test
      public void testPrintWithEscapeReader() throws IOException {
          final StringWriter sw = new StringWriter();
          final CSVFormat format = CSVFormat.DEFAULT.withEscape('!').withQuoteMode(QuoteMode.NONE);
          Reader reader = new FileReader("src/test/resources/testPrintWithReader.txt");
          try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
              printer.print(reader);
              assertEquals("q!,w!,e", sw.toString());
          }
      }
      

      and the values in testPrintWithReader.txt is

      //代码占位符
      q,w,e
      

      after tracing the code I think the problem cause by here

      //代码占位符
      private void printWithEscapes(final Reader reader, final Appendable out) throws IOException {
          int start = 0;
          int pos = 0;
      
          final char delim = getDelimiter();
          final char escape = getEscapeCharacter().charValue();
          final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
      
          int c;
          while (-1 != (c = reader.read())) {
              builder.append((char) c);
              if (c == CR || c == LF || c == delim || c == escape) {
                  // write out segment up until this char
                  if (pos > start) {
                      out.append(builder.substring(start, pos));
                      builder.setLength(0);
                  }
                  if (c == LF) {
                      c = 'n';
                  } else if (c == CR) {
                      c = 'r';
                  }
      
                  out.append(escape);
                  out.append((char) c);
      
                  start = pos + 1; // start on the current char after this one
              }
              pos++;
          }
      
          // write last segment
          if (pos > start) {
              out.append(builder.substring(start, pos));
          }
      }
      

      that line 

      //代码占位符
      builder.setLength(0);
      

      will cause the exception. After delete that line , the testcase will passed.

      is that a bug? may be I could contribute to it

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Guoping1 Chen
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 10m
                  10m