From f5959af2e154134cd7767c43e139578f79b0618f Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Tue, 2 Feb 2021 14:58:39 +0100 Subject: [PATCH] New stream SPIs now behave more like the built-in SPIs. --- .../stream/BufferedFileImageInputStream.java | 3 +-- .../BufferedFileImageInputStreamSpi.java | 21 ++++++++++++------- .../BufferedRAFImageInputStreamSpi.java | 8 +++---- .../BufferedFileImageInputStreamSpiTest.java | 13 ++++++++++++ .../stream/ImageInputStreamSpiTest.java | 4 ++-- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStream.java b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStream.java index 0017b7fd..50106903 100644 --- a/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStream.java +++ b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStream.java @@ -76,9 +76,8 @@ public final class BufferedFileImageInputStream extends ImageInputStreamImpl { * @throws IllegalArgumentException if file is null. * @throws FileNotFoundException if file is a directory or cannot be opened for reading * for any reason. - * @throws IOException if an I/O error occurs. */ - public BufferedFileImageInputStream(final File file) throws IOException { + public BufferedFileImageInputStream(final File file) throws FileNotFoundException { this(new RandomAccessFile(notNull(file, "file"), "r")); } diff --git a/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStreamSpi.java b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStreamSpi.java index 9574ae71..edc3b169 100644 --- a/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStreamSpi.java +++ b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStreamSpi.java @@ -36,7 +36,7 @@ import javax.imageio.spi.ImageInputStreamSpi; import javax.imageio.spi.ServiceRegistry; import javax.imageio.stream.ImageInputStream; import java.io.File; -import java.io.IOException; +import java.io.FileNotFoundException; import java.util.Iterator; import java.util.Locale; @@ -69,14 +69,21 @@ public class BufferedFileImageInputStreamSpi extends ImageInputStreamSpi { } } - public ImageInputStream createInputStreamInstance(final Object input, final boolean pUseCache, final File pCacheDir) throws IOException { + public ImageInputStream createInputStreamInstance(final Object input, final boolean pUseCache, final File pCacheDir) { if (input instanceof File) { - File file = (File) input; - return new BufferedFileImageInputStream(file); - } - else { - throw new IllegalArgumentException("Expected input of type URL: " + input); + try { + return new BufferedFileImageInputStream((File) input); + } + catch (FileNotFoundException e) { + // For consistency with the JRE bundled SPIs, we'll return null here, + // even though the spec does not say that's allowed. + // The problem is that the SPIs can only declare that they support an input type like a File, + // instead they should be allowed to inspect the instance, to see that the file does exist... + return null; + } } + + throw new IllegalArgumentException("Expected input of type File: " + input); } @Override diff --git a/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedRAFImageInputStreamSpi.java b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedRAFImageInputStreamSpi.java index bb5e3fef..69bac835 100644 --- a/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedRAFImageInputStreamSpi.java +++ b/imageio/imageio-core/src/main/java/com/twelvemonkeys/imageio/stream/BufferedRAFImageInputStreamSpi.java @@ -71,12 +71,10 @@ public class BufferedRAFImageInputStreamSpi extends ImageInputStreamSpi { public ImageInputStream createInputStreamInstance(final Object input, final boolean pUseCache, final File pCacheDir) { if (input instanceof RandomAccessFile) { - RandomAccessFile file = (RandomAccessFile) input; - return new BufferedFileImageInputStream(file); - } - else { - throw new IllegalArgumentException("Expected input of type URL: " + input); + return new BufferedFileImageInputStream((RandomAccessFile) input); } + + throw new IllegalArgumentException("Expected input of type RandomAccessFile: " + input); } @Override diff --git a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStreamSpiTest.java b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStreamSpiTest.java index 5622fd2f..9c2a2b18 100644 --- a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStreamSpiTest.java +++ b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/BufferedFileImageInputStreamSpiTest.java @@ -1,9 +1,14 @@ package com.twelvemonkeys.imageio.stream; +import org.junit.Test; + import javax.imageio.spi.ImageInputStreamSpi; import java.io.File; import java.io.IOException; +import static org.junit.Assert.assertNull; +import static org.junit.Assume.assumeFalse; + public class BufferedFileImageInputStreamSpiTest extends ImageInputStreamSpiTest { @Override protected ImageInputStreamSpi createProvider() { @@ -14,4 +19,12 @@ public class BufferedFileImageInputStreamSpiTest extends ImageInputStreamSpiTest protected File createInput() throws IOException { return File.createTempFile("test-", ".tst"); } + + @Test + public void testReturnNullWhenFileDoesNotExist() throws IOException { + // This is really stupid behavior, but it is consistent with the JRE bundled SPIs. + File input = new File("a-file-that-should-not-exist-ever.fnf"); + assumeFalse("File should not exist: " + input.getPath(), input.exists()); + assertNull(provider.createInputStreamInstance(input)); + } } \ No newline at end of file diff --git a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/ImageInputStreamSpiTest.java b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/ImageInputStreamSpiTest.java index cbc69648..00e9841c 100644 --- a/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/ImageInputStreamSpiTest.java +++ b/imageio/imageio-core/src/test/java/com/twelvemonkeys/imageio/stream/ImageInputStreamSpiTest.java @@ -11,10 +11,10 @@ import java.util.Locale; import static org.junit.Assert.*; abstract class ImageInputStreamSpiTest { - private final ImageInputStreamSpi provider = createProvider(); + protected final ImageInputStreamSpi provider = createProvider(); @SuppressWarnings("unchecked") - private final Class inputClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; + protected final Class inputClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; protected abstract ImageInputStreamSpi createProvider();