Skip to content

Commit

Permalink
address review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
kiszk committed Apr 6, 2018
1 parent ba81497 commit 1d75cf3
Showing 1 changed file with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,20 @@ public UTF8StringBuilder() {

// Grows the buffer by at least `neededSize`
private void grow(int neededSize) {
if (neededSize > ARRAY_MAX - totalSize()) {
if (neededSize > ARRAY_MAX - length) {
throw new UnsupportedOperationException(
"Cannot grow internal buffer by size " + neededSize + " because the size after growing " +
"exceeds size limitation " + ARRAY_MAX);
}
final int length = totalSize() + neededSize;
if (buffer.size() < length) {
int newLength = length < ARRAY_MAX / 2 ? length * 2 : ARRAY_MAX;
final int requestedSize = length + neededSize;
if (buffer.size() < requestedSize) {
int newLength = requestedSize < ARRAY_MAX / 2 ? requestedSize * 2 : ARRAY_MAX;
final ByteArrayMemoryBlock tmp = new ByteArrayMemoryBlock(newLength);
MemoryBlock.copyMemory(buffer, tmp, totalSize());
MemoryBlock.copyMemory(buffer, tmp, length);
buffer = tmp;
}
}

private int totalSize() {
return length;
}

public void append(UTF8String value) {
grow(value.numBytes());
value.writeToMemory(buffer.getByteArray(), length + Platform.BYTE_ARRAY_OFFSET);
Expand All @@ -70,6 +66,6 @@ public void append(String value) {
}

public UTF8String build() {
return UTF8String.fromBytes(buffer.getByteArray(), 0, totalSize());
return UTF8String.fromBytes(buffer.getByteArray(), 0, length);
}
}

0 comments on commit 1d75cf3

Please sign in to comment.