Skip to content
Snippets Groups Projects
Commit ba6cd00d authored by kaloyane's avatar kaloyane
Browse files

[LMS-2446] image readers must not keep opened file handles

SVN: 22342
parent 4dc86609
No related branches found
No related tags found
No related merge requests found
......@@ -57,7 +57,14 @@ public abstract class AbstractImageReader implements IImageReader
public final List<ImageID> getImageIDs(File file) throws IOExceptionUnchecked
{
return getImageIDs(new RandomAccessFileImpl(file, "r"));
RandomAccessFileImpl raf = new RandomAccessFileImpl(file, "r");
try
{
return getImageIDs(raf);
} finally
{
raf.close();
}
}
public final List<ImageID> getImageIDs(byte[] bytes)
......@@ -73,7 +80,13 @@ public abstract class AbstractImageReader implements IImageReader
public BufferedImage readImage(File file, ImageID imageID, IReadParams params) throws IOExceptionUnchecked
{
IRandomAccessFile raf = new RandomAccessFileImpl(file, "r");
return readImage(raf, imageID, params);
try
{
return readImage(raf, imageID, params);
} finally
{
raf.close();
}
}
public BufferedImage readImage(byte[] bytes, ImageID imageID, IReadParams params)
......
......@@ -41,7 +41,13 @@ public abstract class AbstractMetaDataAwareImageReader extends AbstractImageRead
throws IOExceptionUnchecked
{
IRandomAccessFile raf = new RandomAccessFileImpl(file, "r");
return readMetaData(raf, imageID, params);
try
{
return readMetaData(raf, imageID, params);
} finally
{
raf.close();
}
}
@Override
......
......@@ -61,16 +61,37 @@ final class BioFormatsImageUtils
*/
public static IFormatReader tryToCreateReaderForFile(String fileName)
{
for (IFormatReader r : READERS)
for (IFormatReader reader : READERS)
{
if (r.isThisType(fileName))
try
{
return createReader(r.getClass());
if (reader.isThisType(fileName))
{
return createReader(reader.getClass());
}
} finally
{
// "r.isThisType(fileName)" line can open a file handle,
// so we need to close it
closeOpenedFiles(reader);
}
}
return null;
}
private static void closeOpenedFiles(IFormatReader r)
{
try
{
r.close(true);
} catch (IOException ex)
{
throw new IOExceptionUnchecked(ex);
}
}
/**
* Tries to create an {@link IFormatReader} for a specified name. This is a factory method which
* returns for each invocation a new instance of the requested reader. May return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment