Search in sources :

Example 1 with RewindableLineReader

use of org.apache.sis.internal.storage.io.RewindableLineReader 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)

Example 2 with RewindableLineReader

use of org.apache.sis.internal.storage.io.RewindableLineReader in project sis by apache.

the class StorageConnector method createReader.

/**
 * Creates a character reader if possible.
 *
 * <p>This method is one of the {@link #OPENERS} methods and should be invoked at most once per
 * {@code StorageConnector} instance.</p>
 */
private Reader createReader() throws IOException, DataStoreException {
    final InputStream input = getStorageAs(InputStream.class);
    if (input == null) {
        // Remember that there is no view.
        addView(Reader.class, null);
        return null;
    }
    input.mark(DEFAULT_BUFFER_SIZE);
    final Reader in = new RewindableLineReader(input, getOption(OptionKey.ENCODING));
    addView(Reader.class, in, InputStream.class, (byte) (CLEAR_ON_RESET | CASCADE_ON_RESET));
    return in;
}
Also used : RewindableLineReader(org.apache.sis.internal.storage.io.RewindableLineReader) BufferedInputStream(java.io.BufferedInputStream) ChannelImageInputStream(org.apache.sis.internal.storage.io.ChannelImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) RewindableLineReader(org.apache.sis.internal.storage.io.RewindableLineReader) LineNumberReader(java.io.LineNumberReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Aggregations

RewindableLineReader (org.apache.sis.internal.storage.io.RewindableLineReader)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1 Reader (java.io.Reader)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1 ChannelImageInputStream (org.apache.sis.internal.storage.io.ChannelImageInputStream)1 InvalidSeekException (org.apache.sis.io.InvalidSeekException)1