Class SeekableInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.twelvemonkeys.io.SeekableInputStream
-
- All Implemented Interfaces:
Seekable,java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
AbstractCachedSeekableStream,FileSeekableStream,RandomAccessStream.InputStreamView
public abstract class SeekableInputStream extends java.io.InputStream implements Seekable
Abstract base class forInputStreams implementing theSeekableinterface.- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/SeekableInputStream.java#4 $
- See Also:
SeekableOutputStream
-
-
Field Summary
Fields Modifier and Type Field Description (package private) booleanclosed(package private) longflushedPositionprotected java.util.Stack<java.lang.Long>markedPositions(package private) longpositionThe stream position in this stream
-
Constructor Summary
Constructors Constructor Description SeekableInputStream()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidcheckOpen()voidclose()Closes the stream.protected abstract voidcloseImpl()protected voidfinalize()Finalizes this object prior to garbage collection.voidflush()Discards the initial position of the stream prior to the current stream position.voidflushBefore(long pPosition)Discards the initial portion of the stream prior to the indicated postion.protected abstract voidflushBeforeImpl(long pPosition)Discards the initial portion of the stream prior to the indicated postion.longgetFlushedPosition()Returns the earliest position in the stream to which seeking may be performed.longgetStreamPosition()Returns the current byte position of the stream.voidmark()Marks a position in the stream to be returned to by a subsequent call to reset.voidmark(int pLimit)booleanmarkSupported()Returnstrue, as marking is always supported.intread(byte[] pBytes)voidreset()Returns the file pointer to its previous position, at the time of the most recent unmatched call to mark.voidseek(long pPosition)Sets the current stream position to the desired location.protected abstract voidseekImpl(long pPosition)longskip(long pLength)Implemented usingseek(currentPos + pLength).-
Methods inherited from class java.io.InputStream
available, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, transferTo
-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.twelvemonkeys.io.Seekable
isCached, isCachedFile, isCachedMemory
-
-
-
-
Method Detail
-
read
public final int read(byte[] pBytes) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
skip
public final long skip(long pLength) throws java.io.IOExceptionImplemented usingseek(currentPos + pLength).- Overrides:
skipin classjava.io.InputStream- Parameters:
pLength- the number of bytes to skip- Returns:
- the actual number of bytes skipped (may be equal to or less
than
pLength) - Throws:
java.io.IOException- if an I/O exception occurs during skip
-
mark
public final void mark(int pLimit)
- Overrides:
markin classjava.io.InputStream
-
markSupported
public final boolean markSupported()
Returnstrue, as marking is always supported.- Overrides:
markSupportedin classjava.io.InputStream- Returns:
true.
-
seek
public final void seek(long pPosition) throws java.io.IOExceptionDescription copied from interface:SeekableSets the current stream position to the desired location. The next read will occur at this location.An
IndexOutOfBoundsExceptionwill be thrown if pPosition is smaller than the flushed position (as returned bySeekable.getFlushedPosition()).It is legal to seek past the end of the file; an
EOFExceptionwill be thrown only if a read is performed.
-
seekImpl
protected abstract void seekImpl(long pPosition) throws java.io.IOException- Throws:
java.io.IOException
-
mark
public final void mark()
Description copied from interface:SeekableMarks a position in the stream to be returned to by a subsequent call to reset. Unlike a standardInputStream, allSeekablestreams upport marking. Additionally, calls tomarkandresetmay be nested arbitrarily.Unlike the
markmethods declared by theReaderorInputStreaminterfaces, noreadLimitparameter is used. An arbitrary amount of data may be read following the call tomark.
-
reset
public final void reset() throws java.io.IOExceptionDescription copied from interface:SeekableReturns the file pointer to its previous position, at the time of the most recent unmatched call to mark.Calls to reset without a corresponding call to mark will either:
- throw an
IOException - or, reset to the beginning of the stream.
An
IOExceptionwill be thrown if the previous marked position lies in the discarded portion of the stream. - throw an
-
flushBefore
public final void flushBefore(long pPosition) throws java.io.IOExceptionDescription copied from interface:SeekableDiscards the initial portion of the stream prior to the indicated postion. Attempting to seek to an offset within the flushed portion of the stream will result in anIndexOutOfBoundsException.Calling
flushBeforemay allow classes implementing this interface to free up resources such as memory or disk space that are being used to store data from the stream.- Specified by:
flushBeforein interfaceSeekable- Parameters:
pPosition- a long containing the length of the file prefix that may be flushed.- Throws:
java.io.IOException- if an I/O error occurs.
-
flushBeforeImpl
protected abstract void flushBeforeImpl(long pPosition) throws java.io.IOExceptionDiscards the initial portion of the stream prior to the indicated postion.- Parameters:
pPosition- the position to flush to- Throws:
java.io.IOException- if an I/O exception occurs during the flush operation- See Also:
flushBefore(long)
-
flush
public final void flush() throws java.io.IOExceptionDescription copied from interface:SeekableDiscards the initial position of the stream prior to the current stream position. Equivalent toflushBefore(getStreamPosition()).
-
getFlushedPosition
public final long getFlushedPosition() throws java.io.IOExceptionDescription copied from interface:SeekableReturns the earliest position in the stream to which seeking may be performed. The returned value will be the maximum of all values passed into previous calls toflushBefore.- Specified by:
getFlushedPositionin interfaceSeekable- Returns:
- the earliest legal position for seeking, as a
long. - Throws:
java.io.IOException- if an I/O error occurs.
-
getStreamPosition
public final long getStreamPosition() throws java.io.IOExceptionDescription copied from interface:SeekableReturns the current byte position of the stream. The next read will take place starting at this offset.- Specified by:
getStreamPositionin interfaceSeekable- Returns:
- a
longcontaining the position of the stream. - Throws:
java.io.IOException- if an I/O error occurs.
-
checkOpen
protected final void checkOpen() throws java.io.IOException- Throws:
java.io.IOException
-
close
public final void close() throws java.io.IOExceptionDescription copied from interface:SeekableCloses the stream.
-
closeImpl
protected abstract void closeImpl() throws java.io.IOException- Throws:
java.io.IOException
-
finalize
protected void finalize() throws java.lang.ThrowableFinalizes this object prior to garbage collection. Theclosemethod is called to close any open input source. This method should not be called from application code.- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable- if an error occurs during superclass finalization.
-
-