@Test public void testCloseUnderlyingFile() throws IOException { final boolean[] closed = new boolean[1]; File file = createFileWithContent(new ByteArrayInputStream(makeRandomArray(256))); RandomAccessFile raf = new RandomAccessFile(file, "r") { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; FileSeekableStream stream = new FileSeekableStream(raf); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); } }
import com.sun.media.jai.codec.FileSeekableStream; import com.sun.media.jai.codec.ImageCodec; import com.sun.media.jai.codec.ImageDecoder; // This function is minimal, you should add exceptions and error handling public RenderedImage read(String filename) FileSeekableStream fss = new FileSeekableStream(filename); ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", fss, null); RenderedImage image = decoder.decodeAsRenderedImage() fss.close(); return image; }
public void testCloseUnderlyingFile() throws IOException { final boolean[] closed = new boolean[1]; File file = createFileWithContent(new ByteArrayInputStream(makeRandomArray(256))); RandomAccessFile raf = new RandomAccessFile(file, "r") { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; FileSeekableStream stream = new FileSeekableStream(raf); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); } }
@Test public void testCloseUnderlyingFile() throws IOException { final boolean[] closed = new boolean[1]; File file = createFileWithContent(new ByteArrayInputStream(makeRandomArray(256))); RandomAccessFile raf = new RandomAccessFile(file, "r") { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; FileSeekableStream stream = new FileSeekableStream(raf); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); } }