Search in sources :

Example 26 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class MultipartTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    httpServer = new AsyncHttpServer();
    httpServer.setErrorCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            fail();
        }
    });
    httpServer.listen(AsyncServer.getDefault(), 5000);
    httpServer.post("/", new HttpServerRequestCallback() {

        int gotten = 0;

        @Override
        public void onRequest(final AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            final MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
            body.setMultipartCallback(new MultipartCallback() {

                @Override
                public void onPart(Part part) {
                    if (part.isFile()) {
                        body.setDataCallback(new DataCallback() {

                            @Override
                            public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                                gotten += bb.remaining();
                                bb.recycle();
                            }
                        });
                    }
                }
            });
            request.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    response.send(body.getField("baz") + gotten + body.getField("foo"));
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) MultipartCallback(com.koushikdutta.async.http.body.MultipartFormDataBody.MultipartCallback) DataCallback(com.koushikdutta.async.callback.DataCallback) Part(com.koushikdutta.async.http.body.Part) DataEmitter(com.koushikdutta.async.DataEmitter) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 27 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class ParserTests method testUtf8String.

public void testUtf8String() throws Exception {
    StringParser p = new StringParser();
    FilteredDataEmitter f = new FilteredDataEmitter() {

        @Override
        public String charset() {
            return Charsets.UTF_8.name();
        }

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    Future<String> ret = p.parse(f);
    ByteBufferList l = new ByteBufferList();
    l.add(ByteBuffer.wrap("æææ".getBytes(Charsets.UTF_8.name())));
    f.onDataAvailable(f, l);
    f.getEndCallback().onCompleted(null);
    String s = ret.get();
    assertEquals(s, "æææ");
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) StringParser(com.koushikdutta.async.parser.StringParser) ByteBufferList(com.koushikdutta.async.ByteBufferList)

Example 28 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class ParserTests method testString.

public void testString() throws Exception {
    StringParser p = new StringParser();
    FilteredDataEmitter f = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    Future<String> ret = p.parse(f);
    ByteBufferList l = new ByteBufferList();
    l.add(ByteBuffer.wrap("foo".getBytes(Charsets.US_ASCII.name())));
    f.onDataAvailable(f, l);
    f.getEndCallback().onCompleted(null);
    String s = ret.get();
    assertEquals(s, "foo");
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) StringParser(com.koushikdutta.async.parser.StringParser) ByteBufferList(com.koushikdutta.async.ByteBufferList)

Example 29 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class ByteUtilTests method testPushParserUntil.

public void testPushParserUntil() {
    valRead = 0;
    FilteredDataEmitter mock = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    new PushParser(mock).until((byte) 0, new DataCallback.NullDataCallback()).readInt(new PushParser.ParseCallback<Integer>() {

        public void parsed(Integer arg) {
            valRead = arg;
        }
    });
    byte[] bytes = new byte[] { 5, 5, 5, 5, 0, 10, 5, 5, 5 };
    Util.emitAllData(mock, new ByteBufferList(bytes));
    assertEquals(valRead, 0x0A050505);
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) PushParser(com.koushikdutta.async.PushParser) ByteBufferList(com.koushikdutta.async.ByteBufferList)

Example 30 with ByteBufferList

use of com.koushikdutta.async.ByteBufferList in project AndroidAsync by koush.

the class ByteUtilTests method testTapCallback.

public void testTapCallback() {
    readInt = 0;
    readByte = 0;
    readString = "";
    FilteredDataEmitter mock = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    new PushParser(mock).readInt().readByte().readString().tap(new TapCallback() {

        void tap(int i, byte b, String s) {
            readInt = i;
            readByte = b;
            readString = s;
        }
    });
    byte[] bytes = new byte[] { 10, 5, 5, 5, 3, 0, 0, 0, 4, 116, 101, 115, 116 };
    Util.emitAllData(mock, new ByteBufferList(bytes));
    assertEquals(readInt, 0x0A050505);
    assertEquals(readByte, (byte) 3);
    assertEquals(readString, "test");
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) PushParser(com.koushikdutta.async.PushParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) TapCallback(com.koushikdutta.async.TapCallback)

Aggregations

ByteBufferList (com.koushikdutta.async.ByteBufferList)39 DataEmitter (com.koushikdutta.async.DataEmitter)13 DataCallback (com.koushikdutta.async.callback.DataCallback)13 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)10 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)10 ByteBuffer (java.nio.ByteBuffer)10 IOException (java.io.IOException)6 Semaphore (java.util.concurrent.Semaphore)5 PushParser (com.koushikdutta.async.PushParser)4 ExecutionException (java.util.concurrent.ExecutionException)4 JsonObject (com.google.gson.JsonObject)3 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)3 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)3 GsonObjectParser (com.koushikdutta.ion.gson.GsonObjectParser)3 TimeoutException (java.util.concurrent.TimeoutException)3 BEncodedDictionary (org.cyanogenmod.pushsms.bencode.BEncodedDictionary)3 RemoteException (android.os.RemoteException)2 JsonParseException (com.google.gson.JsonParseException)2 LineEmitter (com.koushikdutta.async.LineEmitter)2 TapCallback (com.koushikdutta.async.TapCallback)2