Download file with resume
authorszlenkj <jakub.szlenk@contractors.roche.com>
Fri, 26 Jan 2024 07:30:33 +0000 (08:30 +0100)
committerszlenkj <jakub.szlenk@contractors.roche.com>
Tue, 6 Feb 2024 14:12:30 +0000 (15:12 +0100)
Arvados-DCO-1.1-Signed-off-by: Jakub Szlenk jakubszlenk@gmail.com

sdk/java-v2/src/main/java/org/arvados/client/api/client/KeepWebApiClient.java
sdk/java-v2/src/main/java/org/arvados/client/logic/keep/FileDownloader.java
sdk/java-v2/src/test/java/org/arvados/client/api/client/KeepWebApiClientTest.java
sdk/java-v2/src/test/java/org/arvados/client/logic/keep/FileDownloaderTest.java

index 2595956074edb3f8ddf280b38d90f4faeb07873e..ad37dad2bbda5e88296c52d5905701d4bd34cbff 100644 (file)
@@ -34,24 +34,25 @@ public class KeepWebApiClient extends BaseApiClient {
         return newFileCall(request);
     }
 
-    public byte[] downloadPartial(String collectionUuid, String filePathName, long offset) throws IOException {
+    public InputStream get(String collectionUuid, String filePathName, long start, Long end) throws IOException {
         Request.Builder builder = this.getRequestBuilder();
-        if (offset > 0) {
-            builder.addHeader("Range", "bytes=" + offset + "-");
+        String rangeValue = "bytes=" + start + "-";
+        if (end != null) {
+            rangeValue += end;
         }
+        builder.addHeader("Range", rangeValue);
         Request request = builder.url(this.getUrlBuilder(collectionUuid, filePathName).build()).get().build();
-        try (Response response = client.newCall(request).execute()) {
-            if (!response.isSuccessful()) {
-                throw new IOException("Failed to download file: " + response);
-            }
-            try (ResponseBody body = response.body()) {
-                if (body != null) {
-                    return body.bytes();
-                } else {
-                    throw new IOException("Response body is null for request: " + request);
-                }
-            }
+        Response response = client.newCall(request).execute();
+        if (!response.isSuccessful()) {
+            response.close();
+            throw new IOException("Failed to download file: " + response);
         }
+        ResponseBody body = response.body();
+        if (body == null) {
+            response.close();
+            throw new IOException("Response body is null for request: " + request);
+        }
+        return body.byteStream();
     }
 
     public String delete(String collectionUuid, String filePathName) {
index 3fe4e6433a2d357df3a55335fd5082d229e02b7d..5bfcabc10984bdb55a20bf130a2be0c88d819254 100644 (file)
@@ -20,7 +20,6 @@ import org.arvados.client.logic.keep.exception.DownloadFolderAlreadyExistsExcept
 import org.arvados.client.logic.keep.exception.FileAlreadyExistsException;
 import org.slf4j.Logger;
 
-import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -73,9 +72,9 @@ public class FileDownloader {
         return downloadedFile;
     }
 
-    public File downloadFileWithResume(String collectionUuid, String fileName, String pathToDownloadFolder, long offset, int bufferSize) throws IOException {
-        if (bufferSize <= 0) {
-            throw new IllegalArgumentException("Buffer size must be greater than 0");
+    public File downloadFileWithResume(String collectionUuid, String fileName, String pathToDownloadFolder, long start, Long end) throws IOException {
+        if (end != null && end < start) {
+            throw new IllegalArgumentException("End index must be greater than or equal to the start index");
         }
 
         File destinationFile = new File(pathToDownloadFolder, fileName);
@@ -87,14 +86,17 @@ public class FileDownloader {
             }
         }
 
-        try (RandomAccessFile outputFile = new RandomAccessFile(destinationFile, "rw")) {
-            outputFile.seek(offset);
+        try (RandomAccessFile outputFile = new RandomAccessFile(destinationFile, "rw");
+             InputStream inputStream = keepWebApiClient.get(collectionUuid, fileName, start, end)) {
+            outputFile.seek(start);
 
-            byte[] buffer = new byte[bufferSize];
+            long remaining = (end == null) ? Long.MAX_VALUE : end - start + 1;
+            byte[] buffer = new byte[4096];
             int bytesRead;
-            InputStream inputStream = new ByteArrayInputStream(keepWebApiClient.downloadPartial(collectionUuid, fileName, offset));
-            while ((bytesRead = inputStream.read(buffer)) != -1) {
-                outputFile.write(buffer, 0, bytesRead);
+            while ((bytesRead = inputStream.read(buffer)) != -1 && remaining > 0) {
+                int bytesToWrite = (int) Math.min(bytesRead, remaining);
+                outputFile.write(buffer, 0, bytesToWrite);
+                remaining -= bytesToWrite;
             }
         }
 
index e796c690321c846060ceccbd4278d986516c70d1..9b6b4fa17fe094f55935004414bcf9ddbb5d75d7 100644 (file)
@@ -10,7 +10,10 @@ package org.arvados.client.api.client;
 import org.arvados.client.test.utils.ArvadosClientMockedWebServerTest;
 import org.junit.Test;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.file.Files;
 
 import okhttp3.mockwebserver.MockResponse;
@@ -18,7 +21,7 @@ import okio.Buffer;
 
 import static org.arvados.client.test.utils.ApiClientTestUtils.getResponse;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertNotNull;
 
 public class KeepWebApiClientTest extends ArvadosClientMockedWebServerTest {
@@ -46,7 +49,8 @@ public class KeepWebApiClientTest extends ArvadosClientMockedWebServerTest {
         // given
         String collectionUuid = "some-collection-uuid";
         String filePathName = "sample-file-path";
-        long offset = 1024;
+        long start = 1024;
+        Long end = null;
 
         byte[] expectedData = "test data".getBytes();
 
@@ -54,12 +58,24 @@ public class KeepWebApiClientTest extends ArvadosClientMockedWebServerTest {
             server.enqueue(new MockResponse().setBody(buffer));
 
             // when
-            byte[] actualData = client.downloadPartial(collectionUuid, filePathName, offset);
+            InputStream inputStream = client.get(collectionUuid, filePathName, start, end);
+            byte[] actualData = inputStreamToByteArray(inputStream);
 
             // then
             assertNotNull(actualData);
-            assertEquals(new String(expectedData), new String(actualData));
+            assertArrayEquals(expectedData, actualData);
         }
     }
 
+    private byte[] inputStreamToByteArray(InputStream inputStream) throws IOException {
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        int nRead;
+        byte[] data = new byte[1024];
+        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
+            buffer.write(data, 0, nRead);
+        }
+        buffer.flush();
+        return buffer.toByteArray();
+    }
+
 }
index 0fb1f0206c5afad8aa6717e193568fc25a1453ea..2f2f81294cf163dfef85b2b5afbe8954bf6e1a5e 100644 (file)
@@ -27,8 +27,12 @@ import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -36,6 +40,7 @@ import java.util.UUID;
 
 import static org.arvados.client.test.utils.FileTestUtils.*;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertArrayEquals;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -90,7 +95,7 @@ public class FileDownloaderTest {
 
         for(int i = 0; i < downloadedFiles.size(); i ++) {
             File downloaded = new File(collectionDir + Characters.SLASH + files.get(i).getName());
-            Assert.assertArrayEquals(FileUtils.readFileToByteArray(downloaded), FileUtils.readFileToByteArray(files.get(i)));
+            assertArrayEquals(FileUtils.readFileToByteArray(downloaded), FileUtils.readFileToByteArray(files.get(i)));
         }
     }
 
@@ -110,7 +115,36 @@ public class FileDownloaderTest {
         //then
         Assert.assertTrue(downloadedFile.exists());
         Assert.assertEquals(file.getName(), downloadedFile.getName());
-        Assert.assertArrayEquals(FileUtils.readFileToByteArray(downloadedFile), FileUtils.readFileToByteArray(file));
+        assertArrayEquals(FileUtils.readFileToByteArray(downloadedFile), FileUtils.readFileToByteArray(file));
+    }
+
+    @Test
+    public void testDownloadFileWithResume() throws Exception {
+        //given
+        String collectionUuid = "some-collection-uuid";
+        String fileName = "sample-file-name";
+        String pathToDownloadFolder = "downloads";
+        long start = 1024;
+        Long end = null;
+
+        byte[] expectedData = "test data".getBytes();
+        InputStream inputStream = new ByteArrayInputStream(expectedData);
+
+        when(keepWebApiClient.get(collectionUuid, fileName, start, end)).thenReturn(inputStream);
+
+        //when
+        File downloadedFile = fileDownloader.downloadFileWithResume(collectionUuid, fileName, pathToDownloadFolder, start, end);
+
+        //then
+        Assert.assertNotNull(downloadedFile);
+        Assert.assertTrue(downloadedFile.exists());
+        Assert.assertEquals(downloadedFile.length(), expectedData.length);
+
+        byte[] actualData = Files.readAllBytes(downloadedFile.toPath());
+        assertArrayEquals(expectedData, actualData);
+
+        Files.delete(downloadedFile.toPath());
+        Files.delete(Paths.get(pathToDownloadFolder));
     }
 
     @After