You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2020/03/17 14:52:00 UTC

[jira] [Work logged] (CSV-259) CSVFormat.printWithEscapes throws StringIndexOutOfBoundsException when value is Reader

     [ https://issues.apache.org/jira/browse/CSV-259?focusedWorklogId=404769&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-404769 ]

ASF GitHub Bot logged work on CSV-259:
--------------------------------------

                Author: ASF GitHub Bot
            Created on: 17/Mar/20 14:51
            Start Date: 17/Mar/20 14:51
    Worklog Time Spent: 10m 
      Work Description: garydgregory commented on pull request #61: [CSV-259] CSVFormat.printWithEscapes throws StringIndexOutOfBoundsException when value is Reader
URL: https://github.com/apache/commons-csv/pull/61
 
 
   
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Issue Time Tracking
-------------------

            Worklog Id:     (was: 404769)
    Remaining Estimate: 0h
            Time Spent: 10m

> CSVFormat.printWithEscapes throws StringIndexOutOfBoundsException when value is Reader
> --------------------------------------------------------------------------------------
>
>                 Key: CSV-259
>                 URL: https://issues.apache.org/jira/browse/CSV-259
>             Project: Commons CSV
>          Issue Type: Bug
>          Components: Printer
>    Affects Versions: 1.8
>         Environment: windows 10
>            Reporter: Chen
>            Priority: Major
>             Fix For: 1.8.1
>
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> {code:java}
> //代码占位符
> @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());
>     }
> }
> {code}
> and the values in testPrintWithReader.txt is
> {code:java}
> //代码占位符
> q,w,e
> {code}
> after tracing the code I think the problem cause by here
> {code:java}
> //代码占位符
> 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));
>     }
> }
> {code}
> that line 
> {code:java}
> //代码占位符
> builder.setLength(0);
> {code}
> will cause the exception. After delete that line , the testcase will passed.
> is that a bug? may be I could contribute to it



--
This message was sent by Atlassian Jira
(v8.3.4#803005)