static List < String > csvLines ( Reader reader ) { // This needs to handle quoted text that spans multiple lines, // so we divide the full text into chunks that correspond to // a single csv line try { BufferedReader br = new BufferedReader ( reader ) ; List < String > lines = new ArrayList <> ( ) ; // The current line read from the Reader String line ; // The full csv line that may span multiple lines String longLine = null ; while ( ( line = br . readLine ( ) ) != null ) { // If we have a line from the previous iteration, // we concatenate it if ( longLine == null ) { longLine = line ; } else { longLine = longLine . concat ( "\n" ) . concat ( line ) ; } // Count the number of quotes: if it's even, the csv line // must end here. If not, it will continue to the next if ( isEvenQuotes ( longLine ) ) { lines . add ( longLine ) ; longLine = null ; } } // If there is text leftover, the line was not closed propertly. // XXX: we need to figure out how to handle errors like this if ( longLine != null ) { lines . add ( longLine ) ; } return lines ; } catch ( IOException ex ) { throw new RuntimeException ( "Couldn't process data" , ex ) ; } }
Divides the whole text into lines .