Search in sources :

Example 1 with ChannelImageInputStream

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;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) 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) ChannelFactory(org.apache.sis.internal.storage.io.ChannelFactory) ByteBuffer(java.nio.ByteBuffer) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput)

Example 2 with ChannelImageInputStream

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

Example 3 with ChannelImageInputStream

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());
}
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 4 with ChannelImageInputStream

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

Example 5 with ChannelImageInputStream

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());
}
Also used : DataInput(java.io.DataInput) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ChannelImageInputStream(org.apache.sis.internal.storage.io.ChannelImageInputStream) ChannelDataInput(org.apache.sis.internal.storage.io.ChannelDataInput)

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