diff --git a/paimon-common/src/main/java/org/apache/paimon/io/DataOutputSerializer.java b/paimon-common/src/main/java/org/apache/paimon/io/DataOutputSerializer.java index 33f2a8f0bf06..ee87cdbedbfc 100644 --- a/paimon-common/src/main/java/org/apache/paimon/io/DataOutputSerializer.java +++ b/paimon-common/src/main/java/org/apache/paimon/io/DataOutputSerializer.java @@ -153,7 +153,6 @@ public void writeBytes(String s) throws IOException { for (int i = 0; i < sLen; i++) { writeByte(s.charAt(i)); } - this.position += sLen; } @Override diff --git a/paimon-common/src/test/java/org/apache/paimon/io/DataOutputSerializerTest.java b/paimon-common/src/test/java/org/apache/paimon/io/DataOutputSerializerTest.java new file mode 100644 index 000000000000..ac6d99573c16 --- /dev/null +++ b/paimon-common/src/test/java/org/apache/paimon/io/DataOutputSerializerTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.paimon.io; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Test for {@link DataOutputSerializer}. */ +public class DataOutputSerializerTest { + + @Test + public void testWriteBytesAdvancesPositionOnce() throws IOException { + DataOutputSerializer out = new DataOutputSerializer(16); + out.writeBytes("abc"); + + assertThat(out.length()).isEqualTo(3); + assertThat(out.getCopyOfBuffer()).containsExactly('a', 'b', 'c'); + } + + @Test + public void testWriteBytesLeavesTheNextWriteInPlace() throws IOException { + DataOutputSerializer out = new DataOutputSerializer(16); + out.writeBytes("abc"); + out.writeInt(42); + + // "abc" and then 42 as a big-endian int, with no gap of untouched bytes in between. + assertThat(out.getCopyOfBuffer()).containsExactly('a', 'b', 'c', 0, 0, 0, 42); + } + + @Test + public void testWriteBytesAcrossAResize() throws IOException { + DataOutputSerializer out = new DataOutputSerializer(2); + out.writeBytes("abcdef"); + + assertThat(out.getCopyOfBuffer()).containsExactly('a', 'b', 'c', 'd', 'e', 'f'); + } +}