Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: LiangliangSui <[email protected]>
  • Loading branch information
LiangliangSui committed Jul 1, 2024
1 parent 0251df0 commit a3ce9a3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
22 changes: 13 additions & 9 deletions java/fury-core/src/main/java/org/apache/fury/Fury.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions java/fury-core/src/test/java/org/apache/fury/FuryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit a3ce9a3

Please sign in to comment.