Skip to content

Commit 5e5e28c

Browse files
committed
Internal refactoring
Reduce whitepace
1 parent 1eb95dc commit 5e5e28c

6 files changed

Lines changed: 46 additions & 47 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.apache.commons.csv.Constants.PIPE;
2929
import static org.apache.commons.csv.Constants.SP;
3030
import static org.apache.commons.csv.Constants.TAB;
31+
import static org.apache.commons.io.IOUtils.EOF;
3132

3233
import java.io.File;
3334
import java.io.FileOutputStream;
@@ -2172,7 +2173,7 @@ private void printWithEscapes(final Reader reader, final Appendable appendable)
21722173
final char escape = getEscapeCharacter().charValue();
21732174
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
21742175
int c;
2175-
while (-1 != (c = bufferedReader.read())) {
2176+
while (EOF != (c = bufferedReader.read())) {
21762177
builder.append((char) c);
21772178
final boolean isDelimiterStart = isDelimiter((char) c, builder.toString() + new String(bufferedReader.lookAhead(delimLength - 1)), pos, delim,
21782179
delimLength);
@@ -2321,12 +2322,12 @@ private void printWithQuotes(final Reader reader, final Appendable appendable) t
23212322
printWithEscapes(reader, appendable);
23222323
return;
23232324
}
2324-
int pos = 0;
23252325
final char quote = getQuoteCharacter().charValue();
2326-
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
23272326
append(quote, appendable);
2327+
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
23282328
int c;
2329-
while (-1 != (c = reader.read())) {
2329+
int pos = 0;
2330+
while (EOF != (c = reader.read())) {
23302331
builder.append((char) c);
23312332
if (c == quote) {
23322333
// write out segment up until this char

src/main/java/org/apache/commons/csv/Constants.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.commons.csv;
1919

2020
/**
21-
* Constants for this package.
21+
* Private constants to this package.
2222
*/
2323
final class Constants {
2424

@@ -44,9 +44,6 @@ final class Constants {
4444

4545
static final String[] EMPTY_STRING_ARRAY = {};
4646

47-
/** The end of stream symbol */
48-
static final int END_OF_STREAM = -1;
49-
5047
static final char FF = '\f';
5148

5249
static final char LF = '\n';

src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
package org.apache.commons.csv;
1919

2020
import static org.apache.commons.csv.Constants.CR;
21-
import static org.apache.commons.csv.Constants.END_OF_STREAM;
2221
import static org.apache.commons.csv.Constants.LF;
2322
import static org.apache.commons.csv.Constants.UNDEFINED;
23+
import static org.apache.commons.io.IOUtils.EOF;
2424

2525
import java.io.BufferedReader;
2626
import java.io.IOException;
@@ -63,7 +63,7 @@ final class ExtendedBufferedReader extends BufferedReader {
6363
public void close() throws IOException {
6464
// Set ivars before calling super close() in case close() throws an IOException.
6565
closed = true;
66-
lastChar = END_OF_STREAM;
66+
lastChar = EOF;
6767
super.close();
6868
}
6969

@@ -74,7 +74,7 @@ public void close() throws IOException {
7474
*/
7575
long getCurrentLineNumber() {
7676
// Check if we are at EOL or EOF or just starting
77-
if (lastChar == CR || lastChar == LF || lastChar == UNDEFINED || lastChar == END_OF_STREAM) {
77+
if (lastChar == CR || lastChar == LF || lastChar == UNDEFINED || lastChar == EOF) {
7878
return eolCounter; // counter is accurate
7979
}
8080
return eolCounter + 1; // Allow for counter being incremented only at EOL
@@ -84,7 +84,7 @@ long getCurrentLineNumber() {
8484
* Returns the last character that was read as an integer (0 to 65535). This will be the last character returned by
8585
* any of the read methods. This will not include a character read using the {@link #lookAhead()} method. If no
8686
* character has been read then this will return {@link Constants#UNDEFINED}. If the end of the stream was reached
87-
* on the last read then this will return {@link Constants#END_OF_STREAM}.
87+
* on the last read then this will return {@link Constants#EOF}.
8888
*
8989
* @return the last character that was read
9090
*/
@@ -158,7 +158,7 @@ char[] lookAhead(final int n) throws IOException {
158158
public int read() throws IOException {
159159
final int current = super.read();
160160
if (current == CR || current == LF && lastChar != CR ||
161-
current == END_OF_STREAM && lastChar != CR && lastChar != LF && lastChar != END_OF_STREAM) {
161+
current == EOF && lastChar != CR && lastChar != LF && lastChar != EOF) {
162162
eolCounter++;
163163
}
164164
lastChar = current;
@@ -189,8 +189,8 @@ public int read(final char[] buf, final int offset, final int length) throws IOE
189189

190190
lastChar = buf[offset + len - 1];
191191

192-
} else if (len == -1) {
193-
lastChar = END_OF_STREAM;
192+
} else if (len == EOF) {
193+
lastChar = EOF;
194194
}
195195

196196
position += len;
@@ -204,14 +204,14 @@ public int read(final char[] buf, final int offset, final int length) throws IOE
204204
* Increments {@link #eolCounter} and updates {@link #position}.
205205
* </p>
206206
* <p>
207-
* Sets {@link #lastChar} to {@link Constants#END_OF_STREAM} at EOF, otherwise the last EOL character.
207+
* Sets {@link #lastChar} to {@link Constants#EOF} at EOF, otherwise the last EOL character.
208208
* </p>
209209
*
210210
* @return the line that was read, or null if reached EOF.
211211
*/
212212
@Override
213213
public String readLine() throws IOException {
214-
if (lookAhead() == END_OF_STREAM) {
214+
if (lookAhead() == EOF) {
215215
return null;
216216
}
217217
final StringBuilder buffer = new StringBuilder();
@@ -223,7 +223,7 @@ public String readLine() throws IOException {
223223
read();
224224
}
225225
}
226-
if (current == END_OF_STREAM || current == LF || current == CR) {
226+
if (current == EOF || current == LF || current == CR) {
227227
break;
228228
}
229229
buffer.append((char) current);

src/main/java/org/apache/commons/csv/Lexer.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919

2020
import static org.apache.commons.csv.Constants.BACKSPACE;
2121
import static org.apache.commons.csv.Constants.CR;
22-
import static org.apache.commons.csv.Constants.END_OF_STREAM;
2322
import static org.apache.commons.csv.Constants.FF;
2423
import static org.apache.commons.csv.Constants.LF;
2524
import static org.apache.commons.csv.Constants.TAB;
2625
import static org.apache.commons.csv.Constants.UNDEFINED;
2726
import static org.apache.commons.csv.Token.Type.COMMENT;
28-
import static org.apache.commons.csv.Token.Type.EOF;
2927
import static org.apache.commons.csv.Token.Type.EORECORD;
3028
import static org.apache.commons.csv.Token.Type.INVALID;
3129
import static org.apache.commons.csv.Token.Type.TOKEN;
30+
import static org.apache.commons.io.IOUtils.EOF;
3231

3332
import java.io.Closeable;
3433
import java.io.IOException;
@@ -141,7 +140,7 @@ boolean isDelimiter(final int ch) throws IOException {
141140
}
142141
}
143142
final int count = reader.read(delimiterBuf, 0, delimiterBuf.length);
144-
isLastTokenDelimiter = count != END_OF_STREAM;
143+
isLastTokenDelimiter = count != EOF;
145144
return isLastTokenDelimiter;
146145
}
147146

@@ -151,7 +150,7 @@ boolean isDelimiter(final int ch) throws IOException {
151150
* @return true if the given character indicates the end of the file.
152151
*/
153152
boolean isEndOfFile(final int ch) {
154-
return ch == END_OF_STREAM;
153+
return ch == EOF;
155154
}
156155

157156
/**
@@ -182,7 +181,7 @@ boolean isEscapeDelimiter() throws IOException {
182181
}
183182
}
184183
final int count = reader.read(escapeDelimiterBuf, 0, escapeDelimiterBuf.length);
185-
return count != END_OF_STREAM;
184+
return count != EOF;
186185
}
187186

188187
private boolean isMetaChar(final int ch) {
@@ -240,7 +239,7 @@ Token nextToken(final Token token) throws IOException {
240239
eol = readEndOfLine(c);
241240
// reached the end of the file without any content (empty line at the end)
242241
if (isEndOfFile(c)) {
243-
token.type = EOF;
242+
token.type = Token.Type.EOF;
244243
// don't set token.isReady here because no content
245244
return token;
246245
}
@@ -249,15 +248,15 @@ Token nextToken(final Token token) throws IOException {
249248

250249
// Did we reach EOF during the last iteration already? EOF
251250
if (isEndOfFile(lastChar) || !isLastTokenDelimiter && isEndOfFile(c)) {
252-
token.type = EOF;
251+
token.type = Token.Type.EOF;
253252
// don't set token.isReady here because no content
254253
return token;
255254
}
256255

257256
if (isStartOfLine(lastChar) && isCommentStart(c)) {
258257
final String line = reader.readLine();
259258
if (line == null) {
260-
token.type = EOF;
259+
token.type = Token.Type.EOF;
261260
// don't set token.isReady here because no content
262261
return token;
263262
}
@@ -291,7 +290,7 @@ Token nextToken(final Token token) throws IOException {
291290
} else if (isEndOfFile(c)) {
292291
// end of file return EOF()
293292
// noop: token.content.append("");
294-
token.type = EOF;
293+
token.type = Token.Type.EOF;
295294
token.isReady = true; // there is data at EOF
296295
} else {
297296
// next token must be a simple token
@@ -337,7 +336,7 @@ private Token parseEncapsulatedToken(final Token token) throws IOException {
337336
token.content.append(delimiter);
338337
} else {
339338
final int unescaped = readEscape();
340-
if (unescaped == END_OF_STREAM) { // unexpected char after escape
339+
if (unescaped == EOF) { // unexpected char after escape
341340
token.content.append((char) c).append((char) reader.getLastChar());
342341
} else {
343342
token.content.append((char) unescaped);
@@ -357,7 +356,7 @@ private Token parseEncapsulatedToken(final Token token) throws IOException {
357356
return token;
358357
}
359358
if (isEndOfFile(c)) {
360-
token.type = EOF;
359+
token.type = Token.Type.EOF;
361360
token.isReady = true; // There is data at EOF
362361
return token;
363362
}
@@ -411,7 +410,7 @@ private Token parseSimpleToken(final Token token, int ch) throws IOException {
411410
break;
412411
}
413412
if (isEndOfFile(ch)) {
414-
token.type = EOF;
413+
token.type = Token.Type.EOF;
415414
token.isReady = true; // There is data at EOF
416415
break;
417416
}
@@ -425,7 +424,7 @@ private Token parseSimpleToken(final Token token, int ch) throws IOException {
425424
token.content.append(delimiter);
426425
} else {
427426
final int unescaped = readEscape();
428-
if (unescaped == END_OF_STREAM) { // unexpected char after escape
427+
if (unescaped == EOF) { // unexpected char after escape
429428
token.content.append((char) ch).append((char) reader.getLastChar());
430429
} else {
431430
token.content.append((char) unescaped);
@@ -478,7 +477,7 @@ boolean readEndOfLine(int ch) throws IOException {
478477
* On return, the next character is available by calling {@link ExtendedBufferedReader#getLastChar()}
479478
* on the input stream.
480479
*
481-
* @return the unescaped character (as an int) or {@link Constants#END_OF_STREAM} if char following the escape is
480+
* @return the unescaped character (as an int) or {@link Constants#EOF} if char following the escape is
482481
* invalid.
483482
* @throws IOException if there is a problem reading the stream or the end of stream is detected:
484483
* the escape character is not allowed at end of stream
@@ -503,15 +502,15 @@ int readEscape() throws IOException {
503502
case TAB: // TODO is this correct? Do tabs need to be escaped?
504503
case BACKSPACE: // TODO is this correct?
505504
return ch;
506-
case END_OF_STREAM:
505+
case EOF:
507506
throw new IOException("EOF whilst processing escape sequence");
508507
default:
509508
// Now check for meta-characters
510509
if (isMetaChar(ch)) {
511510
return ch;
512511
}
513512
// indicate unexpected char - available from in.getLastChar()
514-
return END_OF_STREAM;
513+
return EOF;
515514
}
516515
}
517516

src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.apache.commons.csv;
1919

20-
import static org.apache.commons.csv.Constants.END_OF_STREAM;
20+
import static org.apache.commons.io.IOUtils.EOF;
2121
import static org.apache.commons.csv.Constants.UNDEFINED;
2222
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2323
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -39,9 +39,9 @@ private ExtendedBufferedReader createBufferedReader(final String s) {
3939
@Test
4040
public void testEmptyInput() throws Exception {
4141
try (final ExtendedBufferedReader br = createBufferedReader("")) {
42-
assertEquals(END_OF_STREAM, br.read());
43-
assertEquals(END_OF_STREAM, br.lookAhead());
44-
assertEquals(END_OF_STREAM, br.getLastChar());
42+
assertEquals(EOF, br.read());
43+
assertEquals(EOF, br.lookAhead());
44+
assertEquals(EOF, br.getLastChar());
4545
assertNull(br.readLine());
4646
assertEquals(0, br.read(new char[10], 0, 0));
4747
}
@@ -69,15 +69,15 @@ public void testReadChar() throws Exception {
6969
}
7070
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
7171
assertEquals(0, br.getCurrentLineNumber());
72-
while (br.read() != -1) {
72+
while (br.read() != EOF) {
7373
// consume all
7474
}
7575
assertEquals(EOLeolct, br.getCurrentLineNumber());
7676
}
7777
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
7878
assertEquals(0, br.getCurrentLineNumber());
7979
final char[] buff = new char[10];
80-
while (br.read(buff, 0, 3) != -1) {
80+
while (br.read(buff, 0, 3) != EOF) {
8181
// consume all
8282
}
8383
assertEquals(EOLeolct, br.getCurrentLineNumber());
@@ -185,12 +185,12 @@ public void testReadLookahead1() throws Exception {
185185
assertEquals('\n', br.getLastChar());
186186
assertEquals(3, br.getCurrentLineNumber());
187187

188-
assertEquals(END_OF_STREAM, br.lookAhead());
188+
assertEquals(EOF, br.lookAhead());
189189
assertEquals('\n', br.getLastChar());
190-
assertEquals(END_OF_STREAM, br.read());
191-
assertEquals(END_OF_STREAM, br.getLastChar());
192-
assertEquals(END_OF_STREAM, br.read());
193-
assertEquals(END_OF_STREAM, br.lookAhead());
190+
assertEquals(EOF, br.read());
191+
assertEquals(EOF, br.getLastChar());
192+
assertEquals(EOF, br.read());
193+
assertEquals(EOF, br.lookAhead());
194194
assertEquals(3, br.getCurrentLineNumber());
195195

196196
}

src/test/java/org/apache/commons/csv/PerformanceTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.io.IOUtils.EOF;
21+
2022
import java.io.BufferedReader;
2123
import java.io.File;
2224
import java.io.FileInputStream;
@@ -258,7 +260,7 @@ private static void testExtendedBuffer(final boolean makeString) throws Exceptio
258260
int read;
259261
if (makeString) {
260262
StringBuilder sb = new StringBuilder();
261-
while ((read = in.read()) != -1) {
263+
while ((read = in.read()) != EOF) {
262264
sb.append((char) read);
263265
if (read == ',') { // count delimiters
264266
sb.toString();
@@ -271,7 +273,7 @@ private static void testExtendedBuffer(final boolean makeString) throws Exceptio
271273
}
272274
}
273275
} else {
274-
while ((read = in.read()) != -1) {
276+
while ((read = in.read()) != EOF) {
275277
if (read == ',') { // count delimiters
276278
fields++;
277279
} else if (read == '\n') {

0 commit comments

Comments
 (0)