Skip to content

Commit

Permalink
fix 104: make RandomAccessSequenceAdapter.readBytes(int) always make … (
Browse files Browse the repository at this point in the history
#171)

Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
  • Loading branch information
anthony-swirldslabs authored Jan 18, 2024
1 parent 6be210d commit 377ec5c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface Codec<T /*extends Record*/> {
* input
*/
@NonNull default T parse(@NonNull Bytes bytes) throws IOException {
return parse(bytes.toCopyingReadableSequentialData());
return parse(bytes.toReadableSequentialData());
}

/**
Expand Down Expand Up @@ -77,7 +77,7 @@ public interface Codec<T /*extends Record*/> {
* input
*/
@NonNull default T parseStrict(@NonNull Bytes bytes) throws IOException {
return parseStrict(bytes.toCopyingReadableSequentialData());
return parseStrict(bytes.toReadableSequentialData());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,6 @@ public ReadableSequentialData toReadableSequentialData() {
return new RandomAccessSequenceAdapter(this);
}

/**
* Create and return a new {@link ReadableSequentialData} that is backed by this {@link Bytes}
* and that returns a replicated Bytes object upon a call to readBytes(int length).
*
* @return A {@link ReadableSequentialData} backed by this {@link Bytes}.
*/
@NonNull
public ReadableSequentialData toCopyingReadableSequentialData() {
return new RandomAccessSequenceAdapter(this, true);
}

/**
* Exposes this {@link Bytes} as an {@link InputStream}. This is a zero-copy operation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ final class RandomAccessSequenceAdapter implements ReadableSequentialData {
/** The delegate {@link RandomAccessData} instance */
private final RandomAccessData delegate;

/** The `Bytes readBytes(int length)` will replicate the return value if true. */
private final boolean copyingBytes;

/**
* The capacity of this sequence will be the difference between the <b>initial</b> position and the
* length of the delegate
Expand All @@ -39,15 +36,6 @@ final class RandomAccessSequenceAdapter implements ReadableSequentialData {
this.capacity = delegate.length();
this.start = 0;
this.limit = this.capacity;
this.copyingBytes = false;
}

RandomAccessSequenceAdapter(@NonNull final RandomAccessData delegate, final boolean copyingBytes) {
this.delegate = delegate;
this.capacity = delegate.length();
this.start = 0;
this.limit = this.capacity;
this.copyingBytes = copyingBytes;
}

/**
Expand All @@ -60,7 +48,6 @@ final class RandomAccessSequenceAdapter implements ReadableSequentialData {
this.start = start;
this.capacity = delegate.length() - start;
this.limit = this.capacity;
this.copyingBytes = false;

if (this.start > delegate.length()) {
throw new IllegalArgumentException("Start " + start + " is greater than the delegate length " + delegate.length());
Expand Down Expand Up @@ -179,9 +166,13 @@ public Bytes readBytes(final int length) {
}

var bytes = delegate.getBytes(start + position, length);
if (copyingBytes) {
bytes = bytes.replicate();
}

// 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();

position += bytes.length();
return bytes;
}
Expand Down

0 comments on commit 377ec5c

Please sign in to comment.