diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtil.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtil.java index 07362f3e707e736b91ff83f6d9bc83c7dd63f7b6..6cb762ffc03c89f337f497f89f608d048e33373f 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtil.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtil.java @@ -45,6 +45,7 @@ import ar.com.hjg.pngj.ImageInfo; import ar.com.hjg.pngj.ImageLine; import ar.com.hjg.pngj.PngFilterType; import ar.com.hjg.pngj.PngWriter; + import ch.rinn.restrictions.Private; import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked; import ch.systemsx.cisd.base.io.IRandomAccessFile; @@ -481,6 +482,16 @@ public class ImageUtil private String filePath; + private String imageLibraryName; + + private String imageLibraryReaderName; + + public ReaderAndFileHandler(String imageLibraryName, String imageLibraryReaderName) + { + this.imageLibraryName = imageLibraryName; + this.imageLibraryReaderName = imageLibraryReaderName; + } + void setFileHandler(IHierarchicalContentNode contentNode) { String newFilePath = contentNode.getRelativePath(); @@ -491,11 +502,16 @@ public class ImageUtil filePath = newFilePath; handle = contentNode.getFileContent(); } + + void close() + { + closeQuietly(handle); + imageReader.close(); + } @Override protected void finalize() throws Throwable { - closeQuietly(handle); } } @@ -567,14 +583,23 @@ public class ImageUtil if (imageLibraryNameOrNull != null && imageLibraryReaderNameOrNull != null) { ReaderAndFileHandler reader = readerStore.get(); - if (reader == null) + if (reader == null || isSameReader(reader, imageLibraryNameOrNull, imageLibraryReaderNameOrNull) == false) { IImageReader imageReader = ImageReaderFactory.tryGetReader(imageLibraryNameOrNull, + imageLibraryReaderNameOrNull); + if (imageReader != null) + { + if (reader != null) + { + reader.close(); + } + reader = new ReaderAndFileHandler(imageLibraryNameOrNull, imageLibraryReaderNameOrNull); - if(imageReader != null) { - reader = new ReaderAndFileHandler(); reader.imageReader = imageReader; readerStore.set(reader); + } else + { + reader = null; } } if (reader != null) @@ -586,6 +611,12 @@ public class ImageUtil return loadUnchangedDataGuessingLibrary(contentNode, operation, imageID); } + protected static boolean isSameReader(ReaderAndFileHandler reader, String imageLibraryNameOrNull, String imageLibraryReaderNameOrNull) + { + return reader.imageLibraryName.equals(imageLibraryNameOrNull) + && reader.imageLibraryReaderName.equals(imageLibraryReaderNameOrNull); + } + /** * Loads the data specified by <var>imageID</var> from the image from the given * </var>handle</var>. Supported images formats are GIF, JPG, PNG, and TIFF. The input stream