diff --git a/kala-base/src/main/java/kala/text/StringAppender.java b/kala-base/src/main/java/kala/text/StringAppender.java index b08bf563..7095a61c 100644 --- a/kala-base/src/main/java/kala/text/StringAppender.java +++ b/kala-base/src/main/java/kala/text/StringAppender.java @@ -17,16 +17,13 @@ import org.jetbrains.annotations.NotNull; -import java.io.IOException; -import java.io.Serial; -import java.io.Serializable; -import java.io.Writer; +import java.io.*; import java.util.Formatter; import java.util.Locale; import java.util.Objects; - -public class StringAppender extends Writer implements Serializable { +@SuppressWarnings("resource") +public class StringAppender extends Writer implements Externalizable { @Serial private static final long serialVersionUID = 0L; @@ -448,6 +445,17 @@ private void beforeAppend(int n) { builder.ensureCapacity(builder.length() + n); } + @Override + public void writeExternal(ObjectOutput out) throws IOException { + out.writeObject(builder.toString()); + } + + @Override + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + this.builder.setLength(0); + this.builder.append((String) in.readObject()); + } + @Override public String toString() { return builder.toString(); diff --git a/src/test/java/kala/text/StringAppenderTest.java b/src/test/java/kala/text/StringAppenderTest.java new file mode 100644 index 00000000..33d12005 --- /dev/null +++ b/src/test/java/kala/text/StringAppenderTest.java @@ -0,0 +1,30 @@ +/* + * Copyright 2024 Glavo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package kala.text; + +import kala.SerializationUtils; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class StringAppenderTest { + + @Test + public void serializeTest() throws Exception { + assertEquals("", SerializationUtils.writeAndRead(new StringAppender()).toString()); + assertEquals("str", SerializationUtils.writeAndRead(new StringAppender("str")).toString()); + } +}