From a3ce9a307cd38355e5827aa50b9125c032be6831 Mon Sep 17 00:00:00 2001 From: LiangliangSui Date: Mon, 1 Jul 2024 11:51:42 +0800 Subject: [PATCH] update Signed-off-by: LiangliangSui --- .../src/main/java/org/apache/fury/Fury.java | 22 +++++++++++-------- .../apache/fury/resolver/ClassResolver.java | 4 ++++ .../test/java/org/apache/fury/FuryTest.java | 13 +++++++++++ 3 files changed, 30 insertions(+), 9 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 ccef2aaa3d..94e75e4a39 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 @@ -45,6 +45,7 @@ import org.apache.fury.resolver.ClassInfoHolder; import org.apache.fury.resolver.ClassResolver; import org.apache.fury.resolver.MapRefResolver; +import org.apache.fury.resolver.MetaContext; import org.apache.fury.resolver.MetaStringResolver; import org.apache.fury.resolver.NoRefResolver; import org.apache.fury.resolver.RefResolver; @@ -310,12 +311,14 @@ private void write(MemoryBuffer buffer, Object obj) { buffer.writeInt32(-1); // preserve 4-byte for nativeObjects start offsets. } // reduce caller stack - if (!refResolver.writeRefOrNull(buffer, obj)) { + boolean isNull = refResolver.writeRefOrNull(buffer, obj); + if (!isNull) { ClassInfo classInfo = classResolver.getOrUpdateClassInfo(obj.getClass()); classResolver.writeClass(buffer, classInfo); writeData(buffer, classInfo, obj); } - if (shareMeta) { + MetaContext metaContext = getSerializationContext().getMetaContext(); + if (shareMeta && !isNull && !metaContext.writingClassDefs.isEmpty()) { buffer.putInt32(startOffset, buffer.writerIndex()); classResolver.writeClassDefs(buffer); } @@ -1031,14 +1034,15 @@ public void serializeJavaObject(MemoryBuffer buffer, Object obj) { if (!refResolver.writeRefOrNull(buffer, obj)) { ClassInfo classInfo = classResolver.getOrUpdateClassInfo(obj.getClass()); writeData(buffer, classInfo, obj); + MetaContext metaContext = getSerializationContext().getMetaContext(); + if (!metaContext.writingClassDefs.isEmpty()) { + buffer.putInt32(startOffset, buffer.writerIndex()); + classResolver.writeClassDefs(buffer); + } } - buffer.putInt32(startOffset, buffer.writerIndex()); - classResolver.writeClassDefs(buffer); - } else { - if (!refResolver.writeRefOrNull(buffer, obj)) { - ClassInfo classInfo = classResolver.getOrUpdateClassInfo(obj.getClass()); - writeData(buffer, classInfo, obj); - } + } else if (!refResolver.writeRefOrNull(buffer, obj)) { + ClassInfo classInfo = classResolver.getOrUpdateClassInfo(obj.getClass()); + writeData(buffer, classInfo, obj); } } catch (StackOverflowError t) { throw processStackOverflowError(t); diff --git a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java index 58ea84aadb..ec16655918 100644 --- a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java +++ b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java @@ -1423,6 +1423,10 @@ public void writeClassDefs(MemoryBuffer buffer) { public void readClassDefs(MemoryBuffer buffer) { MetaContext metaContext = fury.getSerializationContext().getMetaContext(); int classDefOffset = buffer.readInt32(); + if (classDefOffset == -1) { + // -1 means no ClassDefs are available to read. + return; + } int readerIndex = buffer.readerIndex(); buffer.readerIndex(classDefOffset); int numClassDefs = buffer.readVarUint32Small14(); diff --git a/java/fury-core/src/test/java/org/apache/fury/FuryTest.java b/java/fury-core/src/test/java/org/apache/fury/FuryTest.java index 072ff9f6cc..0178128fd5 100644 --- a/java/fury-core/src/test/java/org/apache/fury/FuryTest.java +++ b/java/fury-core/src/test/java/org/apache/fury/FuryTest.java @@ -560,4 +560,17 @@ public void testPrintReadObjectsWhenFailed() { Assert.assertTrue(e.getMessage().contains("[a, b]")); } } + + @Test + public void testNullObjSerAndDe() { + Fury fury = + Fury.builder() + .withRefTracking(true) + .requireClassRegistration(false) + .withMetaShare(true) + .build(); + byte[] bytes = fury.serializeJavaObjectAndClass(null); + Object obj = fury.deserializeJavaObjectAndClass(bytes); + assertNull(obj); + } }