use of org.apache.sis.internal.storage.io.InputStreamAdapter 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.InputStreamAdapter in project sis by apache.
the class StorageConnector method createInputStream.
/**
* Creates an input stream from {@link ReadableByteChannel} if possible, or from {@link ImageInputStream}
* otherwise.
*
* <p>This method is one of the {@link #OPENERS} methods and should be invoked at most once per
* {@code StorageConnector} instance.</p>
*/
private InputStream createInputStream() throws IOException, DataStoreException {
final Class<DataInput> source = DataInput.class;
final DataInput input = getStorageAs(source);
if (input instanceof InputStream) {
// Share the same Coupled instance.
views.put(InputStream.class, views.get(source));
return (InputStream) input;
} else if (input instanceof ImageInputStream) {
/*
* Wrap the ImageInputStream as an ordinary InputStream. We avoid setting CASCADE_ON_RESET (unless
* reset() needs to propagate further than ImageInputStream) because changes in InputStreamAdapter
* position are immediately reflected by corresponding changes in ImageInputStream position.
*/
final InputStream in = new InputStreamAdapter((ImageInputStream) input);
addView(InputStream.class, in, source, (byte) (getView(source).cascade & CASCADE_ON_RESET));
return in;
} else {
// Remember that there is no view.
addView(InputStream.class, null);
return null;
}
}
Aggregations