From ec99ede801835021a38961e323fc412e64869e76 Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Fri, 8 Mar 2024 12:06:39 +0100
Subject: [PATCH] - replaced  IOUtil.copy with own copy routine using a buffer
 - fixed getBlockSize() to parse BLOCK_SIZE once only

---
 PLUGIN-INF/metadata_SLUBStoragePlugin.xml     |  4 +-
 .../plugin/storage/nfs/SLUBStoragePlugin.java | 47 ++++++++++++-------
 2 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/PLUGIN-INF/metadata_SLUBStoragePlugin.xml b/PLUGIN-INF/metadata_SLUBStoragePlugin.xml
index e8e6953..d4e91cf 100644
--- a/PLUGIN-INF/metadata_SLUBStoragePlugin.xml
+++ b/PLUGIN-INF/metadata_SLUBStoragePlugin.xml
@@ -26,7 +26,7 @@
 					<label>ui.storage.nfs.fileBlockSize</label>
 					<ui_tool_tip>BlockSize in Bytes</ui_tool_tip>
 					<single>false</single>
-					<default_value></default_value>
+					<default_value>1048576</default_value>
 					<mandatory>true</mandatory>
 					<validator_class_name>com.exlibris.core.infra.web.utils.formBuilder.validator.NumericFormValidator</validator_class_name>
 					<x_logic_type>String</x_logic_type>
@@ -70,7 +70,7 @@
 		</fr:x_form>
 	</pl:initParameters>
 	<pl:description>SLUB Storage Plugin</pl:description>
-	<pl:version>2.92</pl:version>
+	<pl:version>2.93</pl:version>
 	<pl:materialType>DIGITAL</pl:materialType>
 	<pl:module>Repository</pl:module>
 	<pl:generalType>TASK</pl:generalType>
diff --git a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
index 65d7772..30acf8a 100644
--- a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
+++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
@@ -21,7 +21,6 @@ limitations under the License.
 package org.slub.rosetta.dps.repository.plugin.storage.nfs;
 
 import com.exlibris.core.infra.common.exceptions.logging.ExLogger;
-import com.exlibris.core.infra.common.util.IOUtil;
 import com.exlibris.core.sdk.storage.containers.StoredEntityMetaData;
 import com.exlibris.core.sdk.storage.handler.AbstractStorageHandler;
 import com.exlibris.core.sdk.storage.handler.StorageUtil;
@@ -78,6 +77,7 @@ import java.util.Optional;
  */
 public class SLUBStoragePlugin extends AbstractStorageHandler {
     private static final ExLogger log = ExLogger.getExLogger(SLUBStoragePlugin.class);
+    private int blocksize = 0;
 
     private String getDirRoot() {
         String path = parameters.get("DIR_ROOT");
@@ -112,18 +112,23 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
 
 
     private int getBlockSize () {
-        int blockSize = 32*1024;
-        String blocksizeStr = this.parameters.get("BLOCK_SIZE");
-        if (null != blocksizeStr) {
-            try {
-                blockSize = Integer.parseInt(blocksizeStr);
-            } catch (NumberFormatException e) {
-                log.error("Could not convert BLOCK_SIZE string to int, " + e.getMessage());
+        if (0 == this.blocksize) {
+            String blocksizeStr = this.parameters.get("BLOCK_SIZE");
+            if (null != blocksizeStr) {
+                try {
+                    this.blocksize = Integer.parseInt(blocksizeStr);
+                } catch (NumberFormatException e) {
+                    log.error("Could not convert BLOCK_SIZE string to int, " + e.getMessage());
+                }
+                if (this.blocksize <= 0) {
+                    log.error("BLOCK_SIZE needs to be greater than 0!");
+                }
+            } else {
+                this.blocksize = 1024*1024;
+                log.info("Could not retrieve BLOCK_SIZE, using default of " + this.blocksize + " Bytes") ;
             }
-        } else {
-            log.error("Could not retrieve BLOCK_SIZE");
         }
-        return blockSize;
+        return this.blocksize;
     }
     private String getFilesHandlingMethod () {
         String filehandlingMethod = this.parameters.get("FILES_HANDLING_METHOD");
@@ -165,6 +170,15 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         return "(unknown) Bytes/s";
     }
 
+    private void copyStreamFast(InputStream is, OutputStream os) throws IOException{
+        int blocksize = getBlockSize();
+        assert(blocksize > 0);
+        byte[] buffer = new byte[ blocksize ];
+        int n;
+        while ((n = is.read(buffer)) != -1) {
+            os.write(buffer,0,n);
+        }
+    }
 
 
     /**
@@ -520,11 +534,10 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
                 }
                 try (OutputStream output = java.nio.file.Files.newOutputStream( Paths.get( absoluteDestFilePath.filestring())))
                 {
-                    var blocksize = getBlockSize();
                     long starttime = System.currentTimeMillis();
-                    IOUtil.copy(is, output, blocksize);
+                    copyStreamFast(is, output);
                     long endtime = System.currentTimeMillis();
-                    log.info("SLUBStoragePlugin.storeEntity() try copy (IOUtil.copy(is, '"+absoluteDestFilePath.filestring()+"', "+blocksize+") was successfull (" + throughput(starttime, endtime, absoluteDestFilePath)+ ")" );
+                    log.info("SLUBStoragePlugin.storeEntity() try copy (copyStreamFast(is, '"+absoluteDestFilePath.filestring()+"') was successfull (" + throughput(starttime, endtime, absoluteDestFilePath)+ ")" );
                 }
             }
             if (!checkFixityAbs(storedEntityMetadata.getFixities(), absoluteDestFilePath)) {
@@ -567,9 +580,9 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         } else {
             InputStream input = FileTransferUtil.getUrlContent(source);
             FileOutputStream output = new FileOutputStream(destination.filestring());
-            IOUtil.copy(input, output, getBlockSize());
-            IOUtil.shutdownStream(input);
-            IOUtil.shutdownStream(output);
+            copyStreamFast(input, output);
+            input.close();
+            output.close();
         }
         long endtime = System.currentTimeMillis();
         log.info("SLUBStoragePlugin.copyFile (" + source + ", " + destination.filestring() + ") was successfull (" + throughput(starttime, endtime, destination) + ")");
-- 
GitLab