Skip to content

Commit

Permalink
forget copy method
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase committed Oct 15, 2023
1 parent ae73aa9 commit 4ee5fb0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,6 @@ public void reset() throws IOException {
}
}

@Override
protected BytesReference readPagedBytesReference(int length) throws IOException {
final int offset = offset();
skip(length); // advance stream
return bytesReference.copy(offset, length);
}

@Override
public boolean markSupported() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,31 +164,25 @@ public BytesReference readBytesReference(int length) throws IOException {
if (length == 0) {
return BytesArray.EMPTY;
} else if (length < ByteSizeValue.ofMb(1).getBytes()) {
// if the length is small enough we can just copy the bytes in a single array
// if the bytes reference is small enough we can just copy the bytes in a single array
byte[] bytes = new byte[length];
readBytes(bytes, 0, length);
return new BytesArray(bytes, 0, length);
} else {
return readPagedBytesReference(length);
// paginate the bytes reference to avoid allocating a single byte array that is too large
final BytesReference br = BytesReference.fromByteArray(BigArrays.NON_RECYCLING_INSTANCE.newByteArray(length), length);
final BytesRefIterator iterator = br.iterator();
BytesRef bytesRef;
int offset = 0;
while ((bytesRef = iterator.next()) != null) {
final int len = Math.min(bytesRef.length, length - offset);
readBytes(bytesRef.bytes, bytesRef.offset, len);
offset += len;
}
return br;
}
}

/**
* Reads a bytes reference using pagination if necessary.
*/
protected BytesReference readPagedBytesReference(int length) throws IOException {
final BytesReference bytesReference = BytesReference.fromByteArray(BigArrays.NON_RECYCLING_INSTANCE.newByteArray(length), length);
final BytesRefIterator iterator = bytesReference.iterator();
BytesRef bytesRef;
int offset = 0;
while ((bytesRef = iterator.next()) != null) {
final int len = Math.min(bytesRef.length, length - offset);
readBytes(bytesRef.bytes, bytesRef.offset, len);
offset += len;
}
return bytesReference;
}

public BytesRef readBytesRef() throws IOException {
int length = readArraySize();
return readBytesRef(length);
Expand Down

0 comments on commit 4ee5fb0

Please sign in to comment.