Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arjankowski committed Nov 28, 2024
1 parent 8d71339 commit f08074a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/main/java/com/box/sdk/BinaryBodyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
final class BinaryBodyUtils {
private static final int BUFFER_SIZE = 8192;
private static final String X_ORIGINAL_CONTENT_LENGTH = "X-Original-Content-Length";

private BinaryBodyUtils() {
// utility class has no public constructor
Expand Down Expand Up @@ -93,18 +92,19 @@ static void writeStreamWithContentLength(BoxAPIResponse response, OutputStream o
private static long getContentLengthFromAPIResponse(BoxAPIResponse response) {
long length = response.getContentLength();
if (length == -1) {
try {
if (response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)) {
length = Integer.parseInt(response.getHeaders().get(HttpHeaders.CONTENT_LENGTH).get(0));
} else if (response.getHeaders().containsKey(X_ORIGINAL_CONTENT_LENGTH)) {
length = Integer.parseInt(response.getHeaders().get(X_ORIGINAL_CONTENT_LENGTH).get(0));
String headerValue = null;
if (response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)) {
headerValue = response.getHeaders().get(HttpHeaders.CONTENT_LENGTH).get(0);
} else if (response.getHeaders().containsKey(HttpHeaders.X_ORIGINAL_CONTENT_LENGTH)) {
headerValue = response.getHeaders().get(HttpHeaders.X_ORIGINAL_CONTENT_LENGTH).get(0);
}

if (headerValue != null) {
try {
length = Integer.parseInt(headerValue);
} catch (NumberFormatException e) {
throw new RuntimeException("Invalid content length: " + headerValue);
}
} catch (NumberFormatException e) {
String headerValue = response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)
? response.getHeaders().get(HttpHeaders.CONTENT_LENGTH).get(0)
: response.getHeaders().get(X_ORIGINAL_CONTENT_LENGTH).get(0);
throw new RuntimeException(
"Invalid content length: " + headerValue);
}
}

Expand Down Expand Up @@ -158,8 +158,8 @@ static void writeStreamTo(InputStream input, OutputStream output, long expectedL
totalBytesRead += n; // Track the total bytes read
}
if (totalBytesRead != expectedLength) {
throw new IOException("Stream ended prematurely. Expected " + expectedLength
+ " bytes, but read " + totalBytesRead + " bytes.");
throw new IOException("Stream ended prematurely. Expected "
+ expectedLength + " bytes, but read " + totalBytesRead + " bytes.");
}
} catch (IOException e) {
throw new RuntimeException("Error during streaming: " + e.getMessage(), e);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/box/sdk/http/HttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public final class HttpHeaders {
*/
public static final String CONTENT_LENGTH = "Content-Length";

/**
* HTTP header key X-Original-Content-Length.
*/
public static final String X_ORIGINAL_CONTENT_LENGTH = "X-Original-Content-Length";

/**
* HTTP header key Content-Type.
*/
Expand Down

0 comments on commit f08074a

Please sign in to comment.