Skip to content

Commit

Permalink
AVRO-3748 [java] fix SeekableInputStream.skip (apache#2203)
Browse files Browse the repository at this point in the history
The implementation of SeekableInputStream.skip had a longstantind issue
that it did a seek to a wrong position.
This issue is rather difficult to hit in avro as it is only used in
corner cases of for example the FastReader.
  • Loading branch information
steven-aerts committed Aug 16, 2023
1 parent 603a7e7 commit 33631e1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,11 @@ public long skip(long skip) throws IOException {
long length = in.length();
long remaining = length - position;
if (remaining > skip) {
in.seek(skip);
return in.tell() - position;
in.seek(position + skip);
} else {
in.seek(remaining);
return in.tell() - position;
in.seek(length);
}
return in.tell() - position;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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
*
* https://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.avro.file;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;

public class TestSeekableInputStream {
@Test
public void testSkip(@TempDir Path tempDir) throws IOException {
File f = tempDir.resolve("testSkip.avro").toFile();
try (FileWriter w = new FileWriter(f)) {
w.write("someContent");
}
try(SeekableFileInput in = new SeekableFileInput(f);
DataFileReader.SeekableInputStream stream = new DataFileReader.SeekableInputStream(in)
) {
for (long i = 0; i < stream.length(); i++) {
Assertions.assertEquals(stream.length() - i, stream.available());
Assertions.assertEquals(1, stream.skip(1));
}
}
}
}

0 comments on commit 33631e1

Please sign in to comment.