[
https://issues.apache.org/jira/browse/IO-543?focusedWorklogId=800444&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-800444
]
ASF GitHub Bot logged work on IO-543:
-------------------------------------
Author: ASF GitHub Bot
Created on: 13/Aug/22 18:05
Start Date: 13/Aug/22 18:05
Worklog Time Spent: 10m
Work Description: garydgregory commented on code in PR #39:
URL: https://github.com/apache/commons-io/pull/39#discussion_r945173243
##########
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java:
##########
@@ -467,4 +467,25 @@ public String toString(final int lineCount) throws
IOException {
return lines.isEmpty() ? EMPTY_STRING :
String.join(System.lineSeparator(), lines) + System.lineSeparator();
}
-}
+ /**
+ * Returns the current offset in this file
+ * @return the current offset
+ * @throws IOException if an I/O error occurs.
+ */
+ public long getFilePointer() throws IOException {
Review Comment:
Why not call the new method `position` since (1) there are no pointers in
Java and (2) it reflects the underlying operation?
##########
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java:
##########
@@ -467,4 +467,25 @@ public String toString(final int lineCount) throws
IOException {
return lines.isEmpty() ? EMPTY_STRING :
String.join(System.lineSeparator(), lines) + System.lineSeparator();
}
-}
+ /**
+ * Returns the current offset in this file
+ * @return the current offset
+ * @throws IOException if an I/O error occurs.
+ */
+ public long getFilePointer() throws IOException {
+ return channel.position();
+ }
+
+ /**
+ * Sets the file-pointer offset, measured from the beginning of this file,
at which the next read occurs.
+ * @param pos the offset value to be set
+ * @throws IOException if an I/O error occurs.
+ */
+ public void seek (long pos) throws IOException {
+ final long block = pos / blockSize + 1;
+ final int blockLength = (int) (pos % blockSize);
+ channel.position(pos);
+ this.currentFilePart = new FilePart(block, blockLength, null);
+
+ }
+}
Review Comment:
Files should end in a blank line.
##########
src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestRandomAccess.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.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.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.commons.io.TestResources;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+public class ReversedLinesFileReaderTestRandomAccess {
+
+ private ReversedLinesFileReader reversedLinesFileReader;
+ private static final String testLine1 =
+ "A Test Line. Special chars: "
+ .concat("\u00C4\u00E4\u00DC\u00FC\u00D6\u00F6\u00DF ")
+ .concat("\u00C3\u00E1\u00E9\u00ED\u00EF\u00E7\u00F1\u00C2 ")
+ .concat("\u00A9\u00B5\u00A5\u00A3");
+ private static final String testLine2 = testLine1 +"\u00B1";
+ private static final String testLine3 = testLine2 +"\u00B2";
+ private static final String testLine4 = testLine3 +"\u00AE";
+
+ @AfterEach
+ public void closeReader() {
+ try {
+ reversedLinesFileReader.close();
Review Comment:
Use `IOUtils.closeQuietly()` instead.
##########
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java:
##########
@@ -467,4 +467,25 @@ public String toString(final int lineCount) throws
IOException {
return lines.isEmpty() ? EMPTY_STRING :
String.join(System.lineSeparator(), lines) + System.lineSeparator();
}
-}
+ /**
+ * Returns the current offset in this file
+ * @return the current offset
+ * @throws IOException if an I/O error occurs.
+ */
Review Comment:
New public and protected APIs need an `@since` tag.
##########
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java:
##########
@@ -467,4 +467,25 @@ public String toString(final int lineCount) throws
IOException {
return lines.isEmpty() ? EMPTY_STRING :
String.join(System.lineSeparator(), lines) + System.lineSeparator();
}
-}
+ /**
+ * Returns the current offset in this file
+ * @return the current offset
+ * @throws IOException if an I/O error occurs.
+ */
+ public long getFilePointer() throws IOException {
+ return channel.position();
+ }
+
+ /**
+ * Sets the file-pointer offset, measured from the beginning of this file,
at which the next read occurs.
+ * @param pos the offset value to be set
+ * @throws IOException if an I/O error occurs.
Review Comment:
New public and protected APIs need an @since tag.
##########
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java:
##########
@@ -467,4 +467,25 @@ public String toString(final int lineCount) throws
IOException {
return lines.isEmpty() ? EMPTY_STRING :
String.join(System.lineSeparator(), lines) + System.lineSeparator();
}
-}
+ /**
+ * Returns the current offset in this file
+ * @return the current offset
+ * @throws IOException if an I/O error occurs.
+ */
+ public long getFilePointer() throws IOException {
+ return channel.position();
+ }
+
+ /**
+ * Sets the file-pointer offset, measured from the beginning of this file,
at which the next read occurs.
+ * @param pos the offset value to be set
+ * @throws IOException if an I/O error occurs.
+ */
+ public void seek (long pos) throws IOException {
Review Comment:
No space after the method name. Use final where you can.
Issue Time Tracking
-------------------
Worklog Id: (was: 800444)
Time Spent: 1h 50m (was: 1h 40m)
> ReversedLinesFileReader with 'getFilePointer' and 'seek' API
> ------------------------------------------------------------
>
> Key: IO-543
> URL: https://issues.apache.org/jira/browse/IO-543
> Project: Commons IO
> Issue Type: Improvement
> Affects Versions: 2.6
> Reporter: Jean-Pierre Portier
> Priority: Minor
> Time Spent: 1h 50m
> Remaining Estimate: 0h
>
> Adding API to get/set current file pointer position for
> org.apache.commons.io.input.ReversedLinesFileReader class:
> - long getFilePointer()
> - void seek(long pos)
> (they act like java.io.RandomAccessFile API class)
--
This message was sent by Atlassian Jira
(v8.20.10#820010)