Add ReversedLinesFileReader.toString(int).
MODIFY
src/changes/changes.xml
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...).

MODIFY
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
@@ -202,24 +202,18 @@ private FilePart rollOver() throws IOException {
}
}
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 String readLine() throws IOException {
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();
+ }
+
}

MODIFY
src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestSimple.java
src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestSimple.java
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;
@@ -58,6 +59,21 @@ public void testLineCount() throws URISyntaxException, IOException {
}
}
+ @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());

Add ReversedLinesFileReader.toString(int).