Skip to content

Commit 1a2843b

Browse files
committed
various refactorings. add support for a buffer limit on BufferedDataSink./
1 parent 1f131ce commit 1a2843b

14 files changed

Lines changed: 204 additions & 124 deletions

AndroidAsync/src/com/koushikdutta/async/AsyncSocketImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.nio.channels.SocketChannel;
88

99
import junit.framework.Assert;
10-
import android.util.Log;
1110

1211
import com.koushikdutta.async.callback.ClosedCallback;
1312
import com.koushikdutta.async.callback.DataCallback;
@@ -53,7 +52,9 @@ public void write(ByteBufferList list) {
5352
handleRemaining(list.remaining());
5453
}
5554
catch (IOException e) {
56-
e.printStackTrace();
55+
close();
56+
report(e);
57+
reportClose();
5758
}
5859
}
5960

@@ -83,7 +84,9 @@ public void write(ByteBuffer b) {
8384
handleRemaining(b.remaining());
8485
}
8586
catch (IOException ex) {
86-
ex.printStackTrace();
87+
close();
88+
report(ex);
89+
reportClose();
8790
}
8891
}
8992

@@ -216,6 +219,11 @@ public boolean isConnected() {
216219
return mChannel.isConnected();
217220
}
218221

222+
@Override
223+
public boolean isOpen() {
224+
return mChannel.isConnected();
225+
}
226+
219227
@Override
220228
public void pause() {
221229
mKey.interestOps(~SelectionKey.OP_READ & mKey.interestOps());

AndroidAsync/src/com/koushikdutta/async/BufferedDataSink.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,68 +25,63 @@ public DataSink getDataSink() {
2525

2626
private void writePending() {
2727
// Log.i("NIO", "Writing to buffer...");
28-
mDataSink.write(mPendingWrites);
29-
if (mPendingWrites.remaining() == 0) {
30-
mPendingWrites = null;
31-
onFlushed();
28+
if (mPendingWrites != null) {
29+
mDataSink.write(mPendingWrites);
30+
if (mPendingWrites.remaining() == 0)
31+
mPendingWrites = null;
3232
}
33+
if (mPendingWrites == null && mWritable != null)
34+
mWritable.onWriteable();
3335
}
3436

3537
ByteBufferList mPendingWrites;
3638

3739
@Override
3840
public void write(ByteBuffer bb) {
39-
if (mPendingWrites == null) {
40-
mDataSink.write(bb);
41-
if (bb.remaining() > 0) {
42-
mPendingWrites = new ByteBufferList();
43-
mPendingWrites.add(ByteBuffer.wrap(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining()));
44-
bb.position(0);
45-
bb.limit(0);
46-
}
47-
}
48-
else {
49-
mPendingWrites.add(ByteBuffer.wrap(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining()));
50-
bb.position(0);
51-
bb.limit(0);
52-
writePending();
53-
}
41+
ByteBufferList bbl = new ByteBufferList();
42+
bbl.add(bb);
43+
write(bbl);
5444
}
5545

5646
@Override
5747
public void write(ByteBufferList bb) {
58-
if (mPendingWrites == null) {
48+
if (mPendingWrites == null)
5949
mDataSink.write(bb);
60-
if (bb.remaining() > 0) {
61-
mPendingWrites = new ByteBufferList();
62-
mPendingWrites.add(bb);
50+
51+
if (bb.remaining() > 0) {
52+
int toRead = Math.min(bb.remaining(), mMaxBuffer);
53+
if (toRead > 0) {
54+
if (mPendingWrites == null)
55+
mPendingWrites = new ByteBufferList();
56+
mPendingWrites.add(bb.get(toRead));
6357
}
64-
bb.clear();
65-
}
66-
else {
67-
mPendingWrites.add(bb);
68-
bb.clear();
69-
writePending();
7058
}
7159
}
7260

61+
WritableCallback mWritable;
7362
@Override
7463
public void setWriteableCallback(WritableCallback handler) {
75-
Assert.fail("BufferingDataSink is always writeable.");
64+
mWritable = handler;
7665
}
7766

7867
@Override
7968
public WritableCallback getWriteableCallback() {
80-
Assert.fail("BufferingDataSink is always writeable.");
81-
return null;
69+
return mWritable;
8270
}
8371

8472
public int remaining() {
8573
if (mPendingWrites == null)
8674
return 0;
8775
return mPendingWrites.remaining();
8876
}
89-
90-
public void onFlushed() {
77+
78+
int mMaxBuffer = Integer.MAX_VALUE;
79+
public int getMaxBuffer() {
80+
return mMaxBuffer;
81+
}
82+
83+
public void setMaxBuffer(int maxBuffer) {
84+
Assert.assertTrue(maxBuffer >= 0);
85+
mMaxBuffer = maxBuffer;
9186
}
9287
}

AndroidAsync/src/com/koushikdutta/async/CloseableData.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.koushikdutta.async.callback.ClosedCallback;
44

55
public interface CloseableData {
6+
public boolean isOpen();
67
public void close();
78
public void setClosedCallback(ClosedCallback handler);
89
public ClosedCallback getCloseHandler();

AndroidAsync/src/com/koushikdutta/async/Util.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,22 @@ public void onWriteable() {
8282

8383
cb.onWriteable();
8484
}
85+
86+
public static void writeAll(final DataSink sink, final ByteBufferList bb) {
87+
sink.setWriteableCallback(new WritableCallback() {
88+
@Override
89+
public void onWriteable() {
90+
if (bb.remaining() == 0)
91+
return;
92+
sink.write(bb);
93+
}
94+
});
95+
sink.write(bb);
96+
}
97+
public static void writeAll(DataSink sink, byte[] bytes) {
98+
ByteBuffer bb = ByteBuffer.wrap(bytes);
99+
ByteBufferList bbl = new ByteBufferList();
100+
bbl.add(bb);
101+
writeAll(sink, bbl);
102+
}
85103
}

AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ protected void onHeadersReceived() {
101101
String kas = headers.get("Connection");
102102
if (kas != null && "keep-alive".toLowerCase().equals(kas.toLowerCase()))
103103
keepalive = true;
104+
request.onConnect(this);
104105
callback.onConnectCompleted(null, this);
105106
}
106107
catch (Exception ex) {
@@ -207,17 +208,26 @@ private interface ResultConvert {
207208
public Object convert(ByteBufferList bb) throws Exception;
208209
}
209210

210-
public static void download(String uri, final DownloadCallback callback) {
211-
download(uri, callback, new ResultConvert() {
211+
public static void get(String uri, final DownloadCallback callback) {
212+
get(uri, callback, new ResultConvert() {
212213
@Override
213214
public Object convert(ByteBufferList b) {
214215
return b;
215216
}
216217
});
217218
}
218219

219-
public static void download(String uri, final StringCallback callback) {
220-
download(uri, callback, new ResultConvert() {
220+
public static void get(String uri, final StringCallback callback) {
221+
try {
222+
execute(new AsyncHttpGet(uri), callback);
223+
}
224+
catch (URISyntaxException e) {
225+
callback.onCompleted(e, null, null);
226+
}
227+
}
228+
229+
public static void execute(AsyncHttpRequest req, final StringCallback callback) {
230+
execute(req, callback, new ResultConvert() {
221231
@Override
222232
public Object convert(ByteBufferList bb) {
223233
StringBuilder builder = new StringBuilder();
@@ -229,21 +239,17 @@ public Object convert(ByteBufferList bb) {
229239
});
230240
}
231241

232-
public static void download(String uri, final JSONObjectCallback callback) {
233-
download(uri, callback, new ResultConvert() {
234-
@Override
235-
public Object convert(ByteBufferList bb) throws JSONException {
236-
StringBuilder builder = new StringBuilder();
237-
for (ByteBuffer b: bb) {
238-
builder.append(new String(b.array(), b.arrayOffset() + b.position(), b.remaining()));
239-
}
240-
return new JSONObject(builder.toString());
241-
}
242-
});
242+
public static void get(String uri, final JSONObjectCallback callback) {
243+
try {
244+
execute(new AsyncHttpGet(uri), callback);
245+
}
246+
catch (URISyntaxException e) {
247+
callback.onCompleted(e, null, null);
248+
}
243249
}
244250

245-
public static void download(AsyncHttpRequest req, final JSONObjectCallback callback) {
246-
download(req, callback, new ResultConvert() {
251+
public static void execute(AsyncHttpRequest req, final JSONObjectCallback callback) {
252+
execute(req, callback, new ResultConvert() {
247253
@Override
248254
public Object convert(ByteBufferList bb) throws JSONException {
249255
StringBuilder builder = new StringBuilder();
@@ -268,7 +274,16 @@ public void run() {
268274
});
269275
}
270276

271-
public static void download(String uri, final String filename, final FileCallback callback) {
277+
public static void get(String uri, final String filename, final FileCallback callback) {
278+
try {
279+
execute(new AsyncHttpGet(uri), filename, callback);
280+
}
281+
catch (URISyntaxException e) {
282+
callback.onCompleted(e, null, null);
283+
}
284+
}
285+
286+
public static void execute(AsyncHttpRequest req, final String filename, final FileCallback callback) {
272287
final Handler handler = Looper.myLooper() == null ? null : new Handler();
273288
final File file = new File(filename);
274289
final FileOutputStream fout;
@@ -279,7 +294,7 @@ public static void download(String uri, final String filename, final FileCallbac
279294
invoke(handler, callback, null, e, null);
280295
return;
281296
}
282-
connect(uri, new HttpConnectCallback() {
297+
connect(req, new HttpConnectCallback() {
283298
@Override
284299
public void onConnectCompleted(Exception ex, final AsyncHttpResponse response) {
285300
if (ex != null) {
@@ -314,7 +329,7 @@ public void onCompleted(Exception ex) {
314329
});
315330
}
316331

317-
private static void download(AsyncHttpRequest req, final ResultPairCallback callback, final ResultConvert convert) {
332+
private static void execute(AsyncHttpRequest req, final ResultPairCallback callback, final ResultConvert convert) {
318333
final Handler handler = Looper.myLooper() == null ? null : new Handler();
319334
connect(req, new HttpConnectCallback() {
320335
ByteBufferList buffer = new ByteBufferList();
@@ -348,9 +363,9 @@ public void onCompleted(Exception ex) {
348363
});
349364
}
350365

351-
private static void download(String uri, final ResultPairCallback callback, final ResultConvert convert) {
366+
private static void get(String uri, final ResultPairCallback callback, final ResultConvert convert) {
352367
try {
353-
download(new AsyncHttpGet(new URI(uri)), callback, convert);
368+
execute(new AsyncHttpGet(new URI(uri)), callback, convert);
354369
}
355370
catch (URISyntaxException e) {
356371
callback.onCompleted(e, null, null);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.koushikdutta.async.http;
2+
3+
import java.net.URI;
4+
import java.net.URISyntaxException;
5+
6+
public class AsyncHttpPost extends AsyncHttpRequest {
7+
public static final String METHOD = "POST";
8+
9+
public AsyncHttpPost(String uri) throws URISyntaxException {
10+
super(new URI(uri), METHOD);
11+
}
12+
13+
public AsyncHttpPost(URI uri) {
14+
super(uri, METHOD);
15+
}
16+
17+
@Override
18+
protected void onConnect(AsyncHttpResponse response) {
19+
}
20+
}

AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,11 @@ public boolean getFollowRedirect() {
6161
public void setFollowRedirect(boolean follow) {
6262
mFollowRedirect = follow;
6363
}
64+
65+
void onConnectInternal(AsyncHttpResponse response) {
66+
onConnect(response);
67+
}
68+
69+
protected void onConnect(AsyncHttpResponse response) {
70+
}
6471
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.koushikdutta.async.http;
2+
3+
import com.koushikdutta.async.DataSink;
4+
5+
public abstract class AsyncHttpRequestContentWriter {
6+
public abstract void write(DataSink sink);
7+
}

0 commit comments

Comments
 (0)