Check the following comment in RandomAccessSequenceAdapter.java:
// Always create a copy because Bytes.getBytes() returns a view
// and the RandomAccessSequenceAdapter.readBytes(int) method is unaware of
// the underlying buffer ownership, but users of Bytes assume it's immutable.
// We could make this conditional on `delegate instanceof Bytes` if we have to.
bytes = bytes.replicate();
This is protection from the caller to hold a reference on the input and modify the data later, which would change the parsed Bytes objects. As the comment suggests, this protection can be skipped, if the input is immutable, for example, when delegate is an instance of Bytes. This would avoid allocating some bytes, which can be very noticeable in heavy reading / parsing apps.
Check the following comment in
RandomAccessSequenceAdapter.java:This is protection from the caller to hold a reference on the input and modify the data later, which would change the parsed
Bytesobjects. As the comment suggests, this protection can be skipped, if the input is immutable, for example, whendelegateis an instance ofBytes. This would avoid allocating some bytes, which can be very noticeable in heavy reading / parsing apps.