Skip to content

Commit 0e2230b

Browse files
authored
fix: fix download for empty files (#1231)
1 parent 9a94a1a commit 0e2230b

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

src/intTest/java/com/box/sdk/BoxFileIT.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,36 @@ public void setsAndRetrievesDispositionAt() throws ParseException {
966966
}
967967
}
968968

969+
@Test
970+
public void uploadAndDownloadEmptyFileSucceeds() throws IOException {
971+
BoxAPIConnection api = jwtApiForServiceAccount();
972+
BoxFolder folder = getUniqueFolder(api);
973+
String fileName = "empty_file";
974+
URL fileURL = this.getClass().getResource("/sample-files/" + fileName);
975+
String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8");
976+
long fileSize = new File(filePath).length();
977+
byte[] fileContent = readAllBytes(filePath);
978+
BoxFile uploadedFile = null;
979+
try {
980+
InputStream uploadStream = Files.newInputStream(Paths.get(filePath));
981+
ProgressListener mockUploadListener = mock(ProgressListener.class);
982+
BoxFile.Info uploadedFileInfo = folder.uploadFile(
983+
uploadStream, BoxFileIT.generateString(), fileSize, mockUploadListener
984+
);
985+
uploadedFile = uploadedFileInfo.getResource();
986+
987+
ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
988+
uploadedFile.download(downloadStream);
989+
byte[] downloadedFileContent = downloadStream.toByteArray();
990+
991+
assertThat(downloadedFileContent, is(equalTo(fileContent)));
992+
assertThat(folder, hasItem(Matchers.<BoxItem.Info>hasProperty("ID", equalTo(uploadedFile.getID()))));
993+
} finally {
994+
deleteFile(uploadedFile);
995+
}
996+
997+
}
998+
969999
private byte[] readFileContent(String fileName) throws IOException {
9701000
URL fileURL = this.getClass().getResource("/sample-files/" + fileName);
9711001
String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8");

src/main/java/com/box/sdk/BoxAPIResponse.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,18 @@ static BoxAPIResponse toBoxResponse(Response response) {
148148
);
149149
}
150150
ResponseBody responseBody = response.body();
151-
if (responseBody.contentLength() == 0 || responseBody.contentType() == null) {
151+
if (responseBody.contentType() == null) {
152152
try {
153-
return new BoxAPIResponse(response.code(),
154-
response.request().method(),
155-
response.request().url().toString(),
156-
response.headers().toMultimap()
157-
);
153+
return emptyContentResponse(response);
158154
} finally {
159155
responseBody.close();
160156
}
161157
}
162158
if (responseBody != null && responseBody.contentType() != null) {
163159
if (responseBody.contentType().toString().contains(APPLICATION_JSON)) {
160+
if (responseBody.contentLength() == 0) {
161+
return emptyContentResponse(response);
162+
}
164163
String bodyAsString = "";
165164
try {
166165
bodyAsString = responseBody.string();
@@ -189,6 +188,14 @@ static BoxAPIResponse toBoxResponse(Response response) {
189188
);
190189
}
191190

191+
private static BoxAPIResponse emptyContentResponse(Response response) {
192+
return new BoxAPIResponse(response.code(),
193+
response.request().method(),
194+
response.request().url().toString(),
195+
response.headers().toMultimap()
196+
);
197+
}
198+
192199
/**
193200
* Gets the response code returned by the API.
194201
*

src/test/resources/sample-files/empty_file

Whitespace-only changes.

0 commit comments

Comments
 (0)