Skip to content

Commit 0b35541

Browse files
committed
allow use of progressmonitor for byte bodies
1 parent 7c1562d commit 0b35541

5 files changed

Lines changed: 90 additions & 38 deletions

File tree

unirest-bdd-tests/src/test/java/BehaviorTests/DownloadProgressTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,24 @@ void canAddUploadProgressAsync() throws Exception {
6464
monitor.assertSpideyFileDownload("spidey.jpg");
6565
}
6666

67+
68+
@Test
69+
void canAddUploadProgressWithBytes() {
70+
Unirest.get(MockServer.BINARYFILE)
71+
.downloadMonitor(monitor)
72+
.asBytes();
73+
74+
monitor.assertSpideyFileDownload("body");
75+
}
76+
77+
@Test
78+
void canAddUploadProgressWithBytesAsync() throws Exception {
79+
Unirest.get(MockServer.BINARYFILE)
80+
.downloadMonitor(monitor)
81+
.asBytesAsync()
82+
.get();
83+
84+
monitor.assertSpideyFileDownload("body");
85+
}
86+
6787
}

unirest/src/main/java/kong/unirest/BaseRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,17 @@ public CompletableFuture<HttpResponse<String>> asStringAsync(Callback<String> ca
226226

227227
@Override
228228
public HttpResponse<byte[]> asBytes() {
229-
return request(ByteResponse::new, byte[].class);
229+
return request(r -> new ByteResponse(r, downloadMonitor), byte[].class);
230230
}
231231

232232
@Override
233233
public CompletableFuture<HttpResponse<byte[]>> asBytesAsync() {
234-
return config.getAsyncClient().request(this, ByteResponse::new, new CompletableFuture<>(), byte[].class);
234+
return config.getAsyncClient().request(this, r -> new ByteResponse(r, downloadMonitor), new CompletableFuture<>(), byte[].class);
235235
}
236236

237237
@Override
238238
public CompletableFuture<HttpResponse<byte[]>> asBytesAsync(Callback<byte[]> callback) {
239-
return config.getAsyncClient().request(this, ByteResponse::new, wrap(callback), byte[].class);
239+
return config.getAsyncClient().request(this, r -> new ByteResponse(r, downloadMonitor), wrap(callback), byte[].class);
240240
}
241241

242242
@Override

unirest/src/main/java/kong/unirest/ByteResponse.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,54 @@
2525

2626
package kong.unirest;
2727

28+
import java.io.ByteArrayInputStream;
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.IOException;
31+
import java.io.InputStream;
32+
2833
public class ByteResponse extends BaseResponse<byte[]> {
2934
private final byte[] body;
3035

31-
public ByteResponse(RawResponse r) {
36+
public ByteResponse(RawResponse r, ProgressMonitor downloadMonitor) {
3237
super(r);
33-
this.body = r.getContentAsBytes();
38+
if(downloadMonitor == null) {
39+
this.body = r.getContentAsBytes();
40+
} else {
41+
MonitoringInputStream ip = new MonitoringInputStream(r.getContent(), downloadMonitor, (String)null, r);
42+
try {
43+
body = getBytes(ip);
44+
} catch (IOException e){
45+
throw new UnirestException(e);
46+
}
47+
}
48+
}
49+
50+
public static byte[] getBytes(InputStream is) throws IOException {
51+
try {
52+
int len;
53+
int size = 1024;
54+
byte[] buf;
55+
56+
if (is instanceof ByteArrayInputStream) {
57+
size = is.available();
58+
buf = new byte[size];
59+
len = is.read(buf, 0, size);
60+
} else {
61+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
62+
buf = new byte[size];
63+
while ((len = is.read(buf, 0, size)) != -1) {
64+
bos.write(buf, 0, len);
65+
}
66+
buf = bos.toByteArray();
67+
}
68+
return buf;
69+
} finally {
70+
is.close();
71+
}
72+
}
73+
74+
public static boolean isGzipped(String value) {
75+
return "gzip".equalsIgnoreCase(value.toLowerCase().trim());
3476
}
3577

3678
@Override

unirest/src/main/java/kong/unirest/MonitoringInputStream.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.IOException;
2929
import java.io.InputStream;
3030
import java.nio.file.Path;
31+
import java.util.zip.GZIPInputStream;
3132

3233
class MonitoringInputStream extends InputStream {
3334
private final InputStream content;
@@ -36,11 +37,27 @@ class MonitoringInputStream extends InputStream {
3637
private long byteCount = 0;
3738
private String fileName;
3839

39-
MonitoringInputStream(InputStream content, ProgressMonitor downloadMonitor, Path target, RawResponse contentSize) {
40-
this.content = content;
40+
MonitoringInputStream(InputStream content, ProgressMonitor downloadMonitor, Path target, RawResponse rawResponse) {
41+
this(content, downloadMonitor, target.getFileName().toString(), rawResponse);
42+
}
43+
44+
MonitoringInputStream(InputStream content, ProgressMonitor downloadMonitor, String fileName, RawResponse rawResponse) {
45+
this.content = wrap(content, rawResponse);
4146
this.downloadMonitor = downloadMonitor;
42-
this.fileName = target.getFileName().toString();
43-
this.totalSize = getBodySize(contentSize);
47+
this.fileName = fileName;
48+
this.totalSize = getBodySize(rawResponse);
49+
}
50+
51+
private InputStream wrap(InputStream is , RawResponse rawResponse) {
52+
try {
53+
if (is.available() > 0 && "gzip".equalsIgnoreCase(rawResponse.getContentType())) {
54+
return new GZIPInputStream(is);
55+
} else {
56+
return is;
57+
}
58+
}catch (Exception e){
59+
throw new UnirestException(e);
60+
}
4461
}
4562

4663
private Long getBodySize(RawResponse r) {

unirest/src/main/java/kong/unirest/apache/ApacheResponse.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public byte[] getContentAsBytes() {
8181
}
8282
try {
8383
InputStream is = getContent();
84-
if (is.available() > 0 && isGzipped(getEncoding())) {
84+
if (is.available() > 0 && ByteResponse.isGzipped(getEncoding())) {
8585
is = new GZIPInputStream(getContent());
8686
}
87-
return getBytes(is);
87+
return ByteResponse.getBytes(is);
8888
} catch (IOException e2) {
8989
throw new UnirestException(e2);
9090
} finally {
@@ -149,31 +149,4 @@ public String getEncoding() {
149149
return "";
150150
}
151151

152-
private static byte[] getBytes(InputStream is) throws IOException {
153-
try {
154-
int len;
155-
int size = 1024;
156-
byte[] buf;
157-
158-
if (is instanceof ByteArrayInputStream) {
159-
size = is.available();
160-
buf = new byte[size];
161-
len = is.read(buf, 0, size);
162-
} else {
163-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
164-
buf = new byte[size];
165-
while ((len = is.read(buf, 0, size)) != -1) {
166-
bos.write(buf, 0, len);
167-
}
168-
buf = bos.toByteArray();
169-
}
170-
return buf;
171-
} finally {
172-
is.close();
173-
}
174-
}
175-
176-
private static boolean isGzipped(String value) {
177-
return "gzip".equalsIgnoreCase(value.toLowerCase().trim());
178-
}
179152
}

0 commit comments

Comments
 (0)