Skip to content

Commit 44b1d87

Browse files
samyronbyroot
authored andcommitted
Use a ternary to determine the capacity of the next segment when growing the output buffer.
1 parent 32f3287 commit 44b1d87

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

java/src/json/ext/SegmentedByteListDirectOutputStream.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public void write(int b) throws IOException {
4040
}
4141
currentSegmentIndex++;
4242
int capacity = currentSegment.length * 2;
43-
if (capacity < 0) {
44-
capacity = Integer.MAX_VALUE - totalLength;
45-
}
43+
capacity = (capacity < 0) ? DEFAULT_CAPACITY : capacity;
4644
currentSegment = new byte[capacity];
4745
currentSegmentLength = 0;
4846
segments[currentSegmentIndex] = currentSegment;
@@ -61,10 +59,9 @@ public void write(byte[] bytes, int start, int length) throws IOException {
6159
throw new IOException("Total length exceeds maximum length of an array.");
6260
}
6361
currentSegmentIndex++;
64-
int capacity = currentSegment.length * 2;
65-
if (capacity < 0) {
66-
capacity = Integer.MAX_VALUE - totalLength;
67-
}
62+
int capacity = currentSegment.length << 1;
63+
capacity = (capacity < 0) ? DEFAULT_CAPACITY : capacity;
64+
capacity = (capacity < remaining) ? remaining : capacity;
6865
currentSegment = new byte[capacity];
6966
currentSegmentLength = 0;
7067
segments[currentSegmentIndex] = currentSegment;

0 commit comments

Comments
 (0)