Class RewindableInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.hortonmachine.gears.utils.style.sld.RewindableInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class RewindableInputStream extends InputStream
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_XMLDECL_BUFFER_SIZEDefault buffer size before we've finished with the XMLDecl: I think the name should be left unchanged to give a hint for possible use of this class :)protected byte[]fDataInternal buffer of bytes already read from source input stream.protected intfEndOffsetPosition where the end of underlying stream was encountered.protected InputStreamfInputStreamSource input stream we are wrapping with rewindable one.protected intfLengthNumber of read bytes currently stored infDatabuffer.protected intfMarkOffset of the "marked" position in the stream relative to its beginning.protected booleanfMayReadChunksTells whetherread(byte[], int, int)method is allowed to read multiple bytes beyond the internal buffer (true) or not (trueis the default)protected intfOffsetOffset of the next byte to be read from the stream relative to the beginning of the stream (andfDataarray as well)protected intfStartOffsetPosition in the stream that to which stream pointer can be reset withrewindmethod invocation.
-
Constructor Summary
Constructors Constructor Description RewindableInputStream(InputStream is)Creates newRewindableInputStreamobject with internal buffer of default size and default value of chunked reading flag (which is _currently_true).RewindableInputStream(InputStream is, boolean chunkedMode)Creates new RewindableInputStream with internal buffer of specified size and no chunk reading beyound the buffer limits allowed.RewindableInputStream(InputStream is, boolean chunkedMode, int initialSize)Primary constructor that allows to specify all parameters exlicitly affecting class work (initial size of the internal buffer and chunk read mode).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.voidclose()Closes underlying byte stream.voiddisableChunkedMode()More conscious alias forsetChunkedMode(false).voidenableChunkedMode()More conscious alias forsetChunkedMode(true).voidmark(int howMuch)Sets a mark to the current position in the stream.booleanmarkSupported()Tells that this stream supports mark/reset capability.intread()Reads next byte from this stream.intread(byte[] b, int off, int len)Reads up to len bytes of data from the input stream into an array of bytes.voidreset()Returns stream pointer to the position previously remembered usingmarkmethod (or to beginning of the stream, if there were nomarkmethod calls).voidrewind()Quickly reset stream pointer to the beginning of the stream or to position which offset was specified during the lastsetStartOffsetcall.voidsetChunkedMode(boolean chunkedMode)Allows to change the behavior of the stream regarding chunked reading at runtime.voidsetStartOffset(int offset)Sets the position somewhere in the stream to which the stream pointer will be reset afterrewindinvocation.longskip(long n)Skips over and discardsnbytes of data from this input stream.-
Methods inherited from class java.io.InputStream
nullInputStream, read, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Field Detail
-
DEFAULT_XMLDECL_BUFFER_SIZE
public static final int DEFAULT_XMLDECL_BUFFER_SIZE
Default buffer size before we've finished with the XMLDecl: I think the name should be left unchanged to give a hint for possible use of this class :)- See Also:
- Constant Field Values
-
fMayReadChunks
protected boolean fMayReadChunks
Tells whetherread(byte[], int, int)method is allowed to read multiple bytes beyond the internal buffer (true) or not (trueis the default)
-
fInputStream
protected InputStream fInputStream
Source input stream we are wrapping with rewindable one.
-
fData
protected byte[] fData
Internal buffer of bytes already read from source input stream. Allows to access the same byte more than once.
-
fStartOffset
protected int fStartOffset
Position in the stream that to which stream pointer can be reset withrewindmethod invocation.
-
fEndOffset
protected int fEndOffset
Position where the end of underlying stream was encountered. Potentially inRewindableInputStreaminstance stream's "end" could be reached more than once, so it is a good thing to know where original stream ended to avoidIOExceptions.
-
fOffset
protected int fOffset
Offset of the next byte to be read from the stream relative to the beginning of the stream (andfDataarray as well)
-
fLength
protected int fLength
Number of read bytes currently stored infDatabuffer. Size of the buffer itself can be greater than this value, obviously.
-
fMark
protected int fMark
Offset of the "marked" position in the stream relative to its beginning.
-
-
Constructor Detail
-
RewindableInputStream
public RewindableInputStream(InputStream is)
Creates newRewindableInputStreamobject with internal buffer of default size and default value of chunked reading flag (which is _currently_true).- Parameters:
is- InputStream that needs basic reset/rewind functionality.
-
RewindableInputStream
public RewindableInputStream(InputStream is, boolean chunkedMode)
Creates new RewindableInputStream with internal buffer of specified size and no chunk reading beyound the buffer limits allowed.- Parameters:
is- InputStream that needs some reset/rewind functionality.chunkedMode- See theRewindableInputStream(InputStream, boolean, int)constructor description.
-
RewindableInputStream
public RewindableInputStream(InputStream is, boolean chunkedMode, int initialSize)
Primary constructor that allows to specify all parameters exlicitly affecting class work (initial size of the internal buffer and chunk read mode).- Parameters:
is- InputStream that needs some reset/rewind functionality.chunkedMode-Initial value of
fMayReadChunksflag which determines whether multiple bytes can be read from the underlying stream in single reading operation or not. This value can be changed usingsetChunkedMode(or its aliases). For specific purpose of inferring encoding/charset of XML document typical usage policy is to disable chunked reads while obtaining XML declaration and then enable it to speed up reading the rest of document.initialSize- Initial size of the internal buffer array.
-
-
Method Detail
-
setStartOffset
public void setStartOffset(int offset)
Sets the position somewhere in the stream to which the stream pointer will be reset afterrewindinvocation. By default this position is the beginning of the stream.- Parameters:
offset- New value for "fStartOffset".
-
setChunkedMode
public void setChunkedMode(boolean chunkedMode)
Allows to change the behavior of the stream regarding chunked reading at runtime. If you allowed chunked reading and then read some data from the stream, you better forget aboutresetting orrewinding it after that.- Parameters:
chunkedMode- New value forfMayReadChunks.
-
enableChunkedMode
public void enableChunkedMode()
More conscious alias forsetChunkedMode(true). While last method is a general purpose mutator, code may look a bit more clear if you use specialized methods to enable/disable chunk read mode.
-
disableChunkedMode
public void disableChunkedMode()
More conscious alias forsetChunkedMode(false). While last method is a general purpose mutator, code may look a bit more clear if you use specialized methods to enable/disable chunk read mode.
-
rewind
public void rewind()
Quickly reset stream pointer to the beginning of the stream or to position which offset was specified during the lastsetStartOffsetcall.
-
read
public int read() throws IOExceptionReads next byte from this stream. This byte is either being read from underlying InputStream or taken from the internal buffer in case it was already read at some point before.- Specified by:
readin classInputStream- Returns:
- Next byte of data or
-1if end of stream is reached. - Throws:
IOException- in case of any I/O errors.
-
read
public int read(byte[] b, int off, int len) throws IOExceptionReads up to len bytes of data from the input stream into an array of bytes. In its current implementation it cannot return more bytes than left in the buffer if in "non-chunked" mode (fMayReadChunks == false). After reaching the end of the buffer, each invocation of this method will read exactly 1 byte then. In "chunked" mode this method may return more than 1 byte, but it doesn't buffer the result.From the other hand, for the task of reading xml declaration, such behavior may be desirable, as we probably don't need reset/rewind functionality after we finished with charset deduction. It is good idea to call
enableChunkedModeafter that, in order to improve perfomance and lessen memoery consumption when reading the rest of the data.- Overrides:
readin classInputStream- Returns:
- Total number of bytes actually read or
-1if end of stream has been reached. - Throws:
IOException- when an I/O error occurs while reading dataIndexOutOfBoundsException- in case of invalidoff,lenandb.lengthcombination
-
skip
public long skip(long n) throws IOExceptionSkips over and discardsnbytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly0. The actual number of bytes skipped is returned. Ifnis negative, no bytes are skipped.- Overrides:
skipin classInputStream- Parameters:
n- Number of bytes to be skipped.- Returns:
- Number of bytes actually skipped.
- Throws:
IOException- if an I/O error occurs.
-
available
public int available() throws IOExceptionReturns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. ForRewindableInputStreamthis can be:- Number of unread bytes in the
fDatabuffer, i.e. those between current position (fOffset) and total bytes quantity in the buffer (fLength). - Result of underlying InputStream's
availablecall if there are no unread bytes in the buffer. -1if end of stream is reached.
- Overrides:
availablein classInputStream- Returns:
- the number of bytes that can be read from this input stream without blocking.
- Throws:
IOException- when an I/O error occurs.
- Number of unread bytes in the
-
mark
public void mark(int howMuch)
Sets a mark to the current position in the stream.- Overrides:
markin classInputStream- Parameters:
howMuch- Not used in this implementation I guess.
-
reset
public void reset()
Returns stream pointer to the position previously remembered usingmarkmethod (or to beginning of the stream, if there were nomarkmethod calls).- Overrides:
resetin classInputStream
-
markSupported
public boolean markSupported()
Tells that this stream supports mark/reset capability. This one definitely supports it :)- Overrides:
markSupportedin classInputStream- Returns:
trueif this stream instance supports the mark and reset methods;falseotherwise.
-
close
public void close() throws IOExceptionCloses underlying byte stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- if an I/O error occurs.
-
-