use of org.apache.sis.internal.storage.io.ChannelImageInputStream 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;
}
use of org.apache.sis.internal.storage.io.ChannelImageInputStream in project sis by apache.
the class StorageConnector method createImageInputStream.
/**
* Creates an {@link ImageInputStream} from the {@link DataInput} if possible. This method simply
* casts {@code DataInput} if such cast is allowed. Since {@link #createDataInput()} instantiates
* {@link ChannelImageInputStream}, this cast is usually possible.
*
* <p>This method is one of the {@link #OPENERS} methods and should be invoked at most once per
* {@code StorageConnector} instance.</p>
*/
private ImageInputStream createImageInputStream() throws DataStoreException {
final Class<DataInput> source = DataInput.class;
final DataInput input = getStorageAs(source);
if (input instanceof ImageInputStream) {
// Share the same Coupled instance.
views.put(ImageInputStream.class, views.get(source));
return (ImageInputStream) input;
} else {
// Remember that there is no view.
addView(ImageInputStream.class, null);
return null;
}
}
use of org.apache.sis.internal.storage.io.ChannelImageInputStream in project sis by apache.
the class StorageConnectorTest method testGetAsInputStream.
/**
* Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link InputStream} type.
* The {@code InputStream} was specified as a URL.
*
* @throws DataStoreException if an error occurred while using the storage connector.
* @throws IOException if an error occurred while reading the test file.
*/
@Test
@DependsOnMethod("testGetAsImageInputStream")
public void testGetAsInputStream() throws DataStoreException, IOException {
final StorageConnector connection = create(false);
final InputStream in = connection.getStorageAs(InputStream.class);
assertNotSame(connection.getStorage(), in);
assertSame("Expected cached value.", in, connection.getStorageAs(InputStream.class));
assertInstanceOf("Expected Channel backend.", InputStreamAdapter.class, in);
final ImageInputStream input = ((InputStreamAdapter) in).input;
assertInstanceOf("Expected Channel backend.", ChannelImageInputStream.class, input);
assertSame(input, connection.getStorageAs(DataInput.class));
assertSame(input, connection.getStorageAs(ImageInputStream.class));
final ReadableByteChannel channel = ((ChannelImageInputStream) input).channel;
assertTrue(channel.isOpen());
connection.closeAllExcept(null);
assertFalse(channel.isOpen());
}
use of org.apache.sis.internal.storage.io.ChannelImageInputStream in project sis by apache.
the class StorageConnectorTest method testCloseAllExcept.
/**
* Tests the {@link StorageConnector#closeAllExcept(Object)} method.
*
* @throws DataStoreException if an error occurred while using the storage connector.
* @throws IOException if an error occurred while reading the test file.
*/
@Test
@DependsOnMethod("testGetAsDataInputFromStream")
public void testCloseAllExcept() throws DataStoreException, IOException {
final StorageConnector connection = create(true);
final DataInput input = connection.getStorageAs(DataInput.class);
final ReadableByteChannel channel = ((ChannelImageInputStream) input).channel;
assertTrue("channel.isOpen()", channel.isOpen());
connection.closeAllExcept(input);
assertTrue("channel.isOpen()", channel.isOpen());
channel.close();
}
use of org.apache.sis.internal.storage.io.ChannelImageInputStream in project sis by apache.
the class StorageConnectorTest method testGetAsDataInput.
/**
* Implementation of {@link #testGetAsDataInputFromURL()} and {@link #testGetAsDataInputFromStream()}.
*/
private void testGetAsDataInput(final boolean asStream) throws DataStoreException, IOException {
final StorageConnector connection = create(asStream);
final DataInput input = connection.getStorageAs(DataInput.class);
assertSame("Value shall be cached.", input, connection.getStorageAs(DataInput.class));
assertInstanceOf("Needs the SIS implementation.", ChannelImageInputStream.class, input);
assertSame("Instance shall be shared.", input, connection.getStorageAs(ChannelDataInput.class));
/*
* Reads a single integer for checking that the stream is at the right position, then close the stream.
* Since the file is a compiled Java class, the integer that we read shall be the Java magic number.
*/
final ReadableByteChannel channel = ((ChannelImageInputStream) input).channel;
assertTrue("channel.isOpen()", channel.isOpen());
assertEquals("First 4 bytes", MAGIC_NUMBER, input.readInt());
connection.closeAllExcept(null);
assertFalse("channel.isOpen()", channel.isOpen());
}
Aggregations