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 ImageInputStream
The underlying data input stream.(package private) boolean
private int
Value ofnestedMarks
at the time whenmarkPosition
has been set, or -1 if none.private long
Position of the last mark created bymark(int)
.private int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this input stream.long
Returns the current byte position of the stream.void
mark()
Marks the current position in this input stream.void
mark
(int readlimit) Discards the previous mark created bymark(int)
and marks the current stream position.boolean
Returns alwaystrue
, since marks support is mandatory in image input stream.int
read()
Reads the next byte of data from the input stream.int
read
(byte[] b) Reads some number of bytes from the input stream.int
read
(byte[] b, int off, int len) Reads up tolen
bytes of data from the input stream.void
reset()
Repositions this stream to the position at the time themark
method was last called.void
reset
(long mark) Moves to the given position in the stream and discards all marks at or after that position.long
skip
(long n) Skips over and discardsn
bytes of data from this input stream.Methods inherited from class java.io.InputStream
available, nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
input
The underlying data input stream. In principle, public access to this field breaks encapsulation. But sinceInputStreamAdapter
does 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 ofnestedMarks
at the time whenmarkPosition
has 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:
read
in 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:
read
in 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 tolen
bytes of data from the input stream.- Overrides:
read
in 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 discardsn
bytes of data from this input stream.- Overrides:
skip
in 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:
markSupported
in 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 ofInputStream
API, where only one mark can be set and multiple calls toreset()
move to the same position untilmark(int)
is invoked again.- Overrides:
mark
in 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 ofMarkable
API, where marks can be nested. -
reset
Repositions this stream to the position at the time themark
method 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 thisInputStreamAdapter
class 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:
reset
in interfaceMarkable
- Overrides:
reset
in 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:
reset
in 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:
getStreamPosition
in interfaceMarkable
- Returns:
- the position of the stream.
- Throws:
IOException
- if the position cannot be obtained.
-
close
Closes this input stream.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
- if an I/O error occurs.
-