Skip to content

Commit 13930df

Browse files
authored
[common] Fix Serialize BinaryString throws NotSerializableException (#1496)
1 parent f116202 commit 13930df

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

fluss-common/src/main/java/com/alibaba/fluss/row/BinarySection.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,21 @@ public ByteBuffer wrapByteBuffer() {
8787
}
8888
}
8989

90+
/**
91+
* Support Java Serialization by customize writeObject and readObject methods, because {@link
92+
* MemorySegment} doesn't support Java Serialization.
93+
*/
9094
private void writeObject(ObjectOutputStream out) throws IOException {
91-
out.defaultWriteObject();
9295
byte[] bytes = toBytes();
9396
out.writeInt(bytes.length);
9497
out.write(bytes);
9598
}
9699

100+
/**
101+
* Support Java Serialization by customize writeObject and readObject methods, because {@link
102+
* MemorySegment} doesn't support Java Serialization.
103+
*/
97104
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
98-
in.defaultReadObject();
99105
byte[] bytes = new byte[in.readInt()];
100106
IOUtils.readFully(in, bytes);
101107
pointTo(MemorySegment.wrap(bytes), 0, bytes.length);

fluss-common/src/test/java/com/alibaba/fluss/row/BinaryStringTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
import org.junit.jupiter.api.TestTemplate;
2525
import org.junit.jupiter.api.extension.ExtendWith;
2626

27+
import java.io.ByteArrayInputStream;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.ObjectInputStream;
30+
import java.io.ObjectOutputStream;
2731
import java.math.BigDecimal;
2832
import java.nio.ByteBuffer;
2933
import java.nio.charset.StandardCharsets;
@@ -146,6 +150,30 @@ public void emptyStringTest() {
146150
assertThat(empty.getSizeInBytes()).isEqualTo(0);
147151
}
148152

153+
@TestTemplate
154+
public void testJavaSerialization() throws Exception {
155+
String str = "hello world";
156+
BinaryString bs = fromString(str);
157+
byte[] data;
158+
159+
// serialization: object -> bytes
160+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
161+
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
162+
163+
oos.writeObject(bs);
164+
data = bos.toByteArray();
165+
}
166+
167+
// deserialization:bytes -> object
168+
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
169+
ObjectInputStream ois = new ObjectInputStream(bis)) {
170+
BinaryString deserializedString = (BinaryString) ois.readObject();
171+
172+
assertThat(deserializedString).isEqualTo(bs);
173+
assertThat(deserializedString.toString()).isEqualTo(str);
174+
}
175+
}
176+
149177
@TestTemplate
150178
public void compareTo() {
151179
assertThat(fromString(" ").compareTo(blankString(3))).isEqualTo(0);

0 commit comments

Comments
 (0)