|
1 | 1 | package com.box.sdk; |
2 | 2 |
|
| 3 | +import static com.box.sdk.BinaryBodyUtils.writeStreamWithContentLength; |
3 | 4 | import static com.box.sdk.StandardCharsets.UTF_8; |
4 | 5 | import static com.box.sdk.http.ContentType.APPLICATION_JSON; |
5 | 6 | import static java.util.Arrays.asList; |
|
9 | 10 | import static org.hamcrest.Matchers.is; |
10 | 11 |
|
11 | 12 | import java.io.ByteArrayInputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.OutputStream; |
12 | 16 | import java.util.List; |
13 | 17 | import java.util.Map; |
14 | 18 | import java.util.TreeMap; |
15 | 19 | import org.junit.Test; |
16 | 20 |
|
17 | 21 | public class BoxAPIResponseTest { |
18 | 22 |
|
| 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 | + |
19 | 78 | @Test |
20 | 79 | public void testAPIResponseHeaderIsCaseInsensitive() { |
21 | 80 | Map<String, List<String>> headers = new TreeMap<>(); |
@@ -85,4 +144,18 @@ public void testJsonResponseToString() { |
85 | 144 | + "Body:\n{\"foo\":\"bar\"}") |
86 | 145 | ); |
87 | 146 | } |
| 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 | + } |
88 | 161 | } |
0 commit comments