|
10 | 10 | */ |
11 | 11 | final class BinaryBodyUtils { |
12 | 12 | private static final int BUFFER_SIZE = 8192; |
13 | | - private static final String X_ORIGINAL_CONTENT_LENGTH = "X-Original-Content-Length"; |
14 | 13 |
|
15 | 14 | private BinaryBodyUtils() { |
16 | 15 | // utility class has no public constructor |
@@ -93,18 +92,19 @@ static void writeStreamWithContentLength(BoxAPIResponse response, OutputStream o |
93 | 92 | private static long getContentLengthFromAPIResponse(BoxAPIResponse response) { |
94 | 93 | long length = response.getContentLength(); |
95 | 94 | 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); |
101 | 107 | } |
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); |
108 | 108 | } |
109 | 109 | } |
110 | 110 |
|
@@ -158,8 +158,7 @@ static void writeStreamTo(InputStream input, OutputStream output, long expectedL |
158 | 158 | totalBytesRead += n; // Track the total bytes read |
159 | 159 | } |
160 | 160 | 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."); |
163 | 162 | } |
164 | 163 | } catch (IOException e) { |
165 | 164 | throw new RuntimeException("Error during streaming: " + e.getMessage(), e); |
|
0 commit comments