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:04:59 UTC

[commons-io] branch master updated (11c1f95 -> 645804f)

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

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


    from 11c1f95  Javadoc.
     new 13cf0b4  Javadoc.
     new 4901672  Javadoc.
     new c604d60  Replace magic number with constant.
     new 61da746  Format tweak.
     new 645804f  Add StandardLineSeparator.

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 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 ++-
 .../io/input/UnixLineEndingInputStream.java        |  9 +--
 .../io/input/WindowsLineEndingInputStream.java     | 11 ++--
 .../commons/io/StandardLineSeparatorTest.java      | 65 +++++++++++++++++++
 .../ReversedLinesFileReaderTestParamBlockSize.java |  6 +-
 8 files changed, 169 insertions(+), 15 deletions(-)
 create mode 100644 src/main/java/org/apache/commons/io/StandardLineSeparator.java
 create mode 100644 src/test/java/org/apache/commons/io/StandardLineSeparatorTest.java


[commons-io] 04/05: Format tweak.

Posted by gg...@apache.org.
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 61da746dbb0f7f2c59629e143016f43f75bfa92a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 12 10:59:56 2020 -0500

    Format tweak.
---
 .../java/org/apache/commons/io/input/WindowsLineEndingInputStream.java   | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java b/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
index 9253fd8..ea5426e 100644
--- a/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
@@ -97,7 +97,6 @@ public class WindowsLineEndingInputStream  extends InputStream {
      * Handles the EOF-handling at the end of the stream
      * @return The next char to output to the stream
      */
-
     private int eofGame() {
         if (!ensureLineFeedAtEndOfFile) {
             return EOF;


[commons-io] 02/05: Javadoc.

Posted by gg...@apache.org.
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 4901672998292f9c666e408e78af32143c038a7b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 12 10:27:59 2020 -0500

    Javadoc.
---
 .../java/org/apache/commons/io/input/UnixLineEndingInputStream.java     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
index ef2aecc..8fc1d11 100644
--- a/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
@@ -21,7 +21,7 @@ import java.io.IOException;
 import java.io.InputStream;
 
 /**
- * A filtering input stream that ensures the content will have unix-style line endings, LF.
+ * A filtering input stream that ensures the content will have UNIX-style line endings, LF.
  *
  * @since 2.5
  */


[commons-io] 01/05: Javadoc.

Posted by gg...@apache.org.
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 13cf0b4c2bd3bf20f73ca5361fd30452122ab4ff
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 12 10:22:26 2020 -0500

    Javadoc.
---
 .../java/org/apache/commons/io/input/WindowsLineEndingInputStream.java  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java b/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
index 1d0da0c..85405d2 100644
--- a/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
@@ -20,7 +20,7 @@ import java.io.IOException;
 import java.io.InputStream;
 
 /**
- * A filtering input stream that ensures the content will have windows line endings, CRLF.
+ * A filtering input stream that ensures the content will have Windows line endings, CRLF.
  *
  * @since 2.5
  */


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

Posted by gg...@apache.org.
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);
     }


[commons-io] 03/05: Replace magic number with constant.

Posted by gg...@apache.org.
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 c604d60e0f41a91707cac93cff04ad15874280df
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Dec 12 10:59:18 2020 -0500

    Replace magic number with constant.
---
 .../org/apache/commons/io/input/UnixLineEndingInputStream.java    | 7 ++++---
 .../org/apache/commons/io/input/WindowsLineEndingInputStream.java | 8 +++++---
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
index 8fc1d11..56a1b86 100644
--- a/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.io.input;
 
+import static org.apache.commons.io.IOUtils.EOF;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -55,7 +56,7 @@ public class UnixLineEndingInputStream extends InputStream {
      */
     private int readWithUpdate() throws IOException {
         final int target = this.target.read();
-        eofSeen = target == -1;
+        eofSeen = target == EOF;
         if (eofSeen) {
             return target;
         }
@@ -95,13 +96,13 @@ public class UnixLineEndingInputStream extends InputStream {
      */
     private int eofGame(final boolean previousWasSlashR) {
         if (previousWasSlashR || !ensureLineFeedAtEndOfFile) {
-            return -1;
+            return EOF;
         }
         if (!slashNSeen) {
             slashNSeen = true;
             return '\n';
         }
-        return -1;
+        return EOF;
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java b/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
index 85405d2..9253fd8 100644
--- a/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.io.input;
 
+import static org.apache.commons.io.IOUtils.EOF;
+
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -56,7 +58,7 @@ public class WindowsLineEndingInputStream  extends InputStream {
      */
     private int readWithUpdate() throws IOException {
         final int target = this.target.read();
-        eofSeen = target == -1;
+        eofSeen = target == EOF;
         if (eofSeen) {
             return target;
         }
@@ -98,7 +100,7 @@ public class WindowsLineEndingInputStream  extends InputStream {
 
     private int eofGame() {
         if (!ensureLineFeedAtEndOfFile) {
-            return -1;
+            return EOF;
         }
         if (!slashNSeen && !slashRSeen) {
             slashRSeen = true;
@@ -109,7 +111,7 @@ public class WindowsLineEndingInputStream  extends InputStream {
             slashNSeen = true;
             return '\n';
         }
-        return -1;
+        return EOF;
     }
 
     /**