use of org.apache.sis.internal.storage.io.ChannelFactory in project sis by apache.
the class StorageConnector method createChannelDataInput.
/**
* Creates a view for the input as a {@link ChannelDataInput} if possible.
* This is also a starting point for {@link #createDataInput()} and {@link #createByteBuffer()}.
* This method is one of the {@link #OPENERS} methods and should be invoked at most once per
* {@code StorageConnector} instance.
*
* @param asImageInputStream whether the {@code ChannelDataInput} needs to be {@link ChannelImageInputStream} subclass.
* @throws IOException if an error occurred while opening a channel for the input.
*/
private ChannelDataInput createChannelDataInput(final boolean asImageInputStream) throws IOException, DataStoreException {
/*
* Before to try to wrap an InputStream, mark its position so we can rewind if the user asks for
* the InputStream directly. We need to reset because ChannelDataInput may have read some bytes.
* Note that if mark is unsupported, the default InputStream.mark(…) implementation does nothing.
*/
reset();
if (storage instanceof InputStream) {
((InputStream) storage).mark(DEFAULT_BUFFER_SIZE);
}
/*
* Following method call recognizes ReadableByteChannel, InputStream (with special case for FileInputStream),
* URL, URI, File, Path or other types that may be added in future Apache SIS versions.
* If the given storage is already a ReadableByteChannel, then the factory will return it as-is.
*/
final ChannelFactory factory = ChannelFactory.prepare(storage, getOption(OptionKey.URL_ENCODING), false, getOption(OptionKey.OPEN_OPTIONS));
if (factory == null) {
return null;
}
/*
* ChannelDataInput depends on ReadableByteChannel, which itself depends on storage
* (potentially an InputStream). We need to remember this chain in 'Coupled' objects.
*/
final String name = getStorageName();
final ReadableByteChannel channel = factory.readable(name, null);
addView(ReadableByteChannel.class, channel, null, factory.isCoupled() ? CASCADE_ON_RESET : 0);
// User-supplied buffer.
ByteBuffer buffer = getOption(OptionKey.BYTE_BUFFER);
if (buffer == null) {
// Default buffer if user did not specified any.
buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
}
final ChannelDataInput asDataInput;
if (asImageInputStream) {
asDataInput = new ChannelImageInputStream(name, channel, buffer, false);
} else {
asDataInput = new ChannelDataInput(name, channel, buffer, false);
}
addView(ChannelDataInput.class, asDataInput, ReadableByteChannel.class, CASCADE_ON_RESET);
/*
* Following is an undocumented mechanism for allowing some Apache SIS implementations of DataStore
* to re-open the same channel or input stream another time, typically for re-reading the same data.
*/
if (factory.canOpen()) {
addView(ChannelFactory.class, factory);
}
return asDataInput;
}
Aggregations