Skip to content

Commit 9e71df2

Browse files
author
Gary Gregory
committed
Replace magic numbers with constants with the new IOUtils.CR and LF.
1 parent 645804f commit 9e71df2

6 files changed

Lines changed: 42 additions & 14 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ The <action> type attribute can be add,update,fix,remove.
106106
<action dev="ggregory" type="add" due-to="Gary Gregory">
107107
Add StandardLineSeparator.
108108
</action>
109+
<action dev="ggregory" type="add" due-to="Gary Gregory">
110+
Replace magic numbers with constants with the new IOUtils.CR and LF.
111+
</action>
109112
<!-- UPDATES -->
110113
<action dev="ggregory" type="update" due-to="Dependabot">
111114
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ public class IOUtils {
127127
*/
128128
public static final char DIR_SEPARATOR_WINDOWS = '\\';
129129

130+
/**
131+
* CR char.
132+
*
133+
* @since 2.9.0
134+
*/
135+
public static final int CR = '\r';
136+
137+
/**
138+
* LF char.
139+
*
140+
* @since 2.9.0
141+
*/
142+
public static final int LF = '\n';
143+
130144
/**
131145
* Represents the end-of-file (or stream).
132146
* @since 2.5 (made public)

src/main/java/org/apache/commons/io/input/Tailer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19+
import static org.apache.commons.io.IOUtils.CR;
1920
import static org.apache.commons.io.IOUtils.EOF;
21+
import static org.apache.commons.io.IOUtils.LF;
2022

2123
import java.io.ByteArrayOutputStream;
2224
import java.io.File;
@@ -520,15 +522,15 @@ private long readLines(final RandomAccessFile reader) throws IOException {
520522
for (int i = 0; i < num; i++) {
521523
final byte ch = inbuf[i];
522524
switch (ch) {
523-
case '\n':
525+
case LF:
524526
seenCR = false; // swallow CR before LF
525527
listener.handle(new String(lineBuf.toByteArray(), charset));
526528
lineBuf.reset();
527529
rePos = pos + i + 1;
528530
break;
529-
case '\r':
531+
case CR:
530532
if (seenCR) {
531-
lineBuf.write('\r');
533+
lineBuf.write(CR);
532534
}
533535
seenCR = true;
534536
break;

src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19+
import static org.apache.commons.io.IOUtils.CR;
1920
import static org.apache.commons.io.IOUtils.EOF;
21+
import static org.apache.commons.io.IOUtils.LF;
2022

2123
import java.io.IOException;
2224
import java.io.InputStream;
@@ -60,8 +62,8 @@ private int readWithUpdate() throws IOException {
6062
if (eofSeen) {
6163
return target;
6264
}
63-
slashNSeen = target == '\n';
64-
slashRSeen = target == '\r';
65+
slashNSeen = target == LF;
66+
slashRSeen = target == CR;
6567
return target;
6668
}
6769

@@ -79,7 +81,7 @@ public int read() throws IOException {
7981
return eofGame(previousWasSlashR);
8082
}
8183
if (slashRSeen) {
82-
return '\n';
84+
return LF;
8385
}
8486

8587
if (previousWasSlashR && slashNSeen) {
@@ -100,7 +102,7 @@ private int eofGame(final boolean previousWasSlashR) {
100102
}
101103
if (!slashNSeen) {
102104
slashNSeen = true;
103-
return '\n';
105+
return LF;
104106
}
105107
return EOF;
106108
}

src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19+
import static org.apache.commons.io.IOUtils.CR;
20+
import static org.apache.commons.io.IOUtils.EOF;
21+
import static org.apache.commons.io.IOUtils.LF;
22+
1923
import static org.apache.commons.io.IOUtils.EOF;
2024

2125
import java.io.IOException;
@@ -62,8 +66,8 @@ private int readWithUpdate() throws IOException {
6266
if (eofSeen) {
6367
return target;
6468
}
65-
slashRSeen = target == '\r';
66-
slashNSeen = target == '\n';
69+
slashRSeen = target == CR;
70+
slashNSeen = target == LF;
6771
return target;
6872
}
6973

@@ -76,17 +80,17 @@ public int read() throws IOException {
7680
return eofGame();
7781
} else if (injectSlashN) {
7882
injectSlashN = false;
79-
return '\n';
83+
return LF;
8084
} else {
8185
final boolean prevWasSlashR = slashRSeen;
8286
final int target = readWithUpdate();
8387
if (eofSeen) {
8488
return eofGame();
8589
}
86-
if (target == '\n') {
90+
if (target == LF) {
8791
if (!prevWasSlashR) {
8892
injectSlashN = true;
89-
return '\r';
93+
return CR;
9094
}
9195
}
9296
return target;
@@ -103,12 +107,12 @@ private int eofGame() {
103107
}
104108
if (!slashNSeen && !slashRSeen) {
105109
slashRSeen = true;
106-
return '\r';
110+
return CR;
107111
}
108112
if (!slashNSeen) {
109113
slashRSeen = false;
110114
slashNSeen = true;
111-
return '\n';
115+
return LF;
112116
}
113117
return EOF;
114118
}

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ public synchronized void close() throws IOException {
450450
assertEquals('/', IOUtils.DIR_SEPARATOR);
451451
assertEquals("\n", IOUtils.LINE_SEPARATOR);
452452
}
453+
assertEquals('\r', IOUtils.CR);
454+
assertEquals('\n', IOUtils.LF);
455+
assertEquals(-1, IOUtils.EOF);
453456
}
454457

455458
@Test

0 commit comments

Comments
 (0)