Skip to content

Commit 6c742ca

Browse files
committed
fixes
1 parent 8d71339 commit 6c742ca

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
final class BinaryBodyUtils {
1212
private static final int BUFFER_SIZE = 8192;
13-
private static final String X_ORIGINAL_CONTENT_LENGTH = "X-Original-Content-Length";
1413

1514
private BinaryBodyUtils() {
1615
// utility class has no public constructor
@@ -93,18 +92,19 @@ static void writeStreamWithContentLength(BoxAPIResponse response, OutputStream o
9392
private static long getContentLengthFromAPIResponse(BoxAPIResponse response) {
9493
long length = response.getContentLength();
9594
if (length == -1) {
96-
try {
97-
if (response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)) {
98-
length = Integer.parseInt(response.getHeaders().get(HttpHeaders.CONTENT_LENGTH).get(0));
99-
} else if (response.getHeaders().containsKey(X_ORIGINAL_CONTENT_LENGTH)) {
100-
length = Integer.parseInt(response.getHeaders().get(X_ORIGINAL_CONTENT_LENGTH).get(0));
95+
String headerValue = null;
96+
if (response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)) {
97+
headerValue = response.getHeaders().get(HttpHeaders.CONTENT_LENGTH).get(0);
98+
} else if (response.getHeaders().containsKey(HttpHeaders.X_ORIGINAL_CONTENT_LENGTH)) {
99+
headerValue = response.getHeaders().get(HttpHeaders.X_ORIGINAL_CONTENT_LENGTH).get(0);
100+
}
101+
102+
if (headerValue != null) {
103+
try {
104+
length = Integer.parseInt(headerValue);
105+
} catch (NumberFormatException e) {
106+
throw new RuntimeException("Invalid content length: " + headerValue);
101107
}
102-
} catch (NumberFormatException e) {
103-
String headerValue = response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)
104-
? response.getHeaders().get(HttpHeaders.CONTENT_LENGTH).get(0)
105-
: response.getHeaders().get(X_ORIGINAL_CONTENT_LENGTH).get(0);
106-
throw new RuntimeException(
107-
"Invalid content length: " + headerValue);
108108
}
109109
}
110110

@@ -158,8 +158,7 @@ static void writeStreamTo(InputStream input, OutputStream output, long expectedL
158158
totalBytesRead += n; // Track the total bytes read
159159
}
160160
if (totalBytesRead != expectedLength) {
161-
throw new IOException("Stream ended prematurely. Expected " + expectedLength
162-
+ " bytes, but read " + totalBytesRead + " bytes.");
161+
throw new IOException("Stream ended prematurely. Expected " + expectedLength + " bytes, but read " + totalBytesRead + " bytes.");
163162
}
164163
} catch (IOException e) {
165164
throw new RuntimeException("Error during streaming: " + e.getMessage(), e);

src/main/java/com/box/sdk/http/HttpHeaders.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public final class HttpHeaders {
1010
*/
1111
public static final String CONTENT_LENGTH = "Content-Length";
1212

13+
/**
14+
* HTTP header key X-Original-Content-Length.
15+
*/
16+
public static final String X_ORIGINAL_CONTENT_LENGTH = "X-Original-Content-Length";
17+
1318
/**
1419
* HTTP header key Content-Type.
1520
*/

0 commit comments

Comments
 (0)