Replace magic numbers with constants with the new IOUtils.CR and LF.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -106,6 +106,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add StandardLineSeparator.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Replace magic numbers with constants with the new IOUtils.CR and LF.
+ </action>
<!-- UPDATES -->
<action dev="ggregory" type="update" due-to="Dependabot">
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
*/
public static final char DIR_SEPARATOR_WINDOWS = '\\';
+ /**
+ * CR char.
+ *
+ * @since 2.9.0
+ */
+ public static final int CR = '\r';
+
+ /**
+ * LF char.
+ *
+ * @since 2.9.0
+ */
+ public static final int LF = '\n';
+
/**
* Represents the end-of-file (or stream).
* @since 2.5 (made public)

MODIFY
src/main/java/org/apache/commons/io/input/Tailer.java
src/main/java/org/apache/commons/io/input/Tailer.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.IOUtils.CR;
import static org.apache.commons.io.IOUtils.EOF;
+import static org.apache.commons.io.IOUtils.LF;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -520,15 +522,15 @@ private long readLines(final RandomAccessFile reader) throws IOException {
for (int i = 0; i < num; i++) {
final byte ch = inbuf[i];
switch (ch) {
- case '\n':
+ case LF:
seenCR = false; // swallow CR before LF
listener.handle(new String(lineBuf.toByteArray(), charset));
lineBuf.reset();
rePos = pos + i + 1;
break;
- case '\r':
+ case CR:
if (seenCR) {
- lineBuf.write('\r');
+ lineBuf.write(CR);
}
seenCR = true;
break;

MODIFY
src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.IOUtils.CR;
import static org.apache.commons.io.IOUtils.EOF;
+import static org.apache.commons.io.IOUtils.LF;
import java.io.IOException;
import java.io.InputStream;
@@ -60,8 +62,8 @@ private int readWithUpdate() throws IOException {
if (eofSeen) {
return target;
}
- slashNSeen = target == '\n';
- slashRSeen = target == '\r';
+ slashNSeen = target == LF;
+ slashRSeen = target == CR;
return target;
}
@@ -79,7 +81,7 @@ public int read() throws IOException {
return eofGame(previousWasSlashR);
}
if (slashRSeen) {
- return '\n';
+ return LF;
}
if (previousWasSlashR && slashNSeen) {
@@ -100,7 +102,7 @@ private int eofGame(final boolean previousWasSlashR) {
}
if (!slashNSeen) {
slashNSeen = true;
- return '\n';
+ return LF;
}
return EOF;
}

MODIFY
src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.IOUtils.CR;
+import static org.apache.commons.io.IOUtils.EOF;
+import static org.apache.commons.io.IOUtils.LF;
+
import static org.apache.commons.io.IOUtils.EOF;
import java.io.IOException;
@@ -62,8 +66,8 @@ private int readWithUpdate() throws IOException {
if (eofSeen) {
return target;
}
- slashRSeen = target == '\r';
- slashNSeen = target == '\n';
+ slashRSeen = target == CR;
+ slashNSeen = target == LF;
return target;
}
@@ -76,17 +80,17 @@ public int read() throws IOException {
return eofGame();
} else if (injectSlashN) {
injectSlashN = false;
- return '\n';
+ return LF;
} else {
final boolean prevWasSlashR = slashRSeen;
final int target = readWithUpdate();
if (eofSeen) {
return eofGame();
}
- if (target == '\n') {
+ if (target == LF) {
if (!prevWasSlashR) {
injectSlashN = true;
- return '\r';
+ return CR;
}
}
return target;
@@ -103,12 +107,12 @@ private int eofGame() {
}
if (!slashNSeen && !slashRSeen) {
slashRSeen = true;
- return '\r';
+ return CR;
}
if (!slashNSeen) {
slashRSeen = false;
slashNSeen = true;
- return '\n';
+ return LF;
}
return EOF;
}

MODIFY
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
@@ -450,6 +450,9 @@ public synchronized void close() throws IOException {
assertEquals('/', IOUtils.DIR_SEPARATOR);
assertEquals("\n", IOUtils.LINE_SEPARATOR);
}
+ assertEquals('\r', IOUtils.CR);
+ assertEquals('\n', IOUtils.LF);
+ assertEquals(-1, IOUtils.EOF);
}
@Test

Replace magic numbers with constants with the new IOUtils.CR and LF.