Skip to content

Commit 53952d1

Browse files
author
Abdul Haleem Sheikh
committed
feat: Tracking the bytes in output stream from representation service in BoxFile
1 parent 6ea70f7 commit 53952d1

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ static void writeStream(BoxAPIResponse response, OutputStream output, ProgressLi
4646
}
4747
}
4848

49+
/**
50+
* Writes response body bytes to output stream. After all closes the input stream.
51+
*
52+
* @param response Response that is going to be written.
53+
* @param output Output stream.
54+
* @param listener Listener that will be notified on writing response. Can be null.
55+
*/
56+
57+
static void writeStreamWithContentLength(BoxAPIResponse response, OutputStream output, ProgressListener listener) {
58+
try {
59+
InputStream input;
60+
if (listener != null) {
61+
input = response.getBody(listener);
62+
} else {
63+
input = response.getBody();
64+
}
65+
writeStreamTo(input, output, response.getContentLength());
66+
} finally {
67+
response.close();
68+
}
69+
}
70+
4971
/**
5072
* Writes content of input stream to provided output. Method is NOT closing input stream.
5173
*
@@ -71,4 +93,31 @@ static void writeStreamTo(InputStream input, OutputStream output) {
7193
}
7294
}
7395
}
96+
97+
static void writeStreamTo(InputStream input, OutputStream output, long expectedLength) {
98+
long totalBytesRead = 0;
99+
if (expectedLength < 0) {
100+
throw new RuntimeException("No Data bytes in stream");
101+
}
102+
try {
103+
byte[] buffer = new byte[8192];
104+
for (int n = input.read(buffer); n != -1; n = input.read(buffer)) {
105+
output.write(buffer, 0, n);
106+
totalBytesRead += n; // Track the total bytes read
107+
}
108+
if (totalBytesRead != expectedLength) {
109+
throw new IOException("Stream ended prematurely. Expected " + expectedLength
110+
+ " bytes, but read " + totalBytesRead + " bytes.");
111+
}
112+
} catch (IOException e) {
113+
throw new RuntimeException("Error during streaming: " + e.getMessage(), e);
114+
} finally {
115+
try {
116+
input.close();
117+
output.close();
118+
} catch (IOException closeException) {
119+
throw new RuntimeException("IOException during stream close", closeException);
120+
}
121+
}
122+
}
74123
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.box.sdk;
22

33
import static com.box.sdk.BinaryBodyUtils.writeStream;
4+
import static com.box.sdk.BinaryBodyUtils.writeStreamWithContentLength;
45
import static com.box.sdk.http.ContentType.APPLICATION_JSON;
56
import static com.box.sdk.http.ContentType.APPLICATION_JSON_PATCH;
67
import static com.eclipsesource.json.Json.NULL;
@@ -602,7 +603,7 @@ private void makeRepresentationContentRequest(
602603
URL repURL = new URL(representationURLTemplate.replace("{+asset_path}", assetPath));
603604
BoxAPIRequest repContentReq = new BoxAPIRequest(this.getAPI(), repURL, HttpMethod.GET);
604605
BoxAPIResponse response = repContentReq.send();
605-
writeStream(response, output);
606+
writeStreamWithContentLength(response, output, null);
606607
} catch (MalformedURLException ex) {
607608

608609
throw new BoxAPIException("Could not generate representation content URL");

src/test/java/com/box/sdk/BoxFileTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ public void testGetThumbnailSucceeds() {
433433
))
434434
.willReturn(WireMock.aResponse()
435435
.withHeader("Content-Type", "image/jpg")
436+
.withHeader("Content-Length", String.valueOf(13))
436437
.withBody("This is a JPG")
437438
.withStatus(200))
438439
);
@@ -517,6 +518,7 @@ public void testGetRepresentationContentSuccess() {
517518
))
518519
.willReturn(WireMock.aResponse()
519520
.withHeader("Content-Type", "image/jpg")
521+
.withHeader("Content-Length", String.valueOf(13))
520522
.withBody("This is a JPG")
521523
.withStatus(200))
522524
);

0 commit comments

Comments
 (0)