You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/12/12 16:05:04 UTC

[commons-io] 05/05: Add StandardLineSeparator.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit 645804fde89fde6c1c42501515def5f731f2c9e3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 12 11:04:54 2020 -0500

    Add StandardLineSeparator.
---
 src/changes/changes.xml                            |  3 +
 src/main/java/org/apache/commons/io/IOUtils.java   |  8 ++-
 .../apache/commons/io/StandardLineSeparator.java   | 74 ++++++++++++++++++++++
 .../commons/io/input/ReversedLinesFileReader.java  |  8 ++-
 .../commons/io/StandardLineSeparatorTest.java      | 65 +++++++++++++++++++
 .../ReversedLinesFileReaderTestParamBlockSize.java |  6 +-
 6 files changed, 158 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 530f4ef..6bd0ef0 100644
--- a/src/changes/changes.xml
+++ b/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.
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java
index d545973..ecfd89a 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -143,13 +143,17 @@ public class IOUtils {
 
     /**
      * 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.
diff --git a/src/main/java/org/apache/commons/io/StandardLineSeparator.java b/src/main/java/org/apache/commons/io/StandardLineSeparator.java
new file mode 100644
index 0000000..9008dde
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/StandardLineSeparator.java
@@ -0,0 +1,74 @@
+/*
+ * 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;
+    }
+}
diff --git a/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java b/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
index 5d45b6f..56d223a 100644
--- a/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
+++ b/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
@@ -34,6 +34,7 @@ import java.util.List;
 
 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 class ReversedLinesFileReader implements Closeable {
 
         // 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;
 
diff --git a/src/test/java/org/apache/commons/io/StandardLineSeparatorTest.java b/src/test/java/org/apache/commons/io/StandardLineSeparatorTest.java
new file mode 100644
index 0000000..f37562d
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/StandardLineSeparatorTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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));
+    }
+
+}
diff --git a/src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestParamBlockSize.java b/src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestParamBlockSize.java
index fcd9dce..7794f49 100644
--- a/src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestParamBlockSize.java
+++ b/src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestParamBlockSize.java
@@ -16,6 +16,8 @@
  */
 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 @@ public class ReversedLinesFileReaderTestParamBlockSize {
 
     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);
     }