Package com.amazon.ion.impl
Class IonReaderLookaheadBuffer
- java.lang.Object
-
- com.amazon.ion.impl.IonReaderLookaheadBuffer
-
- All Implemented Interfaces:
ReaderLookaheadBuffer
public final class IonReaderLookaheadBuffer extends java.lang.ObjectMonitors an InputStream over binary Ion data to ensure enough data is available to be navigated successfully by a non-incremental IonReader.Error reporting: this wrapper reads the least amount of Ion data possible in order to determine whether a value is complete. As such, it will not raise any errors if invalid data exists anywhere outside the header of a top-level value. Any such invalid data will be detected as normal by the IonReader. In the few cases where this wrapper does detect an error (e.g. upon finding the illegal type 0xF), it will raise
IonException.
-
-
Field Summary
Fields Modifier and Type Field Description protected BufferConfiguration.DataHandlerdataHandlerThe handler that will be notified when data is processed.protected BufferConfiguration.OversizedValueHandleroversizedValueHandlerThe handler that will be notified when a value exceeds the maximum buffer size.protected ResizingPipedInputStreampipeA buffer for the bytes required for a successful call toIonReader.next().
-
Constructor Summary
Constructors Constructor Description IonReaderLookaheadBuffer(IonBufferConfiguration configuration, java.io.InputStream inputStream)Constructs a wrapper with the given configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Indicates how many bytes are currently stored in the internal buffer.voidclearMark()Clears the mark.voidfillInput()If possible, fills the input pipe with enough bytes to enable one more successful call toIonReader.next()on the non-incremental reader attached to the pipe.protected voidfillInputHelper()Implements the behavior described inReaderLookaheadBuffer.fillInput(), except for clearing the mark.protected java.io.InputStreamgetInput()protected intgetMaximumBufferSize()protected java.io.InputStreamgetPipe()protected booleanisSkippingCurrentValue()Indicates whether the current value is being skipped due to being oversized.voidmark()Marks the current read position in the underlying buffer.booleanmoreDataRequired()Indicates whether more data must become present in the raw input stream before a successful call toIonReader.next()may be made to position the reader on the most-recently-buffered value.IonReadernewIonReader(IonReaderBuilder builder)Builds a reader over the input pipe.voidrewind()Rewinds the underlying buffer to the mark.voidrewindToValueStart()Rewinds to the start of the value currently buffered.protected voidstartNewValue()Prepares for the start of a new value by clearing theisSkippingCurrentValueflag.protected voidstartSkippingValue()Start skipping the current value, if it is not already being skipped.
-
-
-
Field Detail
-
pipe
protected final ResizingPipedInputStream pipe
A buffer for the bytes required for a successful call toIonReader.next().
-
oversizedValueHandler
protected final BufferConfiguration.OversizedValueHandler oversizedValueHandler
The handler that will be notified when a value exceeds the maximum buffer size.
-
dataHandler
protected final BufferConfiguration.DataHandler dataHandler
The handler that will be notified when data is processed.
-
-
Constructor Detail
-
IonReaderLookaheadBuffer
public IonReaderLookaheadBuffer(IonBufferConfiguration configuration, java.io.InputStream inputStream)
Constructs a wrapper with the given configuration.- Parameters:
configuration- the configuration for the new instance.inputStream- an InputStream over binary Ion data.
-
-
Method Detail
-
fillInputHelper
protected void fillInputHelper() throws java.lang.ExceptionImplements the behavior described inReaderLookaheadBuffer.fillInput(), except for clearing the mark.- Throws:
java.io.IOException- if thrown by the underlying input stream.java.lang.Exception- if thrown by a handler method.
-
moreDataRequired
public boolean moreDataRequired()
Description copied from interface:ReaderLookaheadBufferIndicates whether more data must become present in the raw input stream before a successful call toIonReader.next()may be made to position the reader on the most-recently-buffered value.- Returns:
- true if more data is required to complete the next top-level user value; otherwise, false.
-
rewindToValueStart
public void rewindToValueStart()
Rewinds to the start of the value currently buffered. Does not include any system values that may precede the value. This method is not called in conjunction withmark()/rewind(), which may be used if the caller wants to rewind to the start of any system values that precede the current value. This method may be used to re-read the current value and may only be called afterIonReader.next()has been called on the current value; otherwise, the data representing any system values that precede the current value would be lost.- Throws:
java.lang.IllegalStateException- if there is no value currently buffered or if system value data would be lost as a result of calling this method beforeIonReader.next()was called.
-
fillInput
public final void fillInput() throws java.lang.ExceptionDescription copied from interface:ReaderLookaheadBufferIf possible, fills the input pipe with enough bytes to enable one more successful call toIonReader.next()on the non-incremental reader attached to the pipe. If not enough bytes were available in the raw input stream to complete the next top-level user value, callingReaderLookaheadBuffer.moreDataRequired()after this method returns will return `true`. In this case, this method must be called again before calling `IonReader.next()` to position the reader on this value. Otherwise, `moreDataRequired()` will return `false` and a call to `IonReader.next()` may be made to position the reader on this value. Implementations may throw `IonException` if invalid Ion data is detected. Implementations may define exceptional cases.- Specified by:
fillInputin interfaceReaderLookaheadBuffer- Throws:
java.lang.Exception- if an IOException is thrown by the underlying InputStream.
-
available
public final int available()
Description copied from interface:ReaderLookaheadBufferIndicates how many bytes are currently stored in the internal buffer. This can be used to detect whether calls to fillInput() are successfully retrieving data.- Specified by:
availablein interfaceReaderLookaheadBuffer- Returns:
- The number of bytes waiting in the input buffer.
-
newIonReader
public final IonReader newIonReader(IonReaderBuilder builder)
Description copied from interface:ReaderLookaheadBufferBuilds a reader over the input pipe. NOTE: because IonReader construction consumes bytes from the stream (to determine whether the stream is binary or text Ion),ReaderLookaheadBuffer.fillInput()must have been called (such thatReaderLookaheadBuffer.moreDataRequired()returns `false`) before calling this method. For the same reason, this method must only be called once per instance of this class.- Specified by:
newIonReaderin interfaceReaderLookaheadBuffer- Parameters:
builder- the builder containing the reader's configuration.- Returns:
- a new IonReader.
-
mark
public final void mark()
Marks the current read position in the underlying buffer. This mark remains valid only until the next call toReaderLookaheadBuffer.fillInput().- Throws:
java.lang.IllegalStateException- if more data is required to complete a value.
-
rewind
public final void rewind()
Rewinds the underlying buffer to the mark.- Throws:
java.lang.IllegalStateException- if there is no valid mark.- See Also:
mark()
-
clearMark
public final void clearMark()
Clears the mark.- See Also:
mark()
-
getPipe
protected java.io.InputStream getPipe()
- Returns:
- the underlying pipe.
-
getInput
protected java.io.InputStream getInput()
- Returns:
- the underlying input.
-
startNewValue
protected void startNewValue()
Prepares for the start of a new value by clearing theisSkippingCurrentValueflag.
-
startSkippingValue
protected void startSkippingValue() throws java.lang.ExceptionStart skipping the current value, if it is not already being skipped. This should be called when the value is determined to be oversize. This truncates the buffer to the end of the previous value, reclaiming the space.- Throws:
java.lang.Exception- if thrown by the event handler.
-
isSkippingCurrentValue
protected boolean isSkippingCurrentValue()
Indicates whether the current value is being skipped due to being oversized.- Returns:
- true if the value is being skipped; otherwise, false.
-
getMaximumBufferSize
protected int getMaximumBufferSize()
- Returns:
- the maximum size of the buffer.
-
-