-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds a StreamInterceptor interface to allow users to plug in custom interceptors for formats like Zstd. #930
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} |
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(); | ||
Comment on lines
+26
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since some formats (eg Ion itself) may have variable-length headers, I'd make this a bit more general. Maybe |
||
|
||
/** | ||
* 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there some connection between this The class could use more docs on exactly how the matching process works. After seeing |
||
* @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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering whether this should have a sibling method for character streams, so one can accomplish transformations of text inputs. For example, suppose I want to teach the Ion reader to ignore shebang lines atop my Fusion scripts... |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Stream" has several meanings in this package, I suggest renaming to
InputStreamInterceptor
to be more specific.