Download file with resume
[arvados.git] / sdk / java-v2 / src / main / java / org / arvados / client / api / client / KeepWebApiClient.java
index 2c3168649ff70b734eccb57b627a83967f3cf94e..2595956074edb3f8ddf280b38d90f4faeb07873e 100644 (file)
@@ -10,9 +10,13 @@ package org.arvados.client.api.client;
 import okhttp3.HttpUrl;
 import okhttp3.Request;
 import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+
 import org.arvados.client.config.ConfigProvider;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 
 public class KeepWebApiClient extends BaseApiClient {
@@ -30,6 +34,26 @@ public class KeepWebApiClient extends BaseApiClient {
         return newFileCall(request);
     }
 
+    public byte[] downloadPartial(String collectionUuid, String filePathName, long offset) throws IOException {
+        Request.Builder builder = this.getRequestBuilder();
+        if (offset > 0) {
+            builder.addHeader("Range", "bytes=" + offset + "-");
+        }
+        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);
+                }
+            }
+        }
+    }
+
     public String delete(String collectionUuid, String filePathName) {
         Request request = getRequestBuilder()
                 .url(getUrlBuilder(collectionUuid, filePathName).build())