Search in sources :

Example 6 with ChannelImageInputStream

use of org.apache.sis.internal.storage.io.ChannelImageInputStream in project sis by apache.

the class StorageConnector method createDataInput.

/**
 * Creates a view for the input as a {@link DataInput} if possible. This method performs the choice
 * documented in the {@link #getStorageAs(Class)} method for the {@code DataInput} case. Opening the
 * data input may imply creating a {@link ByteBuffer}, in which case the buffer will be stored under
 * the {@code ByteBuffer.class} key together with the {@code DataInput.class} case.
 *
 * <p>This method is one of the {@link #OPENERS} methods and should be invoked at most once per
 * {@code StorageConnector} instance.</p>
 *
 * @throws IOException if an error occurred while opening a stream for the input.
 */
private DataInput createDataInput() throws IOException, DataStoreException {
    /*
         * Gets or creates a ChannelImageInputStream instance if possible. We really need that specific
         * type because some SIS data stores will want to access directly the channel and the buffer.
         * We will fallback on the ImageIO.createImageInputStream(Object) method only in last resort.
         */
    Coupled c = getView(ChannelDataInput.class);
    final ChannelDataInput in;
    if (reset(c)) {
        in = (ChannelDataInput) c.view;
    } else {
        // May be null.
        in = createChannelDataInput(true);
    }
    final DataInput asDataInput;
    if (in != null) {
        // May have been added by createChannelDataInput(…).
        c = getView(ChannelDataInput.class);
        if (in instanceof DataInput) {
            asDataInput = (DataInput) in;
        } else {
            // Upgrade existing instance.
            asDataInput = new ChannelImageInputStream(in);
            c.view = asDataInput;
        }
        // Share the same Coupled instance.
        views.put(DataInput.class, c);
    } else {
        reset();
        asDataInput = ImageIO.createImageInputStream(storage);
        addView(DataInput.class, asDataInput, null, (byte) (CASCADE_ON_RESET | CASCADE_ON_CLOSE));
    /*
             * Note: Java Image I/O wrappers for Input/OutputStream do NOT close the underlying streams.
             * This is a complication for us. We could mitigate the problem by subclassing the standard
             * FileCacheImageInputStream and related classes, but we don't do that for now because this
             * code should never be executed for InputStream storage. Instead getChannelDataInput(true)
             * should have created a ChannelImageInputStream or ChannelDataInput.
             */
    }
    return asDataInput;
}
Also used : DataInput(java.io.DataInput) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput) ChannelImageInputStream(org.apache.sis.internal.storage.io.ChannelImageInputStream) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput)

Example 7 with ChannelImageInputStream

use of org.apache.sis.internal.storage.io.ChannelImageInputStream in project sis by apache.

the class StorageConnectorTest method testGetAsChannelDataInput.

/**
 * Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link ChannelDataInput} type.
 * The initial value should not be an instance of {@link ChannelImageInputStream} in order to avoid initializing
 * the Image I/O classes. However after a call to {@code getStorageAt(ChannelImageInputStream.class)}, the type
 * should have been promoted.
 *
 * @throws DataStoreException if an error occurred while using the storage connector.
 * @throws IOException if an error occurred while reading the test file.
 */
@Test
public void testGetAsChannelDataInput() throws DataStoreException, IOException {
    final StorageConnector connection = create(true);
    final ChannelDataInput input = connection.getStorageAs(ChannelDataInput.class);
    assertFalse(input instanceof ChannelImageInputStream);
    assertEquals(MAGIC_NUMBER, input.buffer.getInt());
    /*
         * Get as an image input stream and ensure that the cached value has been replaced.
         */
    final DataInput stream = connection.getStorageAs(DataInput.class);
    assertInstanceOf("Needs the SIS implementation", ChannelImageInputStream.class, stream);
    assertNotSame("Expected a new instance.", input, stream);
    assertSame("Shall share the channel.", input.channel, ((ChannelDataInput) stream).channel);
    assertSame("Shall share the buffer.", input.buffer, ((ChannelDataInput) stream).buffer);
    assertSame("Cached valud shall have been replaced.", stream, connection.getStorageAs(ChannelDataInput.class));
    connection.closeAllExcept(null);
}
Also used : DataInput(java.io.DataInput) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput) ChannelImageInputStream(org.apache.sis.internal.storage.io.ChannelImageInputStream) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput) Test(org.junit.Test)

Aggregations

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