From ec4f615edf10c8b32e23291e452971209206a339 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Tue, 7 Jul 2015 09:41:39 +0000
Subject: [PATCH] SSDM-2017: bug fixed in loadUnchangedData(). Readers of
 ReaderAndFileHandler are replaced if needed. Old readers are explicitly
 closed (not waiting for gc).

SVN: 34327
---
 .../dss/generic/shared/utils/ImageUtil.java   | 39 +++++++++++++++++--
 1 file changed, 35 insertions(+), 4 deletions(-)

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 07362f3e707..6cb762ffc03 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
-- 
GitLab