-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a StreamInterceptor interface to allow users to plug in custom i…
…nterceptors for formats like Zstd.
- Loading branch information
Showing
7 changed files
with
328 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.ion; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
/** | ||
* The interceptor for GZIP streams. | ||
*/ | ||
public enum GZIPStreamInterceptor implements StreamInterceptor { | ||
|
||
INSTANCE; | ||
|
||
private static final byte[] GZIP_HEADER = {0x1F, (byte) 0x8B}; | ||
|
||
@Override | ||
public String formatName() { | ||
return "GZIP"; | ||
} | ||
|
||
@Override | ||
public int headerLength() { | ||
return GZIP_HEADER.length; | ||
} | ||
|
||
@Override | ||
public boolean matchesHeader(byte[] candidate, int offset, int length) { | ||
if (candidate == null || length < GZIP_HEADER.length) { | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < GZIP_HEADER.length; i++) { | ||
if (GZIP_HEADER[i] != candidate[offset + i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public InputStream newInputStream(InputStream interceptedStream) throws IOException { | ||
return new GZIPInputStream(interceptedStream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.ion; | ||
|
||
import com.amazon.ion.system.IonReaderBuilder; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* 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. | ||
* | ||
* @see com.amazon.ion.system.IonReaderBuilder#addStreamInterceptor(StreamInterceptor) | ||
* @see com.amazon.ion.system.IonSystemBuilder#withReaderBuilder(IonReaderBuilder) | ||
*/ | ||
public interface StreamInterceptor { | ||
|
||
/** | ||
* The name of the format the interceptor recognizes. | ||
* @return a constant String. | ||
*/ | ||
String formatName(); | ||
|
||
/** | ||
* The length of the byte header that identifies streams in this format. | ||
* @return the length in bytes. | ||
*/ | ||
int headerLength(); | ||
|
||
/** | ||
* 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 the candidate byte sequence. | ||
* @return true if the candidate byte sequence matches; otherwise, false. | ||
*/ | ||
boolean matchesHeader(byte[] candidate, int offset, int length); | ||
|
||
/** | ||
* Creates a new InputStream that transforms the bytes in the given InputStream into valid text or binary Ion. | ||
* @param interceptedStream the stream containing bytes in this format. | ||
* @return a new InputStream. | ||
* @throws IOException if thrown when constructing the new InputStream. | ||
*/ | ||
InputStream newInputStream(InputStream interceptedStream) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.