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 intSize of the buffer, in number of characters.private final CharsetThe character encoding, ornullfor the platform default.private InputStreamAdapterThe input stream, ornullif 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 TypeMethodDescriptionvoidclose()Closes this reader.voidmark(int readlimit) Marks the present position in the stream if allowed to do so.booleanTells whether this stream supports the mark and reset operations.final voidMarks current stream position and blocks all subsequent calls tomark(int),reset()andclose()untilprotectedReset()is invoked.final voidStops the protection given byprotectedMark()and reset the stream to its marked position.voidreset()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, skipMethods inherited from class java.io.BufferedReader
lines, ready
-
Field Details
-
BUFFER_SIZE
public static final int BUFFER_SIZESize of the buffer, in number of characters.- See Also:
-
input
The input stream, ornullif this reader cannot rewind anymore.Note: we do not use the more genericInputStreamclass because this wholeRewindableLineReaderclass is useless if we cannot seek in this stream. -
encoding
The character encoding, ornullfor the platform default. -
isMarkProtected
private boolean isMarkProtectedWhether calls tomark(int)andreset()should throwIOException. This is initiallyfalseand may be set totrueif 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, ornullfor 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,thisreader 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 istrueby default but can be set tofalseif the mark is reserved to internal usage.- Overrides:
markSupportedin classBufferedReader- Returns:
- true if this stream supports the mark operation.
-
mark
Marks the present position in the stream if allowed to do so. IfisMarkProtectedistrue, then this method throws an exception.- Overrides:
markin 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. IfisMarkProtectedistrue, then this method throws an exception.- Overrides:
resetin classLineNumberReader- Throws:
IOException- if the stream cannot be reset.
-
close
Closes this reader. The underlying stream will be either reset or closed depending on theisMarkProtectedmode.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classBufferedReader- Throws:
IOException- if an error occurred while closing the reader.
-