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();
}
}
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;
}
Aggregations