From de83e57f3fd83c2bdb5c453d34bea0bfa39c3ddb Mon Sep 17 00:00:00 2001 From: David Turner Date: Fri, 30 Jan 2026 15:46:26 +0000 Subject: [PATCH] Replace `CountingStreamOutput#size()` with `#position()` No need to have two methods to do the same thing, and the general-purpose one is `#position()`, so let's use that here too. --- .../common/geo/SimpleFeatureFactory.java | 2 +- .../io/stream/CountingStreamOutput.java | 49 +++++++++---------- .../common/io/stream/DelayableWriteable.java | 2 +- .../lucene/spatial/TriangleTreeWriter.java | 6 +-- .../common/io/stream/BytesStreamsTests.java | 34 ++++++------- .../io/stream/StreamOutputToBytesTests.java | 10 ++-- 6 files changed, 49 insertions(+), 54 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/geo/SimpleFeatureFactory.java b/server/src/main/java/org/elasticsearch/common/geo/SimpleFeatureFactory.java index fdf3459a3e0c7..c26f3ea8bc87f 100644 --- a/server/src/main/java/org/elasticsearch/common/geo/SimpleFeatureFactory.java +++ b/server/src/main/java/org/elasticsearch/common/geo/SimpleFeatureFactory.java @@ -187,7 +187,7 @@ private static byte[] writeCommands(final int[] commands, final int type, final output.writeVInt(24); output.writeVInt(type); output.writeVInt(34); - output.writeVInt(Math.toIntExact(counting.size())); + output.writeVInt(Math.toIntExact(counting.position())); for (int i = 0; i < length; i++) { output.writeVInt(commands[i]); } diff --git a/server/src/main/java/org/elasticsearch/common/io/stream/CountingStreamOutput.java b/server/src/main/java/org/elasticsearch/common/io/stream/CountingStreamOutput.java index 55349c63d2540..21188ed031e89 100644 --- a/server/src/main/java/org/elasticsearch/common/io/stream/CountingStreamOutput.java +++ b/server/src/main/java/org/elasticsearch/common/io/stream/CountingStreamOutput.java @@ -18,102 +18,97 @@ * A reusable @link {@link StreamOutput} that just count how many bytes are written. */ public class CountingStreamOutput extends StreamOutput { - private long size; + private long position; /** reset the written byes to 0 */ public void reset() { - size = 0L; - } - - /** returns how many bytes would have been written -- TODO replace with position() from StreamOutput interface */ - public long size() { - return size; + position = 0L; } @Override public void writeByte(byte b) { - ++size; + ++position; } @Override public void writeBytes(byte[] b, int offset, int length) { - size += length; + position += length; } @Override public long position() { - return size; + return position; } @Override public void writeShort(short v) throws IOException { - size += Short.BYTES; + position += Short.BYTES; } @Override public void writeInt(int i) { - size += Integer.BYTES; + position += Integer.BYTES; } @Override public void writeIntLE(int i) throws IOException { - size += Integer.BYTES; + position += Integer.BYTES; } @Override public void writeIntArray(int[] values) { writeVInt(values.length); - size += (long) values.length * Integer.BYTES; + position += (long) values.length * Integer.BYTES; } @Override public void writeLong(long i) { - size += Long.BYTES; + position += Long.BYTES; } @Override public void writeLongLE(long i) { - size += Long.BYTES; + position += Long.BYTES; } @Override public void writeLongArray(long[] values) { writeVInt(values.length); - size += (long) values.length * Long.BYTES; + position += (long) values.length * Long.BYTES; } @Override public void writeFloat(float v) { - size += Float.BYTES; + position += Float.BYTES; } @Override public void writeFloatArray(float[] values) { writeVInt(values.length); - size += (long) values.length * Float.BYTES; + position += (long) values.length * Float.BYTES; } @Override public void writeDouble(double v) { - size += Double.BYTES; + position += Double.BYTES; } @Override public void writeDoubleArray(double[] values) { writeVInt(values.length); - size += (long) values.length * Double.BYTES; + position += (long) values.length * Double.BYTES; } @Override public void writeVInt(int v) { // set LSB because 0 takes 1 byte - size += (38 - Integer.numberOfLeadingZeros(v | 1)) / 7; + position += (38 - Integer.numberOfLeadingZeros(v | 1)) / 7; } @Override void writeVLongNoCheck(long v) { // set LSB because 0 takes 1 byte - size += (70 - Long.numberOfLeadingZeros(v | 1L)) / 7; + position += (70 - Long.numberOfLeadingZeros(v | 1L)) / 7; } @Override @@ -125,18 +120,18 @@ public void writeZLong(long i) { public void writeString(String str) { final int charCount = str.length(); writeVInt(charCount); - size += charCount; + position += charCount; for (int i = 0; i < charCount; i++) { final int c = str.charAt(i); if (c > 0x007F) { - size += c > 0x07FF ? 2 : 1; + position += c > 0x07FF ? 2 : 1; } } } @Override public void writeOptionalString(@Nullable String str) { - size += 1; + position += 1; if (str != null) { writeString(str); } @@ -144,7 +139,7 @@ public void writeOptionalString(@Nullable String str) { @Override public void writeGenericString(String value) { - size += 1; + position += 1; writeString(value); } diff --git a/server/src/main/java/org/elasticsearch/common/io/stream/DelayableWriteable.java b/server/src/main/java/org/elasticsearch/common/io/stream/DelayableWriteable.java index aa570f4140f63..7784c6a0ad2a2 100644 --- a/server/src/main/java/org/elasticsearch/common/io/stream/DelayableWriteable.java +++ b/server/src/main/java/org/elasticsearch/common/io/stream/DelayableWriteable.java @@ -238,7 +238,7 @@ public static long getSerializedSize(Writeable ref) { try (CountingStreamOutput out = new CountingStreamOutput()) { out.setTransportVersion(TransportVersion.current()); ref.writeTo(out); - return out.size(); + return out.position(); } catch (IOException exc) { throw new UncheckedIOException(exc); } diff --git a/server/src/main/java/org/elasticsearch/lucene/spatial/TriangleTreeWriter.java b/server/src/main/java/org/elasticsearch/lucene/spatial/TriangleTreeWriter.java index 821e43ec2a3b0..37d0fe3070463 100644 --- a/server/src/main/java/org/elasticsearch/lucene/spatial/TriangleTreeWriter.java +++ b/server/src/main/java/org/elasticsearch/lucene/spatial/TriangleTreeWriter.java @@ -197,7 +197,7 @@ private long nodeSize(boolean includeBox, int parentMaxX, int parentMaxY, Counti long rightSize = right.nodeSize(true, maxX, maxY, countingBuffer); countingBuffer.reset(); countingBuffer.writeVLong(rightSize); - size += countingBuffer.size(); // jump size + size += countingBuffer.position(); // jump size size += rightSize; } if (includeBox) { @@ -206,7 +206,7 @@ private long nodeSize(boolean includeBox, int parentMaxX, int parentMaxY, Counti countingBuffer.writeVLong((long) parentMaxX - maxX); countingBuffer.writeVLong((long) parentMaxY - maxY); countingBuffer.writeVLong(jumpSize); - size += countingBuffer.size(); // box size + size += countingBuffer.position(); // box size } return size; } @@ -229,7 +229,7 @@ private long componentSize(CountingStreamOutput countingBuffer) throws IOExcepti countingBuffer.writeVLong((long) maxX - component.cX); countingBuffer.writeVLong((long) maxY - component.cY); } - return countingBuffer.size(); + return countingBuffer.position(); } } } diff --git a/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java b/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java index 901d57bbdcb25..cd474e53149d4 100644 --- a/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java +++ b/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java @@ -930,14 +930,14 @@ private static class TestStreamOutput extends BytesStream { public void writeByte(byte b) { output.writeByte(b); counting.writeByte(b); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeBytes(byte[] b, int offset, int length) { output.writeBytes(b, offset, length); counting.writeBytes(b, offset, length); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override @@ -951,83 +951,83 @@ public long position() { public void writeInt(int i) throws IOException { output.writeInt(i); counting.writeInt(i); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeIntArray(int[] values) throws IOException { output.writeIntArray(values); counting.writeIntArray(values); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeLong(long i) throws IOException { output.writeLong(i); counting.writeLong(i); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeLongArray(long[] values) throws IOException { output.writeLongArray(values); counting.writeLongArray(values); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeFloat(float v) throws IOException { output.writeFloat(v); counting.writeFloat(v); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeFloatArray(float[] values) throws IOException { output.writeFloatArray(values); counting.writeFloatArray(values); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeDouble(double v) throws IOException { output.writeDouble(v); counting.writeDouble(v); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeDoubleArray(double[] values) throws IOException { output.writeDoubleArray(values); counting.writeDoubleArray(values); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeString(String str) throws IOException { output.writeString(str); counting.writeString(str); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeOptionalString(@Nullable String str) throws IOException { output.writeOptionalString(str); counting.writeOptionalString(str); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void writeGenericString(String value) throws IOException { output.writeGenericString(value); counting.writeGenericString(value); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public BytesReference bytes() { BytesReference bytesReference = output.bytes(); - assertThat((long) bytesReference.length(), equalTo(counting.size())); + assertThat((long) bytesReference.length(), equalTo(counting.position())); return bytesReference; } @@ -1038,7 +1038,7 @@ public void seek(long position) { public int size() { int size = output.size(); - assertThat((long) size, equalTo(counting.size())); + assertThat((long) size, equalTo(counting.position())); return size; } @@ -1046,12 +1046,12 @@ public int size() { public void flush() { output.flush(); counting.flush(); - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); } @Override public void close() { - assertThat((long) output.size(), equalTo(counting.size())); + assertThat((long) output.size(), equalTo(counting.position())); output.close(); counting.close(); } diff --git a/server/src/test/java/org/elasticsearch/common/io/stream/StreamOutputToBytesTests.java b/server/src/test/java/org/elasticsearch/common/io/stream/StreamOutputToBytesTests.java index d08e813478691..23b87d444f228 100644 --- a/server/src/test/java/org/elasticsearch/common/io/stream/StreamOutputToBytesTests.java +++ b/server/src/test/java/org/elasticsearch/common/io/stream/StreamOutputToBytesTests.java @@ -126,16 +126,16 @@ public void write(byte[] b, int off, int len) { return s -> s.writeGenericString(value); }); - while (countingStream.size() < targetSize) { + while (countingStream.position() < targetSize) { var writerIndex = between(0, writers.size() - 1); var writer = writers.get(writerIndex).get(); for (var stream : streams) { writer.accept(stream); } - assertEquals("recyclerBytesStream after " + writerIndex, countingStream.size(), recyclerBytesStream.position()); - assertEquals("plainBytesStream after " + writerIndex, countingStream.size(), plainBytesStream.position()); - assertEquals("bufferedStream after " + writerIndex, countingStream.size(), bufferedStream.position()); - assertEquals("wrappedStream after " + writerIndex, countingStream.size(), wrappedStream.position()); + assertEquals("recyclerBytesStream after " + writerIndex, countingStream.position(), recyclerBytesStream.position()); + assertEquals("plainBytesStream after " + writerIndex, countingStream.position(), plainBytesStream.position()); + assertEquals("bufferedStream after " + writerIndex, countingStream.position(), bufferedStream.position()); + assertEquals("wrappedStream after " + writerIndex, countingStream.position(), wrappedStream.position()); } isExpectedWriteSize.set(allOf(lessThanOrEqualTo(bufferLen), greaterThan(0))); // last write may be undersized