Class InputStreamAdapter
java.lang.Object
java.io.InputStream
org.apache.sis.internal.storage.io.InputStreamAdapter
- All Implemented Interfaces:
Closeable,AutoCloseable,Markable
Wraps an
ImageInputStream as a standard InputStream.
Thread-safety
This class is thread-safe only if the underlyingImageInputStream is itself thread-safe.
For performance reasons, this class does not synchronize the frequently invoked read(…)
methods since they do nothing else than delegating to ImageInputStream. This means that
if the wrapped input is ChannelImageInputStream, then this class is not
thread-safe. This is not necessarily a contradiction with Java API since input streams define no
explicit synchronization lock (contrarily to Reader.- Since:
- 0.4
- Version:
- 1.2
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal ImageInputStreamThe underlying data input stream.(package private) booleanprivate intValue ofnestedMarksat the time whenmarkPositionhas been set, or -1 if none.private longPosition of the last mark created bymark(int).private int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this input stream.longReturns the current byte position of the stream.voidmark()Marks the current position in this input stream.voidmark(int readlimit) Discards the previous mark created bymark(int)and marks the current stream position.booleanReturns alwaystrue, since marks support is mandatory in image input stream.intread()Reads the next byte of data from the input stream.intread(byte[] b) Reads some number of bytes from the input stream.intread(byte[] b, int off, int len) Reads up tolenbytes of data from the input stream.voidreset()Repositions this stream to the position at the time themarkmethod was last called.voidreset(long mark) Moves to the given position in the stream and discards all marks at or after that position.longskip(long n) Skips over and discardsnbytes of data from this input stream.Methods inherited from class java.io.InputStream
available
-
Field Details
-
input
The underlying data input stream. In principle, public access to this field breaks encapsulation. But sinceInputStreamAdapterdoes not hold any state and just forwards every method calls to thatImageInputStream, using on object or the other does not make a difference. -
markPosition
private long markPosition -
markIndex
private int markIndexValue ofnestedMarksat the time whenmarkPositionhas been set, or -1 if none. Used for differentiating the (single) mark created bymark(int)from the (possibly many) marks created bymark(). This complexity exists becausereset()must comply with two inconsistentmark(…)method contracts. -
nestedMarks
private int nestedMarksCount of marks created bymark(), not counting the mark created bymark(int). We have to keep this count ourselves becauseImageInputStream.reset()does nothing if there is no mark, and provides no API for letting us know ifreset()worked. -
keepOpen
boolean keepOpen- See Also:
-
-
Constructor Details
-
InputStreamAdapter
Constructs a new input stream.- Parameters:
input- the stream to wrap.- Throws:
IOException- if an error occurred while creating the adapter.
-
-
Method Details
-
read
Reads the next byte of data from the input stream.- Specified by:
readin classInputStream- Returns:
- the next byte, or -1 if the end of the stream is reached.
- Throws:
IOException- if an I/O error occurs.
-
read
Reads some number of bytes from the input stream.- Overrides:
readin classInputStream- Returns:
- total number of bytes read, or -1 if the end of the stream has been reached.
- Throws:
IOException- if an I/O error occurs.
-
read
Reads up tolenbytes of data from the input stream.- Overrides:
readin classInputStream- Returns:
- total number of bytes read, or -1 if the end of the stream has been reached.
- Throws:
IOException- if an I/O error occurs.
-
skip
Skips over and discardsnbytes of data from this input stream.- Overrides:
skipin classInputStream- Returns:
- total number of bytes skipped.
- Throws:
IOException- if an I/O error occurs.
-
markSupported
public boolean markSupported()Returns alwaystrue, since marks support is mandatory in image input stream.- Overrides:
markSupportedin classInputStream- Returns:
true.
-
mark
public void mark(int readlimit) Discards the previous mark created bymark(int)and marks the current stream position. This method is part ofInputStreamAPI, where only one mark can be set and multiple calls toreset()move to the same position untilmark(int)is invoked again.- Overrides:
markin classInputStream- Parameters:
readlimit- ignored.- Throws:
UncheckedIOException- if the mark cannot be set.
-
mark
public void mark()Marks the current position in this input stream. This method is part ofMarkableAPI, where marks can be nested. -
reset
Repositions this stream to the position at the time themarkmethod was last called. This method has to comply with bothInputStream.reset()andMarkable.reset()contracts. It does that by pulling from most recent mark to oldest mark regardless if marks were created bymark()ormark(int), except that all marks created bymark(int)are ignored except the most recent one.Implementations of
reset()in Java I/O package does not discard the mark. The implementation in thisInputStreamAdapterclass does not discard the mark neither if the mark done by a call tomark(int)is the only mark remaining. Some code depends on the ability to do manyreset()for the same mark.- Specified by:
resetin interfaceMarkable- Overrides:
resetin classInputStream- Throws:
IOException- if this stream cannot move to the last mark position.- See Also:
-
reset
Moves to the given position in the stream and discards all marks at or after that position. This convolved method exists because of the attempt to conciliate two different APIs in this class (seereset()). This method does not simply callImageInputStream.seek(long)because we need to keep track of the marks.- Specified by:
resetin interfaceMarkable- Parameters:
mark- position where to seek.- Throws:
IOException- if this stream cannot move to the specified mark position.
-
getStreamPosition
Returns the current byte position of the stream.- Specified by:
getStreamPositionin interfaceMarkable- Returns:
- the position of the stream.
- Throws:
IOException- if the position cannot be obtained.
-
close
Closes this input stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- if an I/O error occurs.
-