rishabhdaim commented on code in PR #2706:
URL: https://github.com/apache/jackrabbit-oak/pull/2706#discussion_r2739984920


##########
oak-blob-cloud/src/main/java/org/apache/jackrabbit/oak/blob/cloud/s3/S3Backend.java:
##########
@@ -521,28 +521,61 @@ public void addMetadataRecord(final InputStream input, 
final String name) throws
 
         // Executor required to handle reading from the InputStream on a 
separate thread so the main upload is not blocked.
         final ExecutorService executor = Executors.newSingleThreadExecutor();
+        File tempFile = null;
         try {
             
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
             final PutObjectRequest.Builder builder = PutObjectRequest.builder()
                     .bucket(bucket)
                     .contentType("application/octet-stream")
                     .key(addMetaKeyPrefix(name));
 
+            InputStream uploadStream = input;
+            final long length;
+
+            if (input instanceof FileInputStream) {
+                // if the file is modified after opening, the size may not 
reflect the latest changes
+                FileInputStream fis = (FileInputStream) input;
+                length = fis.getChannel().size();
+            } else if (input instanceof ByteArrayInputStream) {
+                length = input.available();
+            } else if (input.markSupported()) {
+                // in case the inputStream supports mark & reset
+                input.mark(Integer.MAX_VALUE);
+                length = IOUtils.consume(input);
+                input.reset();
+            } else {
+                // we have to read all the stream to get the actual length
+                // last else block: store to temp file and re-read
+                tempFile = File.createTempFile("s3backend-", ".tmp");
+                try (OutputStream out = 
Files.newOutputStream(tempFile.toPath())) {
+                    IOUtils.copy(input, out);
+                }
+                length = tempFile.length();
+                uploadStream = Files.newInputStream(tempFile.toPath());
+            }
+
             // Specify `null` for the content length when you don't know the 
content length.

Review Comment:
   addressed in 
https://github.com/apache/jackrabbit-oak/pull/2706/commits/893ff9f5fe2b365b3dcc18048bee34b427838226



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to