You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fury.apache.org by ch...@apache.org on 2024/02/29 11:06:22 UTC

(incubator-fury) branch main updated: fix(java): Allow partial read of serialized size from `InputStream` (#1391)

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/incubator-fury.git


The following commit(s) were added to refs/heads/main by this push:
     new d9198e7e fix(java): Allow partial read of serialized size from `InputStream` (#1391)
d9198e7e is described below

commit d9198e7e2dfdbde25dbe240392e6744164111123
Author: phogh <15...@users.noreply.github.com>
AuthorDate: Thu Feb 29 11:06:14 2024 +0000

    fix(java): Allow partial read of serialized size from `InputStream` (#1391)
    
    Deserialize from `InputStream` throws when the size cannot be read in
    one read. This fixes the issue by reading in a loop like it does for the
    serialized data.
    
    Closes #1383
---
 java/fury-core/src/main/java/org/apache/fury/Fury.java | 16 +++++++++++-----
 1 file changed, 11 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 436b1b38..7b1331dd 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
@@ -1177,19 +1177,25 @@ public final class Fury implements BaseFury {
   private static void readToBufferFromStream(InputStream inputStream, MemoryBuffer buffer)
       throws IOException {
     buffer.readerIndex(0);
-    int read = inputStream.read(buffer.getHeapMemory(), 0, 4);
+    int read = readBytes(inputStream, buffer.getHeapMemory(), 0, 4);
     Preconditions.checkArgument(read == 4);
     int size = buffer.readInt();
     buffer.ensure(4 + size);
-    read = 0;
+    read = readBytes(inputStream, buffer.getHeapMemory(), 4, size);
+    Preconditions.checkArgument(read == size);
+  }
+
+  private static int readBytes(InputStream inputStream, byte[] buffer, int offset, int size)
+      throws IOException {
+    int read = 0;
+    int count = 0;
     while (read < size) {
-      int count;
-      if ((count = inputStream.read(buffer.getHeapMemory(), read + 4, size - read)) == -1) {
+      if ((count = inputStream.read(buffer, offset + read, size - read)) == -1) {
         break;
       }
       read += count;
     }
-    Preconditions.checkArgument(read == size);
+    return (read == 0 && count == -1) ? -1 : read;
   }
 
   public void reset() {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org