Search in sources :

Example 1 with InvalidSeekException

use of org.apache.sis.io.InvalidSeekException in project sis by apache.

the class Store method rewind.

/**
 * Moves the reader position to beginning of file, if possible. We try to use the mark defined by the constructor,
 * which is set after the last header line. If the mark is no longer valid, then we have to create a new line reader.
 * In this later case, we have to skip the header lines (i.e. we reproduce the constructor loop, but without parsing
 * metadata).
 */
private void rewind() throws IOException {
    final BufferedReader reader = source;
    if (!(reader instanceof RewindableLineReader)) {
        throw new InvalidSeekException(Resources.forLocale(getLocale()).getString(Resources.Keys.StreamIsForwardOnly_1, getDisplayName()));
    }
    source = ((RewindableLineReader) reader).rewind();
    if (source != reader) {
        String line;
        while ((line = source.readLine()) != null) {
            line = line.trim();
            if (!line.isEmpty()) {
                final char c = line.charAt(0);
                if (c != COMMENT && c != METADATA)
                    break;
            }
            source.mark(RewindableLineReader.BUFFER_SIZE);
        }
        // Restore position to the first line after the header.
        source.reset();
    }
}
Also used : RewindableLineReader(org.apache.sis.internal.storage.io.RewindableLineReader) InvalidSeekException(org.apache.sis.io.InvalidSeekException) BufferedReader(java.io.BufferedReader)

Aggregations

BufferedReader (java.io.BufferedReader)1 RewindableLineReader (org.apache.sis.internal.storage.io.RewindableLineReader)1 InvalidSeekException (org.apache.sis.io.InvalidSeekException)1