Search in sources :

Example 1 with InputStreamAdapter

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());
}
Also used : DataInput(java.io.DataInput) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ImageInputStream(javax.imageio.stream.ImageInputStream) ChannelImageInputStream(org.apache.sis.internal.storage.io.ChannelImageInputStream) InputStream(java.io.InputStream) ChannelImageInputStream(org.apache.sis.internal.storage.io.ChannelImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ChannelImageInputStream(org.apache.sis.internal.storage.io.ChannelImageInputStream) InputStreamAdapter(org.apache.sis.internal.storage.io.InputStreamAdapter) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 2 with InputStreamAdapter

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;
    }
}
Also used : DataInput(java.io.DataInput) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput) BufferedInputStream(java.io.BufferedInputStream) ChannelImageInputStream(org.apache.sis.internal.storage.io.ChannelImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ChannelImageInputStream(org.apache.sis.internal.storage.io.ChannelImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStreamAdapter(org.apache.sis.internal.storage.io.InputStreamAdapter)

Aggregations

DataInput (java.io.DataInput)2 InputStream (java.io.InputStream)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2 ChannelDataInput (org.apache.sis.internal.storage.io.ChannelDataInput)2 ChannelImageInputStream (org.apache.sis.internal.storage.io.ChannelImageInputStream)2 InputStreamAdapter (org.apache.sis.internal.storage.io.InputStreamAdapter)2 BufferedInputStream (java.io.BufferedInputStream)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 DependsOnMethod (org.apache.sis.test.DependsOnMethod)1 Test (org.junit.Test)1