Skip to content

Commit 6227247

Browse files
authored
Replace CountingStreamOutput#size() with #position() (#141594)
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.
1 parent 337e8b2 commit 6227247

File tree

6 files changed

+49
-54
lines changed

6 files changed

+49
-54
lines changed

server/src/main/java/org/elasticsearch/common/geo/SimpleFeatureFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private static byte[] writeCommands(final int[] commands, final int type, final
187187
output.writeVInt(24);
188188
output.writeVInt(type);
189189
output.writeVInt(34);
190-
output.writeVInt(Math.toIntExact(counting.size()));
190+
output.writeVInt(Math.toIntExact(counting.position()));
191191
for (int i = 0; i < length; i++) {
192192
output.writeVInt(commands[i]);
193193
}

server/src/main/java/org/elasticsearch/common/io/stream/CountingStreamOutput.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,102 +18,97 @@
1818
* A reusable @link {@link StreamOutput} that just count how many bytes are written.
1919
*/
2020
public class CountingStreamOutput extends StreamOutput {
21-
private long size;
21+
private long position;
2222

2323
/** reset the written byes to 0 */
2424
public void reset() {
25-
size = 0L;
26-
}
27-
28-
/** returns how many bytes would have been written -- TODO replace with position() from StreamOutput interface */
29-
public long size() {
30-
return size;
25+
position = 0L;
3126
}
3227

3328
@Override
3429
public void writeByte(byte b) {
35-
++size;
30+
++position;
3631
}
3732

3833
@Override
3934
public void writeBytes(byte[] b, int offset, int length) {
40-
size += length;
35+
position += length;
4136
}
4237

4338
@Override
4439
public long position() {
45-
return size;
40+
return position;
4641
}
4742

4843
@Override
4944
public void writeShort(short v) throws IOException {
50-
size += Short.BYTES;
45+
position += Short.BYTES;
5146
}
5247

5348
@Override
5449
public void writeInt(int i) {
55-
size += Integer.BYTES;
50+
position += Integer.BYTES;
5651
}
5752

5853
@Override
5954
public void writeIntLE(int i) throws IOException {
60-
size += Integer.BYTES;
55+
position += Integer.BYTES;
6156
}
6257

6358
@Override
6459
public void writeIntArray(int[] values) {
6560
writeVInt(values.length);
66-
size += (long) values.length * Integer.BYTES;
61+
position += (long) values.length * Integer.BYTES;
6762
}
6863

6964
@Override
7065
public void writeLong(long i) {
71-
size += Long.BYTES;
66+
position += Long.BYTES;
7267
}
7368

7469
@Override
7570
public void writeLongLE(long i) {
76-
size += Long.BYTES;
71+
position += Long.BYTES;
7772
}
7873

7974
@Override
8075
public void writeLongArray(long[] values) {
8176
writeVInt(values.length);
82-
size += (long) values.length * Long.BYTES;
77+
position += (long) values.length * Long.BYTES;
8378
}
8479

8580
@Override
8681
public void writeFloat(float v) {
87-
size += Float.BYTES;
82+
position += Float.BYTES;
8883
}
8984

9085
@Override
9186
public void writeFloatArray(float[] values) {
9287
writeVInt(values.length);
93-
size += (long) values.length * Float.BYTES;
88+
position += (long) values.length * Float.BYTES;
9489
}
9590

9691
@Override
9792
public void writeDouble(double v) {
98-
size += Double.BYTES;
93+
position += Double.BYTES;
9994
}
10095

10196
@Override
10297
public void writeDoubleArray(double[] values) {
10398
writeVInt(values.length);
104-
size += (long) values.length * Double.BYTES;
99+
position += (long) values.length * Double.BYTES;
105100
}
106101

107102
@Override
108103
public void writeVInt(int v) {
109104
// set LSB because 0 takes 1 byte
110-
size += (38 - Integer.numberOfLeadingZeros(v | 1)) / 7;
105+
position += (38 - Integer.numberOfLeadingZeros(v | 1)) / 7;
111106
}
112107

113108
@Override
114109
void writeVLongNoCheck(long v) {
115110
// set LSB because 0 takes 1 byte
116-
size += (70 - Long.numberOfLeadingZeros(v | 1L)) / 7;
111+
position += (70 - Long.numberOfLeadingZeros(v | 1L)) / 7;
117112
}
118113

119114
@Override
@@ -125,26 +120,26 @@ public void writeZLong(long i) {
125120
public void writeString(String str) {
126121
final int charCount = str.length();
127122
writeVInt(charCount);
128-
size += charCount;
123+
position += charCount;
129124
for (int i = 0; i < charCount; i++) {
130125
final int c = str.charAt(i);
131126
if (c > 0x007F) {
132-
size += c > 0x07FF ? 2 : 1;
127+
position += c > 0x07FF ? 2 : 1;
133128
}
134129
}
135130
}
136131

137132
@Override
138133
public void writeOptionalString(@Nullable String str) {
139-
size += 1;
134+
position += 1;
140135
if (str != null) {
141136
writeString(str);
142137
}
143138
}
144139

145140
@Override
146141
public void writeGenericString(String value) {
147-
size += 1;
142+
position += 1;
148143
writeString(value);
149144
}
150145

server/src/main/java/org/elasticsearch/common/io/stream/DelayableWriteable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public static long getSerializedSize(Writeable ref) {
238238
try (CountingStreamOutput out = new CountingStreamOutput()) {
239239
out.setTransportVersion(TransportVersion.current());
240240
ref.writeTo(out);
241-
return out.size();
241+
return out.position();
242242
} catch (IOException exc) {
243243
throw new UncheckedIOException(exc);
244244
}

server/src/main/java/org/elasticsearch/lucene/spatial/TriangleTreeWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private long nodeSize(boolean includeBox, int parentMaxX, int parentMaxY, Counti
197197
long rightSize = right.nodeSize(true, maxX, maxY, countingBuffer);
198198
countingBuffer.reset();
199199
countingBuffer.writeVLong(rightSize);
200-
size += countingBuffer.size(); // jump size
200+
size += countingBuffer.position(); // jump size
201201
size += rightSize;
202202
}
203203
if (includeBox) {
@@ -206,7 +206,7 @@ private long nodeSize(boolean includeBox, int parentMaxX, int parentMaxY, Counti
206206
countingBuffer.writeVLong((long) parentMaxX - maxX);
207207
countingBuffer.writeVLong((long) parentMaxY - maxY);
208208
countingBuffer.writeVLong(jumpSize);
209-
size += countingBuffer.size(); // box size
209+
size += countingBuffer.position(); // box size
210210
}
211211
return size;
212212
}
@@ -229,7 +229,7 @@ private long componentSize(CountingStreamOutput countingBuffer) throws IOExcepti
229229
countingBuffer.writeVLong((long) maxX - component.cX);
230230
countingBuffer.writeVLong((long) maxY - component.cY);
231231
}
232-
return countingBuffer.size();
232+
return countingBuffer.position();
233233
}
234234
}
235235
}

server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -930,14 +930,14 @@ private static class TestStreamOutput extends BytesStream {
930930
public void writeByte(byte b) {
931931
output.writeByte(b);
932932
counting.writeByte(b);
933-
assertThat((long) output.size(), equalTo(counting.size()));
933+
assertThat((long) output.size(), equalTo(counting.position()));
934934
}
935935

936936
@Override
937937
public void writeBytes(byte[] b, int offset, int length) {
938938
output.writeBytes(b, offset, length);
939939
counting.writeBytes(b, offset, length);
940-
assertThat((long) output.size(), equalTo(counting.size()));
940+
assertThat((long) output.size(), equalTo(counting.position()));
941941
}
942942

943943
@Override
@@ -951,83 +951,83 @@ public long position() {
951951
public void writeInt(int i) throws IOException {
952952
output.writeInt(i);
953953
counting.writeInt(i);
954-
assertThat((long) output.size(), equalTo(counting.size()));
954+
assertThat((long) output.size(), equalTo(counting.position()));
955955
}
956956

957957
@Override
958958
public void writeIntArray(int[] values) throws IOException {
959959
output.writeIntArray(values);
960960
counting.writeIntArray(values);
961-
assertThat((long) output.size(), equalTo(counting.size()));
961+
assertThat((long) output.size(), equalTo(counting.position()));
962962
}
963963

964964
@Override
965965
public void writeLong(long i) throws IOException {
966966
output.writeLong(i);
967967
counting.writeLong(i);
968-
assertThat((long) output.size(), equalTo(counting.size()));
968+
assertThat((long) output.size(), equalTo(counting.position()));
969969
}
970970

971971
@Override
972972
public void writeLongArray(long[] values) throws IOException {
973973
output.writeLongArray(values);
974974
counting.writeLongArray(values);
975-
assertThat((long) output.size(), equalTo(counting.size()));
975+
assertThat((long) output.size(), equalTo(counting.position()));
976976
}
977977

978978
@Override
979979
public void writeFloat(float v) throws IOException {
980980
output.writeFloat(v);
981981
counting.writeFloat(v);
982-
assertThat((long) output.size(), equalTo(counting.size()));
982+
assertThat((long) output.size(), equalTo(counting.position()));
983983
}
984984

985985
@Override
986986
public void writeFloatArray(float[] values) throws IOException {
987987
output.writeFloatArray(values);
988988
counting.writeFloatArray(values);
989-
assertThat((long) output.size(), equalTo(counting.size()));
989+
assertThat((long) output.size(), equalTo(counting.position()));
990990
}
991991

992992
@Override
993993
public void writeDouble(double v) throws IOException {
994994
output.writeDouble(v);
995995
counting.writeDouble(v);
996-
assertThat((long) output.size(), equalTo(counting.size()));
996+
assertThat((long) output.size(), equalTo(counting.position()));
997997
}
998998

999999
@Override
10001000
public void writeDoubleArray(double[] values) throws IOException {
10011001
output.writeDoubleArray(values);
10021002
counting.writeDoubleArray(values);
1003-
assertThat((long) output.size(), equalTo(counting.size()));
1003+
assertThat((long) output.size(), equalTo(counting.position()));
10041004
}
10051005

10061006
@Override
10071007
public void writeString(String str) throws IOException {
10081008
output.writeString(str);
10091009
counting.writeString(str);
1010-
assertThat((long) output.size(), equalTo(counting.size()));
1010+
assertThat((long) output.size(), equalTo(counting.position()));
10111011
}
10121012

10131013
@Override
10141014
public void writeOptionalString(@Nullable String str) throws IOException {
10151015
output.writeOptionalString(str);
10161016
counting.writeOptionalString(str);
1017-
assertThat((long) output.size(), equalTo(counting.size()));
1017+
assertThat((long) output.size(), equalTo(counting.position()));
10181018
}
10191019

10201020
@Override
10211021
public void writeGenericString(String value) throws IOException {
10221022
output.writeGenericString(value);
10231023
counting.writeGenericString(value);
1024-
assertThat((long) output.size(), equalTo(counting.size()));
1024+
assertThat((long) output.size(), equalTo(counting.position()));
10251025
}
10261026

10271027
@Override
10281028
public BytesReference bytes() {
10291029
BytesReference bytesReference = output.bytes();
1030-
assertThat((long) bytesReference.length(), equalTo(counting.size()));
1030+
assertThat((long) bytesReference.length(), equalTo(counting.position()));
10311031
return bytesReference;
10321032
}
10331033

@@ -1038,20 +1038,20 @@ public void seek(long position) {
10381038

10391039
public int size() {
10401040
int size = output.size();
1041-
assertThat((long) size, equalTo(counting.size()));
1041+
assertThat((long) size, equalTo(counting.position()));
10421042
return size;
10431043
}
10441044

10451045
@Override
10461046
public void flush() {
10471047
output.flush();
10481048
counting.flush();
1049-
assertThat((long) output.size(), equalTo(counting.size()));
1049+
assertThat((long) output.size(), equalTo(counting.position()));
10501050
}
10511051

10521052
@Override
10531053
public void close() {
1054-
assertThat((long) output.size(), equalTo(counting.size()));
1054+
assertThat((long) output.size(), equalTo(counting.position()));
10551055
output.close();
10561056
counting.close();
10571057
}

server/src/test/java/org/elasticsearch/common/io/stream/StreamOutputToBytesTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ public void write(byte[] b, int off, int len) {
126126
return s -> s.writeGenericString(value);
127127
});
128128

129-
while (countingStream.size() < targetSize) {
129+
while (countingStream.position() < targetSize) {
130130
var writerIndex = between(0, writers.size() - 1);
131131
var writer = writers.get(writerIndex).get();
132132
for (var stream : streams) {
133133
writer.accept(stream);
134134
}
135-
assertEquals("recyclerBytesStream after " + writerIndex, countingStream.size(), recyclerBytesStream.position());
136-
assertEquals("plainBytesStream after " + writerIndex, countingStream.size(), plainBytesStream.position());
137-
assertEquals("bufferedStream after " + writerIndex, countingStream.size(), bufferedStream.position());
138-
assertEquals("wrappedStream after " + writerIndex, countingStream.size(), wrappedStream.position());
135+
assertEquals("recyclerBytesStream after " + writerIndex, countingStream.position(), recyclerBytesStream.position());
136+
assertEquals("plainBytesStream after " + writerIndex, countingStream.position(), plainBytesStream.position());
137+
assertEquals("bufferedStream after " + writerIndex, countingStream.position(), bufferedStream.position());
138+
assertEquals("wrappedStream after " + writerIndex, countingStream.position(), wrappedStream.position());
139139
}
140140

141141
isExpectedWriteSize.set(allOf(lessThanOrEqualTo(bufferLen), greaterThan(0))); // last write may be undersized

0 commit comments

Comments
 (0)