From b2868031f26d95a536781725ff0fe80a994e8da1 Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Sun, 30 Jan 2011 22:31:49 +0000
Subject: [PATCH] add: method IContent.getReadOnlyRandomAccessFile() change:
 hand in IContent rather than InputStream to ImageUtil and ImageChannelUtil
 methods

SVN: 19648
---
 .../cisd/common/io/ByteArrayBasedContent.java |  9 ++++
 .../io/ContentProviderBasedContent.java       |  9 +++-
 .../cisd/common/io/FileBasedContent.java      | 15 ++++++
 .../ch/systemsx/cisd/common/io/IContent.java  |  9 +++-
 .../dss/generic/shared/utils/ImageUtil.java   | 27 ++++------
 .../generic/shared/utils/ImageUtilTest.java   | 53 +++++++++++++------
 .../server/images/ImageChannelsUtils.java     |  4 +-
 .../server/DssServiceRpcScreening.java        |  2 +-
 .../server/images/ImageChannelsUtilsTest.java |  2 +-
 .../server/DssServiceRpcScreeningTest.java    | 12 +++--
 10 files changed, 98 insertions(+), 44 deletions(-)

diff --git a/common/source/java/ch/systemsx/cisd/common/io/ByteArrayBasedContent.java b/common/source/java/ch/systemsx/cisd/common/io/ByteArrayBasedContent.java
index 5e9627f18e7..9ba45c46f3f 100644
--- a/common/source/java/ch/systemsx/cisd/common/io/ByteArrayBasedContent.java
+++ b/common/source/java/ch/systemsx/cisd/common/io/ByteArrayBasedContent.java
@@ -18,6 +18,10 @@ package ch.systemsx.cisd.common.io;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+import ch.systemsx.cisd.common.filesystem.ByteBufferRandomAccessFile;
+import ch.systemsx.cisd.common.filesystem.IRandomAccessFile;
 
 /**
  * Content based on an array of bytes.
@@ -69,4 +73,9 @@ public class ByteArrayBasedContent implements IContent
     {
         return new ByteArrayInputStream(byteArray);
     }
+
+    public IRandomAccessFile getReadOnlyRandomAccessFile()
+    {
+        return new ByteBufferRandomAccessFile(ByteBuffer.wrap(byteArray));
+    }
 }
diff --git a/common/source/java/ch/systemsx/cisd/common/io/ContentProviderBasedContent.java b/common/source/java/ch/systemsx/cisd/common/io/ContentProviderBasedContent.java
index 647e96f9937..8ff889b3de3 100644
--- a/common/source/java/ch/systemsx/cisd/common/io/ContentProviderBasedContent.java
+++ b/common/source/java/ch/systemsx/cisd/common/io/ContentProviderBasedContent.java
@@ -18,6 +18,8 @@ package ch.systemsx.cisd.common.io;
 
 import java.io.InputStream;
 
+import ch.systemsx.cisd.common.filesystem.IRandomAccessFile;
+
 /**
  * Content based on a {@link IContentProvider}.
  *
@@ -56,6 +58,11 @@ public class ContentProviderBasedContent implements IContent
         return getContent().getInputStream();
     }
     
+    public IRandomAccessFile getReadOnlyRandomAccessFile()
+    {
+        return getContent().getReadOnlyRandomAccessFile();
+    }
+    
     private IContent getContent()
     {
         if (content == null)
@@ -64,5 +71,5 @@ public class ContentProviderBasedContent implements IContent
         }
         return content;
     }
-    
+
 }
diff --git a/common/source/java/ch/systemsx/cisd/common/io/FileBasedContent.java b/common/source/java/ch/systemsx/cisd/common/io/FileBasedContent.java
index b76e4400843..4442ae357fb 100644
--- a/common/source/java/ch/systemsx/cisd/common/io/FileBasedContent.java
+++ b/common/source/java/ch/systemsx/cisd/common/io/FileBasedContent.java
@@ -20,9 +20,13 @@ import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.RandomAccessFile;
 
 import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
+import ch.systemsx.cisd.common.filesystem.IRandomAccessFile;
+import ch.systemsx.cisd.common.filesystem.RandomAccessFileImpl;
 
 /**
  * File content. Wraps an instance of {@link File}.
@@ -78,4 +82,15 @@ public class FileBasedContent implements IContent
             throw CheckedExceptionTunnel.wrapIfNecessary(ex);
         }
     }
+
+    public IRandomAccessFile getReadOnlyRandomAccessFile()
+    {
+        try
+        {
+            return new RandomAccessFileImpl(new RandomAccessFile(file, "r"));
+        } catch (IOException ex)
+        {
+            throw CheckedExceptionTunnel.wrapIfNecessary(ex);
+        }
+    }
 }
diff --git a/common/source/java/ch/systemsx/cisd/common/io/IContent.java b/common/source/java/ch/systemsx/cisd/common/io/IContent.java
index e2b6037273f..cf4b6b33045 100644
--- a/common/source/java/ch/systemsx/cisd/common/io/IContent.java
+++ b/common/source/java/ch/systemsx/cisd/common/io/IContent.java
@@ -18,8 +18,10 @@ package ch.systemsx.cisd.common.io;
 
 import java.io.InputStream;
 
+import ch.systemsx.cisd.common.filesystem.IRandomAccessFile;
+
 /**
- * Abstract of streamable binary content with name and known size.
+ * Abstract of streamable and random-accessible binary content with name and known size.
  * 
  * @author Franz-Josef Elmer
  */
@@ -40,6 +42,11 @@ public interface IContent
      */
     public boolean exists();
 
+    /**
+     * Returns a random access file object for read-only use.
+     */
+    public IRandomAccessFile getReadOnlyRandomAccessFile();
+    
     /**
      * Returns a new instance of an input stream over the complete content. Note that the returned
      * {@link InputStream} is expected to have {@link InputStream#markSupported()}
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 52521107b74..9a1f18c3cde 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
@@ -37,12 +37,13 @@ import javax.imageio.ImageIO;
 
 import org.apache.commons.io.IOUtils;
 
-import com.sun.media.jai.codec.FileSeekableStream;
 import com.sun.media.jai.codec.ImageCodec;
 import com.sun.media.jai.codec.ImageDecoder;
 
 import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
 import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
+import ch.systemsx.cisd.common.io.FileBasedContent;
+import ch.systemsx.cisd.common.io.IContent;
 import ch.systemsx.cisd.common.utilities.DataTypeUtil;
 
 /**
@@ -182,9 +183,9 @@ public class ImageUtil
      * @throws IllegalArgumentException if the input stream doesn't start with a magic number
      *             identifying supported image format.
      */
-    public static BufferedImage loadImage(InputStream inputStream)
+    public static BufferedImage loadImage(IContent content)
     {
-        return loadImage(inputStream, 0);
+        return loadImage(content, 0);
     }
 
     /**
@@ -195,12 +196,12 @@ public class ImageUtil
      * @throws IllegalArgumentException if the input stream doesn't start with a magic number
      *             identifying supported image format.
      */
-    public static BufferedImage loadImage(InputStream inputStream, int page)
+    public static BufferedImage loadImage(IContent content, int page)
     {
-        InputStream markSupportingInputStream = inputStream;
-        if (inputStream.markSupported() == false)
+        InputStream markSupportingInputStream = content.getInputStream();
+        if (markSupportingInputStream.markSupported() == false)
         {
-            markSupportingInputStream = new BufferedInputStream(inputStream);
+            markSupportingInputStream = new BufferedInputStream(markSupportingInputStream);
         }
         String fileType = DataTypeUtil.tryToFigureOutFileTypeOf(markSupportingInputStream);
         return loadImage(markSupportingInputStream, fileType, page);
@@ -214,7 +215,7 @@ public class ImageUtil
      * @throws IllegalArgumentException if the input stream doesn't start with a magic number
      *             identifying supported image format.
      */
-    public static BufferedImage loadImage(InputStream inputStream, String fileType, int page)
+    private static BufferedImage loadImage(InputStream inputStream, String fileType, int page)
     {
         try
         {
@@ -251,15 +252,7 @@ public class ImageUtil
         {
             throw new IllegalArgumentException("File does not exist: " + file.getAbsolutePath());
         }
-        try
-        {
-            FileSeekableStream inStream = new FileSeekableStream(file);
-            return loadImage(inStream);
-        } catch (IOException ex)
-        {
-            throw new IllegalArgumentException("Isn't a valid image file: "
-                    + file.getAbsolutePath() + ". Error: " + ex.getMessage());
-        }
+        return loadImage(new FileBasedContent(file));
     }
 
     /**
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtilTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtilTest.java
index 2d8e8ac588c..22b9bbcae4b 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtilTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtilTest.java
@@ -18,8 +18,6 @@ package ch.systemsx.cisd.openbis.dss.generic.shared.utils;
 
 import java.awt.image.BufferedImage;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -27,7 +25,9 @@ import org.testng.AssertJUnit;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
-import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
+import ch.systemsx.cisd.common.filesystem.IRandomAccessFile;
+import ch.systemsx.cisd.common.io.FileBasedContent;
+import ch.systemsx.cisd.common.io.IContent;
 
 /**
  * 
@@ -36,6 +36,37 @@ import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
  */
 public class ImageUtilTest extends AssertJUnit
 {
+    private static class MockIContent implements IContent
+    {
+        final MockInputStream is = new MockInputStream();
+        
+        public String tryGetName()
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public long getSize()
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean exists()
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public IRandomAccessFile getReadOnlyRandomAccessFile()
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public InputStream getInputStream()
+        {
+            return is;
+        }
+        
+    }
+    
     private static class MockInputStream extends InputStream
     {
         boolean closeInvoked;
@@ -100,17 +131,17 @@ public class ImageUtilTest extends AssertJUnit
     @Test
     public void testInputStreamAutomaticallyClosed()
     {
-        MockInputStream inputStream = new MockInputStream();
+        MockIContent content = new MockIContent();
         try
         {
-            ImageUtil.loadImage(inputStream);
+            ImageUtil.loadImage(content);
             fail("IllegalArgumentException expected");
         } catch (IllegalArgumentException ex)
         {
             assertEquals("File type of an image input stream couldn't be determined.", ex.getMessage());
         }
         
-        assertEquals(true, inputStream.closeInvoked);
+        assertEquals(true, content.is.closeInvoked);
     }
 
     private BufferedImage loadImageByFile(String fileName)
@@ -123,15 +154,7 @@ public class ImageUtilTest extends AssertJUnit
     private BufferedImage loadImageByInputStream(String fileName)
     {
         File file = new File(dir, fileName);
-        BufferedImage image;
-        try
-        {
-            image = ImageUtil.loadImage(new FileInputStream(file));
-        } catch (FileNotFoundException ex)
-        {
-            throw CheckedExceptionTunnel.wrapIfNecessary(ex);
-        }
-        return image;
+        return ImageUtil.loadImage(new FileBasedContent(file));
     }
     
     private void assertImageSize(int expectedWith, int expectedHeight, BufferedImage image)
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/images/ImageChannelsUtils.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/images/ImageChannelsUtils.java
index 4e4ab5dc897..6b22e8228d4 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/images/ImageChannelsUtils.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/generic/server/images/ImageChannelsUtils.java
@@ -24,7 +24,6 @@ import java.awt.image.RenderedImage;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -470,12 +469,11 @@ public class ImageChannelsUtils
     private static BufferedImage loadImage(AbsoluteImageReference imageReference)
     {
         IContent content = imageReference.getContent();
-        InputStream inputStream = content.getInputStream();
 
         // extracts the correct page if necessary
         int page = (imageReference.tryGetPage() != null) ? imageReference.tryGetPage() : 0;
 
-        BufferedImage image = ImageUtil.loadImage(inputStream, page);
+        BufferedImage image = ImageUtil.loadImage(content, page);
         return image;
     }
 
diff --git a/screening/source/java/ch/systemsx/cisd/openbis/dss/screening/server/DssServiceRpcScreening.java b/screening/source/java/ch/systemsx/cisd/openbis/dss/screening/server/DssServiceRpcScreening.java
index ed4cf6cb7ac..ad6ef6d49e6 100644
--- a/screening/source/java/ch/systemsx/cisd/openbis/dss/screening/server/DssServiceRpcScreening.java
+++ b/screening/source/java/ch/systemsx/cisd/openbis/dss/screening/server/DssServiceRpcScreening.java
@@ -219,7 +219,7 @@ public class DssServiceRpcScreening extends AbstractDssServiceRpc<IDssServiceRpc
             IImagingDatasetLoader imageAccessor)
     {
         IContent imageFile = getAnyImagePath(imageAccessor, dataset);
-        BufferedImage image = ImageUtil.loadImage(imageFile.getInputStream());
+        BufferedImage image = ImageUtil.loadImage(imageFile);
         Size imageSize = new Size(image.getWidth(), image.getHeight());
         return imageSize;
     }
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/images/ImageChannelsUtilsTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/images/ImageChannelsUtilsTest.java
index 00b25a1caac..db7cc1938fe 100644
--- a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/images/ImageChannelsUtilsTest.java
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/images/ImageChannelsUtilsTest.java
@@ -242,7 +242,7 @@ public class ImageChannelsUtilsTest extends AssertJUnit
 
     private String getImageContentDescription(IContent image)
     {
-        BufferedImage bufferedImage = ImageUtil.loadImage(image.getInputStream());
+        BufferedImage bufferedImage = ImageUtil.loadImage(image);
         return getImageContentDescription(bufferedImage);
     }
 
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/screening/server/DssServiceRpcScreeningTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/screening/server/DssServiceRpcScreeningTest.java
index 4abfe1dda2c..2269ffb0c83 100644
--- a/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/screening/server/DssServiceRpcScreeningTest.java
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/dss/screening/server/DssServiceRpcScreeningTest.java
@@ -17,7 +17,6 @@
 package ch.systemsx.cisd.openbis.dss.screening.server;
 
 import java.awt.image.BufferedImage;
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
@@ -42,6 +41,7 @@ import ch.systemsx.cisd.base.image.IImageTransformer;
 import ch.systemsx.cisd.base.image.IImageTransformerFactory;
 import ch.systemsx.cisd.base.mdarray.MDFloatArray;
 import ch.systemsx.cisd.bds.hcs.Location;
+import ch.systemsx.cisd.common.io.ByteArrayBasedContent;
 import ch.systemsx.cisd.common.io.ConcatenatedFileOutputStreamWriter;
 import ch.systemsx.cisd.common.io.FileBasedContent;
 import ch.systemsx.cisd.common.io.IContent;
@@ -333,7 +333,8 @@ public class DssServiceRpcScreeningTest extends AssertJUnit
     {
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         imagesWriter.writeNextBlock(outputStream);
-        return ImageUtil.loadImage(new ByteArrayInputStream(outputStream.toByteArray()));
+        return ImageUtil
+                .loadImage(new ByteArrayBasedContent(outputStream.toByteArray(), "UNKNOWN"));
     }
 
     @Test
@@ -448,14 +449,14 @@ public class DssServiceRpcScreeningTest extends AssertJUnit
                     ImgDatasetDTO dataset = createDataset(datasetId);
                     dataset.setContainerId(containerId);
                     dataset.setPermId(DATASET_CODE);
-                    
+
                     long experimentId = 888;
                     ImgContainerDTO container = new ImgContainerDTO(null, null, null, experimentId);
                     container.setId(containerId);
 
                     one(dao).tryGetDatasetByPermId(DATASET_CODE);
                     will(returnValue(dataset));
-                    
+
                     allowing(dao).hasDatasetChannels(DATASET_CODE);
                     will(returnValue(false));
 
@@ -601,7 +602,8 @@ public class DssServiceRpcScreeningTest extends AssertJUnit
 
     private FeatureVectorDatasetReference createFeatureVectorDatasetReference(String dataSetCode)
     {
-        return new FeatureVectorDatasetReference(dataSetCode, "", null, null, null, null, null, null);
+        return new FeatureVectorDatasetReference(dataSetCode, "", null, null, null, null, null,
+                null);
     }
 
     // Used for the authorization test
-- 
GitLab