Class RewindableLineReader
java.lang.Object
java.io.Reader
java.io.BufferedReader
java.io.LineNumberReader
org.apache.sis.internal.storage.io.RewindableLineReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Readable
A
LineNumberReader
which may be rewound to its original position even if the mark is no longer valid.
This class assumes that mark(int)
has not been invoked, or has been invoked at the position where to
rewind. A call to rewind()
performs the following actions:
- Attempt to call
reset()
. - If
reset()
failed, then attempt to seek the input stream to its original position and create a new reader.
- Since:
- 0.8
- Version:
- 1.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Size of the buffer, in number of characters.private final Charset
The character encoding, ornull
for the platform default.private InputStreamAdapter
The input stream, ornull
if this reader cannot rewind anymore.private boolean
-
Constructor Summary
ConstructorsConstructorDescriptionRewindableLineReader
(InputStream input, Charset encoding) Creates a line reader wrapping the given input stream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this reader.void
mark
(int readlimit) Marks the present position in the stream if allowed to do so.boolean
Tells whether this stream supports the mark and reset operations.final void
Marks current stream position and blocks all subsequent calls tomark(int)
,reset()
andclose()
untilprotectedReset()
is invoked.final void
Stops the protection given byprotectedMark()
and reset the stream to its marked position.void
reset()
Resets the stream if allowed to do so.rewind()
Returns a reader rewound to the beginning of data to read.Methods inherited from class java.io.LineNumberReader
getLineNumber, read, read, readLine, setLineNumber, skip
Methods inherited from class java.io.BufferedReader
lines, ready
Methods inherited from class java.io.Reader
nullReader, read, read, transferTo
-
Field Details
-
BUFFER_SIZE
public static final int BUFFER_SIZESize of the buffer, in number of characters.- See Also:
-
input
The input stream, ornull
if this reader cannot rewind anymore.Note: we do not use the more genericInputStream
class because this wholeRewindableLineReader
class is useless if we cannot seek in this stream. -
encoding
The character encoding, ornull
for the platform default. -
isMarkProtected
private boolean isMarkProtectedWhether calls tomark(int)
andreset()
should throwIOException
. This is initiallyfalse
and may be set totrue
if the caller wants to block users from overwriting the mark (s)he just did. This flag changes also theclose()
behavior. It is used for probing the content of the same file by different data stores and we want a safety against implementations that do not follow theprobeContent(…)
contract.
-
-
Constructor Details
-
RewindableLineReader
Creates a line reader wrapping the given input stream.- Parameters:
input
- the input stream from which to read characters.encoding
- the character encoding, ornull
for the platform default.- Throws:
IOException
- if an error occurred while marking the reader.
-
-
Method Details
-
rewind
Returns a reader rewound to the beginning of data to read. This method invokesreset()
first. If that call succeed, then this method returnsthis
. Otherwise this method returns a new reader. In the latter case,this
reader should not be used anymore.- Returns:
- the reader to use for next read operation (may be
this
). - Throws:
IOException
- if an error occurred while rewinding the reader.
-
protectedMark
Marks current stream position and blocks all subsequent calls tomark(int)
,reset()
andclose()
untilprotectedReset()
is invoked.- Throws:
IOException
- if the stream cannot be marked.
-
protectedReset
Stops the protection given byprotectedMark()
and reset the stream to its marked position.- Throws:
IOException
- if the stream cannot be reset.
-
markSupported
public boolean markSupported()Tells whether this stream supports the mark and reset operations. This istrue
by default but can be set tofalse
if the mark is reserved to internal usage.- Overrides:
markSupported
in classBufferedReader
- Returns:
- true if this stream supports the mark operation.
-
mark
Marks the present position in the stream if allowed to do so. IfisMarkProtected
istrue
, then this method throws an exception.- Overrides:
mark
in classLineNumberReader
- Parameters:
readlimit
- limit on the number of characters that may be read.- Throws:
IOException
- if the stream cannot be marked.
-
reset
Resets the stream if allowed to do so. IfisMarkProtected
istrue
, then this method throws an exception.- Overrides:
reset
in classLineNumberReader
- Throws:
IOException
- if the stream cannot be reset.
-
close
Closes this reader. The underlying stream will be either reset or closed depending on theisMarkProtected
mode.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classBufferedReader
- Throws:
IOException
- if an error occurred while closing the reader.
-