Skip to content

Commit b7307e5

Browse files
committed
Add bit more commentary on README wrt buffering
1 parent 5e6952a commit b7307e5

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ Then in your code you can simply do ([Javadoc](http://sonatype.github.com/async-
2929
Response r = f.get();
3030
```
3131

32-
You can also accomplish asynchronous operation without using a Future if you want to receive and process the response in your handler:
32+
Note that in this case all the content must be read fully in memory, even if you used `getResponseBodyAsStream()' method on returned `Response` object.
33+
34+
You can also accomplish asynchronous (non-blocking) operation without using a Future if you want to receive and process the response in your handler:
3335

3436
```java
3537
import com.ning.http.client.*;
@@ -52,6 +54,8 @@ You can also accomplish asynchronous operation without using a Future if you wan
5254
});
5355
```
5456

57+
(this will also fully read `Response` in memory before calling `onCompleted`)
58+
5559
You can also mix Future with AsyncHandler to only retrieve part of the asynchronous response
5660

5761
```java
@@ -74,9 +78,11 @@ You can also mix Future with AsyncHandler to only retrieve part of the asynchron
7478
}
7579
});
7680

77-
int statuѕCode = f.get();
81+
int statusCode = f.get();
7882
```
7983

84+
which is something you want to do for large responses: this way you can process content as soon as it becomes available, piece by piece, without having to buffer it all in memory.
85+
8086
You have full control on the Response life cycle, so you can decide at any moment to stop processing what the server is sending back:
8187

8288
```java
@@ -85,14 +91,16 @@ You can also mix Future with AsyncHandler to only retrieve part of the asynchron
8591

8692
AsyncHttpClient c = new AsyncHttpClient();
8793
Future<String> f = c.prepareGet("http://www.ning.com/ ").execute(new AsyncHandler<String>() {
88-
private StringBuilder builder = new StringBuilder();
94+
private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
8995

9096
@Override
9197
public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
9298
int statusCode = status.getStatusCode();
93-
// The Status have been read
94-
// If you don't want to read the headers,body or stop processing the response
95-
return STATE.ABORT;
99+
// The Status have been read
100+
// If you don't want to read the headers,body or stop processing the response
101+
if (statusCode >= 500) {
102+
return STATE.ABORT;
103+
}
96104
}
97105

98106
@Override
@@ -105,15 +113,16 @@ You can also mix Future with AsyncHandler to only retrieve part of the asynchron
105113

106114
@Override
107115
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
108-
builder.append(new String(bodyPart.getBodyPartBytes()));
116+
bytes.write(bodyPart.getBodyPartBytes());
109117
return STATE.CONTINUE
110118
}
111119

112120
@Override
113121
public String onCompleted() throws Exception {
114122
// Will be invoked once the response has been fully read or a ResponseComplete exception
115123
// has been thrown.
116-
return builder.toString();
124+
// NOTE: should probably use Content-Encoding from headers
125+
return bytes.toString("UTF-8");
117126
}
118127

119128
@Override

0 commit comments

Comments
 (0)