]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/helper/AttachmentHelper.java
Close attachment input streams after upload
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / helper / AttachmentHelper.java
index 3005325a22cef68903d54ba5bbbb5c1a481b4882..b55153f061764a6cbbcc22eb6f696b580d41e0a9 100644 (file)
@@ -44,14 +44,20 @@ public class AttachmentHelper {
     }
 
     public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
-        var attachmentStreams = createAttachmentStreams(attachments);
+        final var attachmentStreams = createAttachmentStreams(attachments);
 
-        // Upload attachments here, so we only upload once even for multiple recipients
-        var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
-        for (var attachmentStream : attachmentStreams) {
-            attachmentPointers.add(uploadAttachment(attachmentStream));
+        try {
+            // Upload attachments here, so we only upload once even for multiple recipients
+            final var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
+            for (final var attachmentStream : attachmentStreams) {
+                attachmentPointers.add(uploadAttachment(attachmentStream));
+            }
+            return attachmentPointers;
+        } finally {
+            for (final var attachmentStream : attachmentStreams) {
+                attachmentStream.close();
+            }
         }
-        return attachmentPointers;
     }
 
     private List<SignalServiceAttachmentStream> createAttachmentStreams(List<String> attachments) throws AttachmentInvalidException, IOException {
@@ -104,9 +110,7 @@ public class AttachmentHelper {
         retrieveAttachment(attachment, input -> IOUtils.copyStream(input, outputStream));
     }
 
-    public void retrieveAttachment(
-            SignalServiceAttachment attachment, AttachmentHandler consumer
-    ) throws IOException {
+    public void retrieveAttachment(SignalServiceAttachment attachment, AttachmentHandler consumer) throws IOException {
         if (attachment.isStream()) {
             var input = attachment.asStream().getInputStream();
             // don't close input stream here, it might be reused later (e.g. with contact sync messages ...)
@@ -131,7 +135,8 @@ public class AttachmentHelper {
     }
 
     private InputStream retrieveAttachmentAsStream(
-            SignalServiceAttachmentPointer pointer, File tmpFile
+            SignalServiceAttachmentPointer pointer,
+            File tmpFile
     ) throws IOException {
         try {
             return dependencies.getMessageReceiver()