Skip to content

Commit 3bcf788

Browse files
authored
fix: Fix parsing content length header (#1292)
1 parent 84a46d5 commit 3bcf788

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static long getContentLengthFromAPIResponse(BoxAPIResponse response) {
101101

102102
if (headerValue != null) {
103103
try {
104-
length = Integer.parseInt(headerValue);
104+
length = Long.parseLong(headerValue);
105105
} catch (NumberFormatException e) {
106106
throw new RuntimeException("Invalid content length: " + headerValue);
107107
}

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.box.sdk;
22

3+
import static com.box.sdk.BinaryBodyUtils.writeStreamWithContentLength;
34
import static com.box.sdk.StandardCharsets.UTF_8;
45
import static com.box.sdk.http.ContentType.APPLICATION_JSON;
56
import static java.util.Arrays.asList;
@@ -9,13 +10,71 @@
910
import static org.hamcrest.Matchers.is;
1011

1112
import java.io.ByteArrayInputStream;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.OutputStream;
1216
import java.util.List;
1317
import java.util.Map;
1418
import java.util.TreeMap;
1519
import org.junit.Test;
1620

1721
public class BoxAPIResponseTest {
1822

23+
/**
24+
* Simulated large InputStream that generates data on demand.
25+
*/
26+
static class LargeByteArrayInputStream extends InputStream {
27+
private final long size;
28+
private long bytesRead = 0;
29+
30+
LargeByteArrayInputStream(long size) {
31+
this.size = size;
32+
}
33+
34+
@Override
35+
public int read() throws IOException {
36+
if (bytesRead < size) {
37+
bytesRead++;
38+
return 'A'; // Simulated byte
39+
}
40+
return -1; // End of stream
41+
}
42+
43+
@Override
44+
public int read(byte[] b, int off, int len) throws IOException {
45+
if (bytesRead >= size) {
46+
return -1;
47+
}
48+
int bytesToRead = (int) Math.min(len, size - bytesRead);
49+
for (int i = 0; i < bytesToRead; i++) {
50+
b[off + i] = 'A';
51+
}
52+
bytesRead += bytesToRead;
53+
return bytesToRead;
54+
}
55+
}
56+
57+
58+
/**
59+
* Null OutputStream that discards written data (useful for counting bytes).
60+
*/
61+
class NullOutputStream extends OutputStream {
62+
long bytesWritten = 0;
63+
@Override
64+
public void write(int b) {
65+
bytesWritten++;
66+
}
67+
68+
@Override
69+
public void write(byte[] b, int off, int len) {
70+
bytesWritten += len;
71+
}
72+
73+
long getBytesWritten() {
74+
return bytesWritten;
75+
}
76+
}
77+
1978
@Test
2079
public void testAPIResponseHeaderIsCaseInsensitive() {
2180
Map<String, List<String>> headers = new TreeMap<>();
@@ -85,4 +144,18 @@ public void testJsonResponseToString() {
85144
+ "Body:\n{\"foo\":\"bar\"}")
86145
);
87146
}
147+
148+
@Test
149+
public void testLargeBinaryResponseContentLength() {
150+
long contentLength = Integer.MAX_VALUE + 10000L;
151+
Map<String, List<String>> headers = new TreeMap<>();
152+
headers.put("content-length", singletonList(Long.toString(contentLength)));
153+
LargeByteArrayInputStream inputStream = new LargeByteArrayInputStream(contentLength);
154+
NullOutputStream outputStream = new NullOutputStream();
155+
BoxAPIResponse response = new BoxAPIResponse(
156+
202, "GET", "https://aaa.com", headers, inputStream, "image/jpg", -1
157+
);
158+
writeStreamWithContentLength(response, outputStream);
159+
assertThat(outputStream.getBytesWritten(), is(contentLength));
160+
}
88161
}

0 commit comments

Comments
 (0)