Skip to content

Commit 3b0e730

Browse files
committed
Add support for InputStream and tests for sending byte[] as List<Short>
1 parent 15b448c commit 3b0e730

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/AndroidClient/client/src/main/java/net/servicestack/client/JsonServiceClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,10 @@ public <TResponse> TResponse send(String url, String httpMethod, Object request,
317317

318318
public <TResponse> TResponse send(String requestUrl, String httpMethod, byte[] requestBody, String requestType, Object responseClass) {
319319
HttpURLConnection req = null;
320+
Class resClass = null;
320321
try {
321322
req = createRequest(requestUrl, httpMethod, requestBody, requestType);
322-
Class resClass = responseClass instanceof Class ? (Class)responseClass : null;
323+
resClass = responseClass instanceof Class ? (Class)responseClass : null;
323324
Type resType = responseClass instanceof Type ? (Type)responseClass : null;
324325
if (resClass == null && resType == null)
325326
throw new RuntimeException("responseClass '" + responseClass.getClass().getSimpleName() + "' must be a Class or Type");
@@ -364,6 +365,8 @@ public <TResponse> TResponse send(String requestUrl, String httpMethod, byte[] r
364365
return (TResponse)Utils.readBytesToEnd(req);
365366
if (resClass == String.class)
366367
return (TResponse)Utils.readToEnd(is, UTF8.name());
368+
if (resClass == InputStream.class)
369+
return (TResponse)is;
367370

368371
if (Log.isDebugEnabled()) {
369372
String json = Utils.readToEnd(is, UTF8.name());
@@ -388,7 +391,7 @@ public <TResponse> TResponse send(String requestUrl, String httpMethod, byte[] r
388391
throw new RuntimeException(e);
389392
}
390393
finally {
391-
if (req != null)
394+
if (req != null && resClass != InputStream.class)
392395
req.disconnect();
393396
}
394397
}

src/AndroidClient/client/src/test/java/net/servicestack/client/tests/TestServiceTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import net.servicestack.client.*;
88
import net.servicestack.client.tests.testdtos.*;
99

10+
import java.io.IOException;
11+
import java.io.InputStream;
1012
import java.math.BigDecimal;
1113
import java.math.BigInteger;
1214
import java.net.HttpURLConnection;
@@ -283,6 +285,27 @@ public void test_Can_get_response_as_Raw_Bytes() {
283285
assertEquals("World", Utils.fromUtf8Bytes(response));
284286
}
285287

288+
public void test_Can_get_response_as_Stream() throws IOException {
289+
InputStream is = client.get("/json/reply/HelloString?Name=World", InputStream.class);
290+
byte[] response = Utils.readBytesToEnd(is);
291+
is.close();
292+
assertEquals("World", Utils.fromUtf8Bytes(response));
293+
}
294+
295+
public void test_Can_post_data_and_read_as_Stream() throws IOException {
296+
ReturnStream req = new ReturnStream();
297+
ArrayList<Short> data = new ArrayList<>();
298+
data.add((short) 65);
299+
data.add((short) 66);
300+
data.add((short) 67);
301+
req.setData(data);
302+
303+
InputStream is = client.post(req);
304+
byte[] response = Utils.readBytesToEnd(is);
305+
is.close();
306+
assertEquals("ABC", Utils.fromUtf8Bytes(response));
307+
}
308+
286309
/* TEST HELPERS */
287310

288311
public static HelloAllTypes createHelloAllTypes(){

0 commit comments

Comments
 (0)