Add StandardLineSeparator.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -103,6 +103,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Rob Spoor, Gary Gregory">
Add QueueInputStream and QueueOutputStream as simpler alternatives to PipedInputStream and PipedOutputStream #171.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add StandardLineSeparator.
+ </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
/**
* The Unix line separator string.
+ *
+ * @see StandardLineSeparator#LF
*/
- public static final String LINE_SEPARATOR_UNIX = "\n";
+ public static final String LINE_SEPARATOR_UNIX = StandardLineSeparator.LF.getString();
/**
* The Windows line separator string.
+ *
+ * @see StandardLineSeparator#CRLF
*/
- public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
+ public static final String LINE_SEPARATOR_WINDOWS = StandardLineSeparator.CRLF.getString();
/**
* The default buffer to use for the skip() methods.

ADD
src/main/java/org/apache/commons/io/StandardLineSeparator.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io;
+
+import java.nio.charset.Charset;
+import java.util.Objects;
+
+/**
+ * Enumerates standard line separators: {@link #CR}, {@link #CRLF}, {@link #LF}.
+ *
+ * @since 2.9.0
+ */
+public enum StandardLineSeparator {
+
+ /**
+ * Carriage return. This is the line ending used on Macos 9 and earlier.
+ */
+ CR("\r"),
+
+ /**
+ * Carriage return followed by line feed. This is the line ending used on Windows.
+ */
+ CRLF("\r\n"),
+
+ /**
+ * Line feed. This is the line ending used on Linux and Macos X and later.
+ */
+ LF("\n");
+
+ private final String lineSeparator;
+
+ /**
+ * Constructs a new instance for a non-null line separator.
+ *
+ * @param lineSeparator a non-null line separator.
+ */
+ StandardLineSeparator(final String lineSeparator) {
+ this.lineSeparator = Objects.requireNonNull(lineSeparator);
+ }
+
+ /**
+ * Gets the bytes for this instance encoded using the given Charset.
+ *
+ * @param charset the encoding Charset.
+ * @return the bytes for this instance encoded using the given Charset.
+ */
+ public byte[] getBytes(final Charset charset) {
+ return lineSeparator.getBytes(charset);
+ }
+
+ /**
+ * Gets the String value of this instance.
+ *
+ * @return the String value of this instance.
+ */
+ public String getString() {
+ return lineSeparator;
+ }
+}

MODIFY
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.StandardLineSeparator;
/**
* Reads lines in a file reversely (similar to a BufferedReader, but starting at
@@ -337,8 +338,11 @@ public ReversedLinesFileReader(final Path file, final int blockSize, final Chars
// NOTE: The new line sequences are matched in the order given, so it is
// important that \r\n is BEFORE \n
- this.newLineSequences = new byte[][] { "\r\n".getBytes(this.charset), "\n".getBytes(this.charset),
- "\r".getBytes(this.charset) };
+ this.newLineSequences = new byte[][] {
+ StandardLineSeparator.CRLF.getBytes(this.charset),
+ StandardLineSeparator.LF.getBytes(this.charset),
+ StandardLineSeparator.CR.getBytes(this.charset)
+ };
this.avoidNewlineSplitBufferSize = newLineSequences[0].length;

ADD
src/test/java/org/apache/commons/io/StandardLineSeparatorTest.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io;
+
+import static org.apache.commons.io.StandardLineSeparator.CR;
+import static org.apache.commons.io.StandardLineSeparator.CRLF;
+import static org.apache.commons.io.StandardLineSeparator.LF;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.nio.charset.StandardCharsets;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link StandardLineSeparator}.
+ */
+public class StandardLineSeparatorTest {
+
+ @Test
+ public void testCR() {
+ assertEquals("\r", CR.getString());
+ }
+
+ @Test
+ public void testCR_getBytes() {
+ assertArrayEquals("\r".getBytes(StandardCharsets.ISO_8859_1), CR.getBytes(StandardCharsets.ISO_8859_1));
+ }
+
+ @Test
+ public void testCRLF() {
+ assertEquals("\r\n", CRLF.getString());
+ }
+
+ @Test
+ public void testCRLF_getBytes() {
+ assertArrayEquals("\r\n".getBytes(StandardCharsets.ISO_8859_1), CRLF.getBytes(StandardCharsets.ISO_8859_1));
+ }
+
+ @Test
+ public void testLF() {
+ assertEquals("\n", LF.getString());
+ }
+
+ @Test
+ public void testLF_getBytes() {
+ assertArrayEquals("\n".getBytes(StandardCharsets.ISO_8859_1), LF.getBytes(StandardCharsets.ISO_8859_1));
+ }
+
+}

MODIFY
src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestParamBlockSize.java
src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestParamBlockSize.java
*/
package org.apache.commons.io.input;
+import static org.apache.commons.io.StandardLineSeparator.CR;
+import static org.apache.commons.io.StandardLineSeparator.LF;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -237,8 +239,8 @@ private void assertFileWithShrinkingTestLines(final ReversedLinesFileReader reve
static void assertEqualsAndNoLineBreaks(final String msg, final String expected, final String actual) {
if (actual != null) {
- assertFalse(actual.contains("\n"), "Line contains \\n: line=" + actual);
- assertFalse(actual.contains("\r"), "Line contains \\r: line=" + actual);
+ assertFalse(actual.contains(LF.getString()), "Line contains \\n: line=" + actual);
+ assertFalse(actual.contains(CR.getString()), "Line contains \\r: line=" + actual);
}
assertEquals(expected, actual, msg);
}

Add StandardLineSeparator.