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

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fury.git


The following commit(s) were added to refs/heads/main by this push:
     new 6e4d8a0a fix(java): fix big buffer streaming MetaShared read offset 
(#1760)
6e4d8a0a is described below

commit 6e4d8a0a746a8d471895afbdbda51caa4ea0e633
Author: Shawn Yang <[email protected]>
AuthorDate: Thu Jul 25 14:46:45 2024 +0800

    fix(java): fix big buffer streaming MetaShared read offset (#1760)
    
    ## What does this PR do?
    
     fix big buffer streaming MetaShared read by using relative offset
    
    ## Related issues
    
    Closes #1759
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/fury/issues/new/choose) describing the
    need to do so and update the document if necessary.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    -->
---
 .../src/main/java/org/apache/fury/Fury.java        |  8 +++---
 .../src/test/java/org/apache/fury/StreamTest.java  | 29 +++++++++++++++++++++-
 2 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/java/fury-core/src/main/java/org/apache/fury/Fury.java 
b/java/fury-core/src/main/java/org/apache/fury/Fury.java
index bb2aa87e..52c8e669 100644
--- a/java/fury-core/src/main/java/org/apache/fury/Fury.java
+++ b/java/fury-core/src/main/java/org/apache/fury/Fury.java
@@ -345,7 +345,7 @@ public final class Fury implements BaseFury {
       writeData(buffer, classInfo, obj);
     }
     if (shareMeta) {
-      buffer.putInt32(startOffset, buffer.writerIndex());
+      buffer.putInt32(startOffset, buffer.writerIndex() - startOffset - 4);
       classResolver.writeClassDefs(buffer);
     }
   }
@@ -1064,7 +1064,7 @@ public final class Fury implements BaseFury {
           ClassInfo classInfo = 
classResolver.getOrUpdateClassInfo(obj.getClass());
           writeData(buffer, classInfo, obj);
         }
-        buffer.putInt32(startOffset, buffer.writerIndex());
+        buffer.putInt32(startOffset, buffer.writerIndex() - startOffset - 4);
         classResolver.writeClassDefs(buffer);
       } else {
         if (!refResolver.writeRefOrNull(buffer, obj)) {
@@ -1421,9 +1421,9 @@ public final class Fury implements BaseFury {
   }
 
   private void readClassDefs(MemoryBuffer buffer) {
-    int classDefOffset = buffer.readInt32();
+    int relativeClassDefOffset = buffer.readInt32();
     int readerIndex = buffer.readerIndex();
-    buffer.readerIndex(classDefOffset);
+    buffer.readerIndex(readerIndex + relativeClassDefOffset);
     classResolver.readClassDefs(buffer);
     classDefEndOffset = buffer.readerIndex();
     buffer.readerIndex(readerIndex);
diff --git a/java/fury-core/src/test/java/org/apache/fury/StreamTest.java 
b/java/fury-core/src/test/java/org/apache/fury/StreamTest.java
index ea81e605..56e7da3c 100644
--- a/java/fury-core/src/test/java/org/apache/fury/StreamTest.java
+++ b/java/fury-core/src/test/java/org/apache/fury/StreamTest.java
@@ -33,6 +33,7 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import org.apache.fury.config.CompatibleMode;
 import org.apache.fury.io.FuryInputStream;
 import org.apache.fury.io.FuryReadableChannel;
@@ -43,7 +44,7 @@ import org.apache.fury.test.bean.BeanA;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
-public class StreamTest {
+public class StreamTest extends FuryTestBase {
 
   @Test
   public void testBufferStream() {
@@ -351,4 +352,30 @@ public class StreamTest {
     Assert.assertEquals(fury.deserialize(stream), map);
     Assert.assertEquals(fury.deserialize(stream), list2);
   }
+
+  @Test
+  public void testBigBufferStreamingMetaShared() throws IOException {
+    Fury fury = 
builder().withCompatibleMode(CompatibleMode.COMPATIBLE).build();
+    ByteArrayOutputStream bas = new ByteArrayOutputStream();
+    List<Integer> list = new ArrayList<>();
+    HashMap<String, String> map = new HashMap<>();
+    for (int i = 0; i < 5000; i++) {
+      list.add(i);
+      map.put("key" + i, "value" + i);
+    }
+    fury.serialize(bas, list);
+    fury.serialize(bas, map);
+    fury.serialize(bas, list);
+    fury.serialize(bas, new long[5000]);
+    fury.serialize(bas, new int[5000]);
+    bas.flush();
+
+    InputStream bis = new ByteArrayInputStream(bas.toByteArray());
+    FuryInputStream stream = of(bis);
+    assertEquals(fury.deserialize(stream), list);
+    assertEquals(fury.deserialize(stream), map);
+    assertEquals(fury.deserialize(stream), list);
+    assertEquals(fury.deserialize(stream), new long[5000]);
+    assertEquals(fury.deserialize(stream), new int[5000]);
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to