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 29, 2024
1 parent ba4ac8e commit 4ed36ac
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
17 changes: 13 additions & 4 deletions java/fury-core/src/main/java/org/apache/fury/Fury.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 @@ -339,12 +340,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 != null && !metaContext.writingClassDefs.isEmpty()) {
buffer.putInt32(startOffset, buffer.writerIndex() - startOffset - 4);
classResolver.writeClassDefs(buffer);
}
Expand Down Expand Up @@ -1063,9 +1066,12 @@ 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 != null && !metaContext.writingClassDefs.isEmpty()) {
buffer.putInt32(startOffset, buffer.writerIndex() - startOffset - 4);
classResolver.writeClassDefs(buffer);
}
}
buffer.putInt32(startOffset, buffer.writerIndex() - startOffset - 4);
classResolver.writeClassDefs(buffer);
} else {
if (!refResolver.writeRefOrNull(buffer, obj)) {
ClassInfo classInfo = classResolver.getOrUpdateClassInfo(obj.getClass());
Expand Down Expand Up @@ -1437,6 +1443,9 @@ private void serializeToStream(OutputStream outputStream, Consumer<MemoryBuffer>

private void readClassDefs(MemoryBuffer buffer) {
int relativeClassDefOffset = buffer.readInt32();
if (relativeClassDefOffset == -1) {
return;
}
int readerIndex = buffer.readerIndex();
buffer.readerIndex(readerIndex + relativeClassDefOffset);
classResolver.readClassDefs(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

/**
* Set all object array elements to null. This method is faster than {@link Arrays#fill} for large
* arrays (> 128).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,7 @@ private void writeClassDefs(
*/
public void readClassDefs(MemoryBuffer buffer) {
MetaContext metaContext = fury.getSerializationContext().getMetaContext();
assert metaContext != null : SET_META__CONTEXT_MSG;
int numClassDefs = buffer.readVarUint32Small14();
for (int i = 0; i < numClassDefs; i++) {
long id = buffer.readInt64();
Expand Down
17 changes: 17 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 @@ -62,6 +62,7 @@
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.memory.MemoryUtils;
import org.apache.fury.memory.Platform;
import org.apache.fury.resolver.MetaContext;
import org.apache.fury.serializer.ArraySerializersTest;
import org.apache.fury.serializer.EnumSerializerTest;
import org.apache.fury.serializer.ObjectSerializer;
Expand Down Expand Up @@ -598,4 +599,20 @@ 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();
MetaContext metaContext = new MetaContext();
fury.getSerializationContext().setMetaContext(metaContext);
byte[] bytes = fury.serializeJavaObjectAndClass(null);
fury.getSerializationContext().setMetaContext(metaContext);
Object obj = fury.deserializeJavaObjectAndClass(bytes);
assertNull(obj);
}
}

0 comments on commit 4ed36ac

Please sign in to comment.