Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for IO-639 #590

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
import org.apache.commons.io.build.AbstractStreamBuilder;

/**
* Reads lines in a file reversely (similar to a BufferedReader, but starting at the last line). Useful for e.g. searching in log files.
* Reads lines in a file in reverse (similar to a BufferedReader, but starting at the last line).
* Line endings (\n, \r) are not included in the lines returned.
* Useful for e.g. searching in log files.
* <p>
* To build an instance, use {@link Builder}.
* </p>
Expand Down Expand Up @@ -108,7 +110,7 @@ public ReversedLinesFileReader get() throws IOException {
}

private final class FilePart {
private final long no;
private final long partNumber;

private final byte[] data;

Expand All @@ -119,20 +121,20 @@ private final class FilePart {
/**
* Constructs a new instance.
*
* @param no the part number
* @param partNumber the part number
* @param length its length
* @param leftOverOfLastFilePart remainder
* @throws IOException if there is a problem reading the file
*/
private FilePart(final long no, final int length, final byte[] leftOverOfLastFilePart) throws IOException {
this.no = no;
private FilePart(final long partNumber, final int length, final byte[] leftOverOfLastFilePart) throws IOException {
this.partNumber = partNumber;
final int dataLength = length + (leftOverOfLastFilePart != null ? leftOverOfLastFilePart.length : 0);
this.data = new byte[dataLength];
final long off = (no - 1) * blockSize;
final long offset = (partNumber - 1) * blockSize;

// read data
if (no > 0 /* file not empty */) {
channel.position(off);
if (partNumber > 0 /* file not empty */) {
channel.position(offset);
final int countRead = channel.read(ByteBuffer.wrap(data, 0, length));
if (countRead != length) {
throw new IllegalStateException("Count of requested bytes and actually read bytes don't match");
Expand Down Expand Up @@ -191,7 +193,7 @@ private String readLine() { //NOPMD Bug in PMD
String line = null;
int newLineMatchByteCount;

final boolean isLastFilePart = no == 1;
final boolean isLastFilePart = partNumber == 1;

int i = currentLastBytePos;
while (i > -1) {
Expand Down Expand Up @@ -252,8 +254,8 @@ private FilePart rollOver() throws IOException {
+ "last readLine() should have returned something! currentLastCharPos=" + currentLastBytePos);
}

if (no > 1) {
return new FilePart(no - 1, blockSize, leftOver);
if (partNumber > 1) {
return new FilePart(partNumber - 1, blockSize, leftOver);
}
// NO 1 was the last FilePart, we're finished
if (leftOver != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@

public class ReversedLinesFileReaderTestSimple {

private final int BLOCK_SIZE = 10;

@Test
public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException, IOException {
final int blockSize = 10;
final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize,
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, BLOCK_SIZE,
StandardCharsets.ISO_8859_1.name())) {
assertEqualsAndNoLineBreaks("987654321", reversedLinesFileReader.readLine());
assertEqualsAndNoLineBreaks("123456789", reversedLinesFileReader.readLine());
Expand All @@ -47,9 +48,8 @@ public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException,

@Test
public void testLineCount() throws URISyntaxException, IOException {
final int blockSize = 10;
final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize,
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, BLOCK_SIZE,
StandardCharsets.ISO_8859_1.name())) {
assertThrows(IllegalArgumentException.class, () -> reversedLinesFileReader.readLines(-1));
assertTrue(reversedLinesFileReader.readLines(0).isEmpty());
Expand All @@ -63,9 +63,8 @@ public void testLineCount() throws URISyntaxException, IOException {

@Test
public void testToString() throws URISyntaxException, IOException {
final int blockSize = 10;
final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize,
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, BLOCK_SIZE,
StandardCharsets.ISO_8859_1.name())) {
assertThrows(IllegalArgumentException.class, () -> reversedLinesFileReader.toString(-1));
assertTrue(reversedLinesFileReader.readLines(0).isEmpty());
Expand All @@ -90,4 +89,17 @@ public void testUnsupportedEncodingUTF16() throws URISyntaxException {
() -> new ReversedLinesFileReader(testFileEmpty, IOUtils.DEFAULT_BUFFER_SIZE, StandardCharsets.UTF_16.name()).close());
}

@Test
// IO-639
public void testEmptyFirstLine() throws IOException, URISyntaxException {
final File testFileEmptyFirstLine = TestResources.getFile("/empty_first_line.bin");
try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFileEmptyFirstLine, BLOCK_SIZE,
StandardCharsets.US_ASCII.name())) {
assertEqualsAndNoLineBreaks("test2", reversedLinesFileReader.readLine());
assertEqualsAndNoLineBreaks("", reversedLinesFileReader.readLine());
assertEqualsAndNoLineBreaks("test1", reversedLinesFileReader.readLine());
assertEqualsAndNoLineBreaks("", reversedLinesFileReader.readLine());
}
}

}
4 changes: 4 additions & 0 deletions src/test/resources/org/apache/commons/io/empty_first_line.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

test1

test2
Loading