Skip to content

Commit

Permalink
Renames InputStreamInterceptor methods to avoid referring to 'headers'.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgregg committed Jan 14, 2025
1 parent 6c97502 commit 514ca6c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/amazon/ion/impl/_Private_IonReaderBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ static IonReader buildReader(
) {
List<InputStreamInterceptor> streamInterceptors = builder.getInputStreamInterceptors();
for (InputStreamInterceptor streamInterceptor : streamInterceptors) {
int headerLength = streamInterceptor.headerMatchLength();
int headerLength = streamInterceptor.numberOfBytesNeededToDetermineMatch();
validateHeaderLength(headerLength);
if (length < headerLength) {
continue;
}
if (streamInterceptor.matchesHeader(ionData, offset, length)) {
if (streamInterceptor.isMatch(ionData, offset, length)) {
try {
return buildReader(
builder,
Expand Down Expand Up @@ -329,7 +329,7 @@ static IonReader buildReader(
}
int maxHeaderLength = Math.max(
_Private_IonConstants.BINARY_VERSION_MARKER_SIZE,
inputStreamInterceptors.stream().mapToInt(InputStreamInterceptor::headerMatchLength).max().orElse(0)
inputStreamInterceptors.stream().mapToInt(InputStreamInterceptor::numberOfBytesNeededToDetermineMatch).max().orElse(0)
);
validateHeaderLength(maxHeaderLength);
// Note: this can create a lot of layers of InputStream wrappers. For example, if this method is called
Expand All @@ -349,10 +349,10 @@ static IonReader buildReader(
// or it's a binary stream (in which case the correct reader was created) or it's a growing text stream
// (which has always been unsupported).
for (InputStreamInterceptor streamInterceptor : inputStreamInterceptors) {
if (bytesRead < streamInterceptor.headerMatchLength()) {
if (bytesRead < streamInterceptor.numberOfBytesNeededToDetermineMatch()) {
continue;
}
if (streamInterceptor.matchesHeader(possibleIVM, 0, bytesRead)) {
if (streamInterceptor.isMatch(possibleIVM, 0, bytesRead)) {
try {
ionData = streamInterceptor.newInputStream(
new TwoElementInputStream(new ByteArrayInputStream(possibleIVM, 0, bytesRead), ionData)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/amazon/ion/util/GzipStreamInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public String formatName() {
}

@Override
public int headerMatchLength() {
public int numberOfBytesNeededToDetermineMatch() {
return GZIP_HEADER.length;
}

@Override
public boolean matchesHeader(byte[] candidate, int offset, int length) {
public boolean isMatch(byte[] candidate, int offset, int length) {
if (candidate == null || length < GZIP_HEADER.length) {
return false;
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/amazon/ion/util/InputStreamInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

/**
* An interceptor to be consulted by the {@link com.amazon.ion.system.IonReaderBuilder} when creating an
* {@link IonReader} over a user-provided stream. This allows users to configure a sequence of interceptors
* to allow transformation of the stream's raw bytes into valid text or binary Ion.
* {@link IonReader} over a user-provided stream. This allows users to transform a stream's raw bytes into
* valid text or binary Ion.
*
* @see com.amazon.ion.system.IonReaderBuilder#addInputStreamInterceptor(InputStreamInterceptor)
* @see com.amazon.ion.system.IonSystemBuilder#withReaderBuilder(IonReaderBuilder)
Expand All @@ -26,22 +26,22 @@ public interface InputStreamInterceptor {

/**
* The number of bytes required to be read from the beginning of the stream in order to determine whether
* it matches this format. If a stream contains fewer than the number of bytes returned by this method, then
* {@link #matchesHeader(byte[], int, int)} will never be called and this interceptor will not be considered
* a match.
* it matches the format relevant to this interceptor. If a stream contains fewer than the number of bytes
* returned by this method, then this interceptor will not be considered a match and
* {@link #isMatch(byte[], int, int)} will not be called.
* @return the length in bytes.
*/
int headerMatchLength();
int numberOfBytesNeededToDetermineMatch();

/**
* Determines whether the given candidate byte sequence matches this format.
* @param candidate the candidate byte sequence.
* @param offset the offset into the candidate bytes to begin matching.
* @param length the number of bytes (beginning at 'offset') in `candidate`. Must be greater than or equal to
* {@link #headerMatchLength()}.
* {@link #numberOfBytesNeededToDetermineMatch()}.
* @return true if the candidate byte sequence matches; otherwise, false.
*/
boolean matchesHeader(byte[] candidate, int offset, int length);
boolean isMatch(byte[] candidate, int offset, int length);

/**
* Creates a new InputStream that transforms the bytes in the given InputStream into valid text or binary Ion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public String formatName() {
}

@Override
public int headerMatchLength() {
public int numberOfBytesNeededToDetermineMatch() {
return ZSTD_HEADER.length;
}

@Override
public boolean matchesHeader(byte[] candidate, int offset, int length) {
public boolean isMatch(byte[] candidate, int offset, int length) {
if (candidate == null || length < ZSTD_HEADER.length) {
return false;
}
Expand Down Expand Up @@ -362,12 +362,12 @@ public String formatName() {
}

@Override
public int headerMatchLength() {
public int numberOfBytesNeededToDetermineMatch() {
return length;
}

@Override
public boolean matchesHeader(byte[] candidate, int offset, int length) {
public boolean isMatch(byte[] candidate, int offset, int length) {
return Assertions.fail("This method should be unreachable.");
}

Expand Down

0 comments on commit 514ca6c

Please sign in to comment.