mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-04 12:05:29 -04:00
New stream SPIs now behave more like the built-in SPIs.
This commit is contained in:
parent
ea74ac2714
commit
f5959af2e1
@ -76,9 +76,8 @@ public final class BufferedFileImageInputStream extends ImageInputStreamImpl {
|
|||||||
* @throws IllegalArgumentException if <code>file</code> is <code>null</code>.
|
* @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
|
* @throws FileNotFoundException if <code>file</code> is a directory or cannot be opened for reading
|
||||||
* for any reason.
|
* 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"));
|
this(new RandomAccessFile(notNull(file, "file"), "r"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ import javax.imageio.spi.ImageInputStreamSpi;
|
|||||||
import javax.imageio.spi.ServiceRegistry;
|
import javax.imageio.spi.ServiceRegistry;
|
||||||
import javax.imageio.stream.ImageInputStream;
|
import javax.imageio.stream.ImageInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Locale;
|
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) {
|
if (input instanceof File) {
|
||||||
File file = (File) input;
|
try {
|
||||||
return new BufferedFileImageInputStream(file);
|
return new BufferedFileImageInputStream((File) input);
|
||||||
}
|
}
|
||||||
else {
|
catch (FileNotFoundException e) {
|
||||||
throw new IllegalArgumentException("Expected input of type URL: " + input);
|
// 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
|
@Override
|
||||||
|
@ -71,12 +71,10 @@ public class BufferedRAFImageInputStreamSpi extends ImageInputStreamSpi {
|
|||||||
|
|
||||||
public ImageInputStream createInputStreamInstance(final Object input, final boolean pUseCache, final File pCacheDir) {
|
public ImageInputStream createInputStreamInstance(final Object input, final boolean pUseCache, final File pCacheDir) {
|
||||||
if (input instanceof RandomAccessFile) {
|
if (input instanceof RandomAccessFile) {
|
||||||
RandomAccessFile file = (RandomAccessFile) input;
|
return new BufferedFileImageInputStream((RandomAccessFile) input);
|
||||||
return new BufferedFileImageInputStream(file);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new IllegalArgumentException("Expected input of type URL: " + input);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Expected input of type RandomAccessFile: " + input);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
package com.twelvemonkeys.imageio.stream;
|
package com.twelvemonkeys.imageio.stream;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.imageio.spi.ImageInputStreamSpi;
|
import javax.imageio.spi.ImageInputStreamSpi;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assume.assumeFalse;
|
||||||
|
|
||||||
public class BufferedFileImageInputStreamSpiTest extends ImageInputStreamSpiTest<File> {
|
public class BufferedFileImageInputStreamSpiTest extends ImageInputStreamSpiTest<File> {
|
||||||
@Override
|
@Override
|
||||||
protected ImageInputStreamSpi createProvider() {
|
protected ImageInputStreamSpi createProvider() {
|
||||||
@ -14,4 +19,12 @@ public class BufferedFileImageInputStreamSpiTest extends ImageInputStreamSpiTest
|
|||||||
protected File createInput() throws IOException {
|
protected File createInput() throws IOException {
|
||||||
return File.createTempFile("test-", ".tst");
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
@ -11,10 +11,10 @@ import java.util.Locale;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
abstract class ImageInputStreamSpiTest<T> {
|
abstract class ImageInputStreamSpiTest<T> {
|
||||||
private final ImageInputStreamSpi provider = createProvider();
|
protected final ImageInputStreamSpi provider = createProvider();
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@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();
|
protected abstract ImageInputStreamSpi createProvider();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user