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

ArrayIndexOutOfBoundsException when trying to read record written by CSVPrinter using CSVParser with same format

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Not A Problem
    • 1.5, 1.6, 1.7
    • 1.8
    • Parser
    • None

    Description

      The following code will fail with the exception 

      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

      import org.apache.commons.csv.*;
      
      import java.io.StringReader;
      import java.io.StringWriter;
      import java.util.List;
      
      public class Poc {
      
          public static void main(String[] args) throws Exception {
      
              CSVFormat csvFormat = CSVFormat.DEFAULT
                      .withEscape('\\');
      
              StringWriter stringWriter = new StringWriter();
      
              CSVPrinter printer = new CSVPrinter(stringWriter, csvFormat);
              printer.printRecord("foo \\", "bar");
      
              StringReader stringReader = new StringReader(stringWriter.toString());
              CSVParser parser = new CSVParser(stringReader, csvFormat);
              List<CSVRecord> records = parser.getRecords();
      
              records.forEach(record -> {
                  System.out.println(record.get(0));
                  System.out.println(record.get(1));
              });
      
          }
      }

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jelmer1 Jelmer Kuperus
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: