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) {
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;
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);
}
}
- 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;
}
}
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;
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 {
// 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();
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();
+ }
+
}
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;
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)
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)));
}
}
//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