SLING-8453 Add size of sent bytes to ScanResult
diff --git a/src/main/java/org/apache/sling/commons/clam/ScanResult.java b/src/main/java/org/apache/sling/commons/clam/ScanResult.java
index b2dbe58..27c9cad 100644
--- a/src/main/java/org/apache/sling/commons/clam/ScanResult.java
+++ b/src/main/java/org/apache/sling/commons/clam/ScanResult.java
@@ -32,10 +32,13 @@
 
     private final long started;
 
-    public ScanResult(@NotNull final Status status, @NotNull final String message, long started) {
+    private final long size;
+
+    public ScanResult(@NotNull final Status status, @NotNull final String message, long started, long size) {
         this.status = status;
         this.message = message;
         this.started = started;
+        this.size = size;
     }
 
     public long getTimestamp() {
@@ -56,6 +59,10 @@
         return started;
     }
 
+    public long getSize() {
+        return size;
+    }
+
     public boolean isOk() {
         return Status.OK.equals(status);
     }
diff --git a/src/main/java/org/apache/sling/commons/clam/internal/ClamdService.java b/src/main/java/org/apache/sling/commons/clam/internal/ClamdService.java
index f17f385..78cd189 100644
--- a/src/main/java/org/apache/sling/commons/clam/internal/ClamdService.java
+++ b/src/main/java/org/apache/sling/commons/clam/internal/ClamdService.java
@@ -96,13 +96,11 @@
     @Override
     @NotNull
     public ScanResult scan(@NotNull final InputStream inputStream) throws IOException {
-        final long started = System.currentTimeMillis();
         try {
-            final byte[] reply = doInstream(inputStream);
-            return parseClamdReply(reply, started);
+            return doInstream(inputStream);
         } catch (InstreamSizeLimitExceededException e) {
             logger.error("doing INSTREAM failed", e);
-            return new ScanResult(ScanResult.Status.ERROR, e.getMessage(), started);
+            return new ScanResult(ScanResult.Status.ERROR, e.getMessage(), e.getStarted(), e.getSize());
         }
     }
 
@@ -149,10 +147,11 @@
      * limit exceeded and close the connection.
      *
      * @param inputStream data sent to clamd in chunks
-     * @return reply from clamd
+     * @return scan result from clamd
      */
-    private byte[] doInstream(final InputStream inputStream) throws IOException, InstreamSizeLimitExceededException {
+    private ScanResult doInstream(final InputStream inputStream) throws IOException, InstreamSizeLimitExceededException {
         logger.info("connecting to clam daemon at {}:{} for scanning", configuration.clamd_host(), configuration.clamd_port());
+        final long started = System.currentTimeMillis();
         try (final Socket socket = new Socket(configuration.clamd_host(), configuration.clamd_port());
              final OutputStream out = new BufferedOutputStream(socket.getOutputStream());
              final InputStream in = socket.getInputStream()) {
@@ -179,7 +178,7 @@
                 if (in.available() > 0) {
                     logger.info("total bytes sent: {}", total);
                     final byte[] reply = IOUtils.toByteArray(in);
-                    throw new InstreamSizeLimitExceededException(reply);
+                    throw new InstreamSizeLimitExceededException(reply, started, total);
                 }
 
                 read = inputStream.read(data);
@@ -192,21 +191,22 @@
             out.flush();
 
             // return reply on complete
-            return IOUtils.toByteArray(in);
+            final byte[] reply = IOUtils.toByteArray(in);
+            return parseClamdReply(reply, started, total);
         }
     }
 
-    private ScanResult parseClamdReply(final byte[] reply, final long started) {
+    private ScanResult parseClamdReply(final byte[] reply, final long started, final long size) {
         final String message = new String(reply, StandardCharsets.US_ASCII).trim();
         logger.info("reply message from clam daemon: '{}'", message);
         if (message.matches(OK_REPLY_PATTERN)) {
-            return new ScanResult(Status.OK, message, started);
+            return new ScanResult(Status.OK, message, started, size);
         } else if (message.matches(FOUND_REPLY_PATTERN)) {
-            return new ScanResult(Status.FOUND, message, started);
+            return new ScanResult(Status.FOUND, message, started, size);
         } else if (message.matches(INSTREAM_SIZE_LIMIT_EXCEEDED_PATTERN)) {
-            return new ScanResult(Status.ERROR, message, started);
+            return new ScanResult(Status.ERROR, message, started, size);
         } else {
-            return new ScanResult(Status.UNKNOWN, message, started);
+            return new ScanResult(Status.UNKNOWN, message, started, size);
         }
     }
 
diff --git a/src/main/java/org/apache/sling/commons/clam/internal/InstreamSizeLimitExceededException.java b/src/main/java/org/apache/sling/commons/clam/internal/InstreamSizeLimitExceededException.java
index ec7aeef..afdf95f 100644
--- a/src/main/java/org/apache/sling/commons/clam/internal/InstreamSizeLimitExceededException.java
+++ b/src/main/java/org/apache/sling/commons/clam/internal/InstreamSizeLimitExceededException.java
@@ -22,8 +22,22 @@
 
 class InstreamSizeLimitExceededException extends Exception {
 
-    InstreamSizeLimitExceededException(final byte[] reply) {
+    private final long started;
+
+    private final long size;
+
+    InstreamSizeLimitExceededException(final byte[] reply, long started, long size) {
         super(new String(reply, StandardCharsets.US_ASCII).trim());
+        this.started = started;
+        this.size = size;
+    }
+
+    long getStarted() {
+        return started;
+    }
+
+    long getSize() {
+        return size;
     }
 
 }