@@ -110,13 +110,15 @@ public static String readLine(String data){
110110 //**************************************************************************
111111 //** readLine
112112 //**************************************************************************
113- /** Returns a substring for the given data, ending at the first line break
114- * that is not inside a quote. Example usage:
113+ /** Returns a row of data from an InputStream. This method will read
114+ * characters one at a time until it reaches a line break that is not
115+ * inside a double quote. Depending on the source of the InputStream, this
116+ * method may be significantly slower than the other readLine() method that
117+ * uses a BufferedReader. Example usage:
115118 <pre>
116119
117- //Get input stream
118- javaxt.io.File file; //create file!
119- java.io.InputStream is = file.getInputStream();
120+ //Create an input stream
121+ java.io.InputStream is = ...
120122
121123 //Read header
122124 String header = CSV.readLine(is);
@@ -130,15 +132,13 @@ public static String readLine(String data){
130132 console.log(row);
131133 }
132134
133- //Close input stream
134- is.close();
135135 </pre>
136136 */
137137 public static String readLine (java .io .InputStream is ) throws java .io .IOException {
138138
139139 StringBuilder str = new StringBuilder ();
140140 boolean insideDoubleQuotes = false ;
141- int i = 0 ;
141+ int i ;
142142 while ((i =is .read ())!=-1 ) {
143143 char c = (char ) i ;
144144
@@ -158,6 +158,58 @@ public static String readLine(java.io.InputStream is) throws java.io.IOException
158158 }
159159
160160
161+ //**************************************************************************
162+ //** readLine
163+ //**************************************************************************
164+ /** Returns a row of data from a BufferedReader. Unlike the BufferedReader
165+ * readLine() method, this method will not stop at line breaks inside a
166+ * double quote. Note that a BufferedReader is significantly faster than
167+ * an InputStream when reading files. Example usage:
168+ <pre>
169+
170+ //Open input stream from an javaxt.io.File
171+ try (java.io.BufferedReader is = file.getBufferedReader("UTF-8)){
172+
173+ //Read header
174+ String header = CSV.readLine(is);
175+ int bom = CSV.getByteOrderMark(header);
176+ if (bom>-1) header = header.substring(bom);
177+ console.log(header);
178+
179+ //Read rows
180+ String row;
181+ while (!(row=CSV.readLine(is)).isEmpty()){
182+ console.log(row);
183+ }
184+
185+ }
186+ </pre>
187+ */
188+ public static String readLine (java .io .BufferedReader reader ) throws java .io .IOException {
189+
190+ StringBuilder str = new StringBuilder ();
191+ boolean insideDoubleQuotes = false ;
192+ int i ;
193+
194+ while ((i =reader .read ())!=-1 ) {
195+ char c = (char ) i ;
196+
197+ if ((c =='\r' || c =='\n' ) && str .length ()==0 ) continue ;
198+
199+ if (c =='"' ){
200+ if (insideDoubleQuotes ) insideDoubleQuotes = false ;
201+ else insideDoubleQuotes = true ;
202+ }
203+
204+ if (c =='\r' || c =='\n' ){
205+ if (!insideDoubleQuotes ) break ;
206+ }
207+ str .append (c );
208+ }
209+ return str .toString ();
210+ }
211+
212+
161213 //**************************************************************************
162214 //** getByteOrderMark
163215 //**************************************************************************
0 commit comments