Download file with resume
[arvados.git] / sdk / java-v2 / src / main / java / org / arvados / client / api / client / KeepWebApiClient.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) {