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

public final class RewindableLineReader extends LineNumberReader
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 Details

    • BUFFER_SIZE

      public static final int BUFFER_SIZE
      Size of the buffer, in number of characters.
      See Also:
    • input

      private InputStreamAdapter input
      The input stream, or null if this reader cannot rewind anymore.
      Note: we do not use the more generic InputStream class because this whole RewindableLineReader class is useless if we cannot seek in this stream.
    • encoding

      private final Charset encoding
      The character encoding, or null for the platform default.
    • isMarkProtected

      private boolean isMarkProtected
      Whether calls to mark(int) and reset() should throw IOException. This is initially false and may be set to true if the caller wants to block users from overwriting the mark (s)he just did. This flag changes also the close() 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 the probeContent(…) contract.
  • Constructor Details

    • RewindableLineReader

      public RewindableLineReader(InputStream input, Charset encoding) throws IOException
      Creates a line reader wrapping the given input stream.
      Parameters:
      input - the input stream from which to read characters.
      encoding - the character encoding, or null for the platform default.
      Throws:
      IOException - if an error occurred while marking the reader.
  • Method Details

    • rewind

      public RewindableLineReader rewind() throws IOException
      Returns a reader rewound to the beginning of data to read. This method invokes reset() first. If that call succeed, then this method returns this. 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

      public final void protectedMark() throws IOException
      Marks current stream position and blocks all subsequent calls to mark(int), reset() and close() until protectedReset() is invoked.
      Throws:
      IOException - if the stream cannot be marked.
    • protectedReset

      public final void protectedReset() throws IOException
      Stops the protection given by protectedMark() 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 is true by default but can be set to false if the mark is reserved to internal usage.
      Overrides:
      markSupported in class BufferedReader
      Returns:
      true if this stream supports the mark operation.
    • mark

      public void mark(int readlimit) throws IOException
      Marks the present position in the stream if allowed to do so. If isMarkProtected is true, then this method throws an exception.
      Overrides:
      mark in class LineNumberReader
      Parameters:
      readlimit - limit on the number of characters that may be read.
      Throws:
      IOException - if the stream cannot be marked.
    • reset

      public void reset() throws IOException
      Resets the stream if allowed to do so. If isMarkProtected is true, then this method throws an exception.
      Overrides:
      reset in class LineNumberReader
      Throws:
      IOException - if the stream cannot be reset.
    • close

      public void close() throws IOException
      Closes this reader. The underlying stream will be either reset or closed depending on the isMarkProtected mode.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class BufferedReader
      Throws:
      IOException - if an error occurred while closing the reader.