New stream SPIs now behave more like the built-in SPIs.

This commit is contained in:
Harald Kuhr 2021-02-02 14:58:39 +01:00
parent ea74ac2714
commit f5959af2e1
5 changed files with 33 additions and 16 deletions

View File

@ -76,9 +76,8 @@ public final class BufferedFileImageInputStream extends ImageInputStreamImpl {
* @throws IllegalArgumentException if <code>file</code> is <code>null</code>.
* @throws FileNotFoundException if <code>file</code> 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"));
}

View File

@ -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,16 +69,23 @@ 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);
try {
return new BufferedFileImageInputStream((File) input);
}
else {
throw new IllegalArgumentException("Expected input of type URL: " + 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
public boolean canUseCacheFile() {
return false;

View File

@ -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

View File

@ -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<File> {
@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));
}
}

View File

@ -11,10 +11,10 @@ import java.util.Locale;
import static org.junit.Assert.*;
abstract class ImageInputStreamSpiTest<T> {
private final ImageInputStreamSpi provider = createProvider();
protected final ImageInputStreamSpi provider = createProvider();
@SuppressWarnings("unchecked")
private final Class<T> inputClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
protected final Class<T> inputClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
protected abstract ImageInputStreamSpi createProvider();