You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cl...@apache.org on 2023/08/16 09:35:09 UTC

[avro] branch master updated: AVRO-3748 [java] fix SeekableInputStream.skip (#2203)

This is an automated email from the ASF dual-hosted git repository.

clesaec pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 33631e1e0 AVRO-3748 [java] fix SeekableInputStream.skip (#2203)
33631e1e0 is described below

commit 33631e1e0fa48af0cd4ff46a32cbdb809e326177
Author: Steven Aerts <st...@gmail.com>
AuthorDate: Wed Aug 16 11:35:02 2023 +0200

    AVRO-3748 [java] fix SeekableInputStream.skip (#2203)
    
    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.
---
 .../java/org/apache/avro/file/DataFileReader.java  |  7 ++--
 .../apache/avro/file/TestSeekableInputStream.java  | 45 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader.java b/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader.java
index 10067cd6c..7bf2c05b1 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader.java
@@ -312,12 +312,11 @@ public class DataFileReader<D> extends DataFileStream<D> implements FileReader<D
       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
diff --git a/lang/java/avro/src/test/java/org/apache/avro/file/TestSeekableInputStream.java b/lang/java/avro/src/test/java/org/apache/avro/file/TestSeekableInputStream.java
new file mode 100644
index 000000000..97715c044
--- /dev/null
+++ b/lang/java/avro/src/test/java/org/apache/avro/file/TestSeekableInputStream.java
@@ -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));
+            }
+        }
+    }
+}