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


The following commit(s) were added to refs/heads/master by this push:
     new 2acd50b  Add ReversedLinesFileReader.toString(int).
2acd50b is described below

commit 2acd50bf440156d53418355f1571e4784a2b8a85
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Aug 18 15:47:48 2020 -0400

    Add ReversedLinesFileReader.toString(int).
---
 src/changes/changes.xml                            |  3 +++
 .../commons/io/input/ReversedLinesFileReader.java  | 27 +++++++++++++++-------
 .../input/ReversedLinesFileReaderTestSimple.java   | 16 +++++++++++++
 3 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2ef2517..d975dea 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -95,6 +95,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add ReversedLinesFileReader.readLines(int).
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add ReversedLinesFileReader.toString(int).
+      </action>
       <action issue="IO-684" dev="ggregory" type="add" due-to="Gary Gregory, 
Robin Jansohn">
         Add PathUtils.delete(Path, DeleteOption...).
         Add PathUtils.deleteDirectory(Path, DeleteOption...).
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 872222e..a82ae58 100644
--- a/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
+++ b/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
@@ -202,24 +202,18 @@ public class ReversedLinesFileReader implements Closeable 
{
         }
     }
     private static final String EMPTY_STRING = "";
-
     private static final int DEFAULT_BLOCK_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
-    private final int blockSize;
 
+    private final int blockSize;
     private final Charset encoding;
-
     private final SeekableByteChannel channel;
     private final long totalByteLength;
-
     private final long totalBlockCount;
     private final byte[][] newLineSequences;
     private final int avoidNewlineSplitBufferSize;
-
     private final int byteDecrement;
-
     private FilePart currentFilePart;
-
-    private boolean trailingNewlineOfFileSkipped = false;
+    private boolean trailingNewlineOfFileSkipped;
 
     /**
      * Creates a ReversedLinesFileReader with default block size of 4KB and the
@@ -452,4 +446,21 @@ public class ReversedLinesFileReader implements Closeable {
         return arrayList;
     }
 
+    /**
+     * Returns the last {@code lineCount} lines of the file.
+     * <p>
+     * If there are less than {@code lineCount} lines in the file, then that's 
what you get.
+     * </p>
+     *
+     * @param lineCount How many lines to read.
+     * @return A String.
+     * @throws IOException if an I/O error occurs
+     * @since 2.8.0
+     */
+    public String toString(final int lineCount) throws IOException {
+        final List<String> lines = readLines(lineCount);
+        Collections.reverse(lines);
+        return lines.isEmpty() ? EMPTY_STRING : 
String.join(System.lineSeparator(), lines) + System.lineSeparator();
+    }
+
 }
diff --git 
a/src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestSimple.java
 
b/src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestSimple.java
index 3f4a7c9..912fb69 100644
--- 
a/src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestSimple.java
+++ 
b/src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestSimple.java
@@ -17,6 +17,7 @@
 package org.apache.commons.io.input;
 
 import static 
org.apache.commons.io.input.ReversedLinesFileReaderTestParamBlockSize.assertEqualsAndNoLineBreaks;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -59,6 +60,21 @@ public class ReversedLinesFileReaderTestSimple {
     }
 
     @Test
+    public void testToString() throws URISyntaxException, IOException {
+        final int blockSize = 10;
+        final File testFile20Bytes = new 
File(this.getClass().getResource("/test-file-20byteslength.bin").toURI());
+        try (ReversedLinesFileReader reversedLinesFileReader = new 
ReversedLinesFileReader(testFile20Bytes, blockSize,
+            "ISO-8859-1")) {
+            assertThrows(IllegalArgumentException.class, () -> 
reversedLinesFileReader.toString(-1));
+            assertTrue(reversedLinesFileReader.readLines(0).isEmpty());
+            final String lines = reversedLinesFileReader.toString(2);
+            assertEquals("123456789" + System.lineSeparator() + "987654321" + 
System.lineSeparator(), lines);
+            assertTrue(reversedLinesFileReader.toString(0).isEmpty());
+            assertTrue(reversedLinesFileReader.toString(10000).isEmpty());
+        }
+    }
+
+    @Test
     public void testUnsupportedEncodingUTF16() throws URISyntaxException {
         final File testFileEmpty = new 
File(this.getClass().getResource("/test-file-empty.bin").toURI());
         assertThrows(UnsupportedEncodingException.class,

Reply via email to