Skip to content
Prev Previous commit
Next Next commit
ReversedLinesFileReader truncate consumed lines
  • Loading branch information
apuig committed Mar 31, 2025
commit 0d2527e51861969230463b7fe15becdfe9dc3710
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
Expand Down Expand Up @@ -278,7 +278,8 @@ public static Builder builder() {

private final int blockSize;
private final Charset charset;
private final SeekableByteChannel channel;
private final FileChannel channel;
private final FileLock fileLock;
private final long totalByteLength;
private final long totalBlockCount;
private final byte[][] newLineSequences;
Expand Down Expand Up @@ -422,7 +423,8 @@ public ReversedLinesFileReader(final Path file, final int blockSize, final Chars
this.avoidNewlineSplitBufferSize = newLineSequences[0].length;

// Open file
this.channel = Files.newByteChannel(file, StandardOpenOption.READ);
this.channel = FileChannel.open(file, StandardOpenOption.READ, StandardOpenOption.WRITE);
this.fileLock = channel.lock();
this.totalByteLength = channel.size();
int lastBlockLength = (int) (this.totalByteLength % blockSize);
if (lastBlockLength > 0) {
Expand Down Expand Up @@ -461,6 +463,7 @@ public ReversedLinesFileReader(final Path file, final int blockSize, final Strin
*/
@Override
public void close() throws IOException {
fileLock.release();
channel.close();
}

Expand Down Expand Up @@ -514,10 +517,18 @@ public List<String> readLines(final int lineCount) throws IOException {
for (int i = 0; i < lineCount; i++) {
final String line = readLine();
if (line == null) {
channel.truncate(0);
return arrayList;
}
arrayList.add(line);
}

long truncateTo = (this.currentFilePart.no - 1) * blockSize;
truncateTo += this.currentFilePart.currentLastBytePos + 1;
channel.truncate(truncateTo);

channel.force(true);

return arrayList;
}

Expand Down