From c1983ffda5c362ff4576c2a1181d442f8b80c4e1 Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Mon, 13 Feb 2023 12:14:41 +0100
Subject: [PATCH] - minor improvements

---
 .../plugin/SLUBVirusCheckClamAVPlugin.java    | 35 ++++++++++---------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/java/org/slub/rosetta/dps/repository/plugin/SLUBVirusCheckClamAVPlugin.java b/java/org/slub/rosetta/dps/repository/plugin/SLUBVirusCheckClamAVPlugin.java
index d643391..ef0e408 100644
--- a/java/org/slub/rosetta/dps/repository/plugin/SLUBVirusCheckClamAVPlugin.java
+++ b/java/org/slub/rosetta/dps/repository/plugin/SLUBVirusCheckClamAVPlugin.java
@@ -42,16 +42,16 @@ import java.util.Map;
  * ClamScan, should use clamdscan variant to avoid initialization overhead
  * <p/>
  * clamd-client opens a TCP-connection, see p18 in clamdoc.pdf
- * or source at https://github.com/vrtadmin/clamav-devel/blob/master/clamdscan/client.c
- * or source at https://github.com/vrtadmin/clamav-devel/blob/master/clamdscan/proto.c
- * code could also be copied from https://code.google.com/p/clamavj/source/browse/trunk/src/main/java/com/philvarner/clamavj/ClamScan.java?r=2
+ * or source at <a href="https://github.com/vrtadmin/clamav-devel/blob/master/clamdscan/client.c">...</a<a href=">
+ ">* or source at https://github.com/vrtadmin/clamav-devel/blob/master</a>/clamdscan/proto.c
+ <a href="*">code could also be copied from https://code.google.com/p/clamavj/source/browse/trunk/src/main/java/com/phi</a>lvarner/clamavj/ClamScan.java?r=2
  *
  * @author andreas.romeyke@slub-dresden.de (Andreas Romeyke)
- * @see com.exlibris.dps.repository.plugin.virusChcek.VirusCheckPluginV2
+ * @see VirusCheckPluginV2
  */
 public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 {
     //private static final ExLogger log = ExLogger.getExLogger(SLUBVirusCheckClamAVPlugin.class);
-    private static final int DEFAULT_CHUNK_SIZE = 4096;
+    private static final int DEFAULT_CHUNK_SIZE = 8192;
     private static final byte[] INSTREAM = "zINSTREAM\0".getBytes();
     private static final byte[] VERSION = "zVERSION\0".getBytes();
     private static final String RESPONSEOK = "stream: OK";
@@ -67,8 +67,8 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 {
      * @return 1 if last scan found a virus              -> FAILED
      * @return >1 if last scan result is undetermined    -> UNDETERMINED
      */
-    private enum Status {PASSED, FAILED, UNDETERMINED}; /* order is important, because we use .ordinal() in return code */
-    private Status status = Status.UNDETERMINED;
+    private enum Status {PASSED, FAILED, UNDETERMINED} /* order is important, because we use .ordinal() in return code */
+    private Status status = Status.FAILED;
     private String signature = "";
     /** constructor */
     public SLUBVirusCheckClamAVPlugin() {
@@ -89,9 +89,8 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 {
      * @param args list of files which should be scanned
      */
     public static void main(String[] args) {
-
         SLUBVirusCheckClamAVPlugin plugin = new SLUBVirusCheckClamAVPlugin();
-        Map<String, String> initp = new HashMap<String, String>();
+        Map<String, String> initp = new HashMap<>();
         initp.put( "host", "127.0.0.1");
         initp.put( "port", "3310");
         initp.put( "timeout", "60000");
@@ -217,9 +216,10 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 {
         }
     }
 
+
     private String read_socket_to_buffer(Socket socket, byte[] buffer) throws IOException {
         int read = socket.getInputStream().read(buffer);
-        String response = "";
+        String response="";
         if (read > 0) response = new String(buffer, 0, read);
         return response;
     }
@@ -232,7 +232,7 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 {
      */
     private String callSocketCommand_Version(Socket socket) throws IOException {
         DataOutputStream dos = null;
-        String response = "";
+        String response;
         try {
             dos = new DataOutputStream(socket.getOutputStream());
             dos.write(SLUBVirusCheckClamAVPlugin.VERSION);
@@ -256,7 +256,7 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 {
      */
     private String callSocketCommand_Stream(Socket socket, InputStream in) throws IOException {
         DataOutputStream dos = null;
-        String response = "";
+        String response;
         try {
             dos = new DataOutputStream(socket.getOutputStream());
             dos.write(SLUBVirusCheckClamAVPlugin.INSTREAM);
@@ -278,10 +278,11 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 {
         setStatus(Status.UNDETERMINED); /* default */
         try {
             Socket socket = openSocket();
-            InputStream in = new FileInputStream(fileFullPath);
-            // send stream
-            String result = callSocketCommand_Stream(socket, in);
-            in.close();
+            String result;
+            try (FileInputStream in = new FileInputStream(fileFullPath)) {
+                // send stream
+                result = callSocketCommand_Stream(socket, in);
+            }
             //log.debug( "Response: " + result);
             //System.out.println("Response: " + result);
             // parse return code
@@ -326,7 +327,7 @@ public class SLUBVirusCheckClamAVPlugin implements VirusCheckPluginV2 {
         } catch (IOException e) {
             log.error("exception creation socket in getAgent(), clamd not available at host=" + host + "port=" + port, e);
             //System.out.println("exception creation socket, clamd not available at host=" + host + "port=" + port + " " + e);
-            setStatus(Status.FAILED);
+            setStatus(Status.UNDETERMINED);
             setSignature("ERROR: clamd not available");
             return "ERROR: clamd not available";
         }
-- 
GitLab