Search in sources :

Example 36 with ByteBufferList

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

the class UrlEncodedFormBody method parse.

@Override
public void parse(DataEmitter emitter, final CompletedCallback completed) {
    final ByteBufferList data = new ByteBufferList();
    emitter.setDataCallback(new DataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            bb.get(data);
        }
    });
    emitter.setEndCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            if (ex != null) {
                completed.onCompleted(ex);
                return;
            }
            try {
                mParameters = Multimap.parseUrlEncoded(data.readString());
                completed.onCompleted(null);
            } catch (Exception e) {
                completed.onCompleted(e);
            }
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataEmitter(com.koushikdutta.async.DataEmitter) DataCallback(com.koushikdutta.async.callback.DataCallback) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 37 with ByteBufferList

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

the class SpdyMiddleware method concatLengthPrefixed.

static byte[] concatLengthPrefixed(Protocol... protocols) {
    ByteBuffer result = ByteBuffer.allocate(8192);
    for (Protocol protocol : protocols) {
        // No HTTP/1.0 for NPN.
        if (protocol == Protocol.HTTP_1_0)
            continue;
        result.put((byte) protocol.toString().length());
        result.put(protocol.toString().getBytes(Charsets.UTF_8));
    }
    result.flip();
    byte[] ret = new ByteBufferList(result).getAllByteArray();
    return ret;
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) Protocol(com.koushikdutta.async.http.Protocol) ByteBuffer(java.nio.ByteBuffer)

Example 38 with ByteBufferList

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

the class ByteBufferListParser method parse.

@Override
public Future<ByteBufferList> parse(final DataEmitter emitter) {
    final ByteBufferList bb = new ByteBufferList();
    final SimpleFuture<ByteBufferList> ret = new SimpleFuture<ByteBufferList>() {

        @Override
        protected void cancelCleanup() {
            emitter.close();
        }
    };
    emitter.setDataCallback(new DataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList data) {
            data.get(bb);
        }
    });
    emitter.setEndCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            if (ex != null) {
                ret.setComplete(ex);
                return;
            }
            try {
                ret.setComplete(bb);
            } catch (Exception e) {
                ret.setComplete(e);
            }
        }
    });
    return ret;
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataEmitter(com.koushikdutta.async.DataEmitter) DataCallback(com.koushikdutta.async.callback.DataCallback) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 39 with ByteBufferList

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

the class GZIPInputFilter method onDataAvailable.

@Override
@SuppressWarnings("unused")
public void onDataAvailable(final DataEmitter emitter, ByteBufferList bb) {
    if (mNeedsHeader) {
        final PushParser parser = new PushParser(emitter);
        parser.readByteArray(10, new ParseCallback<byte[]>() {

            int flags;

            boolean hcrc;

            public void parsed(byte[] header) {
                short magic = peekShort(header, 0, ByteOrder.LITTLE_ENDIAN);
                if (magic != (short) GZIPInputStream.GZIP_MAGIC) {
                    report(new IOException(String.format(Locale.ENGLISH, "unknown format (magic number %x)", magic)));
                    emitter.setDataCallback(new NullDataCallback());
                    return;
                }
                flags = header[3];
                hcrc = (flags & FHCRC) != 0;
                if (hcrc) {
                    crc.update(header, 0, header.length);
                }
                if ((flags & FEXTRA) != 0) {
                    parser.readByteArray(2, new ParseCallback<byte[]>() {

                        public void parsed(byte[] header) {
                            if (hcrc) {
                                crc.update(header, 0, 2);
                            }
                            int length = peekShort(header, 0, ByteOrder.LITTLE_ENDIAN) & 0xffff;
                            parser.readByteArray(length, new ParseCallback<byte[]>() {

                                public void parsed(byte[] buf) {
                                    if (hcrc) {
                                        crc.update(buf, 0, buf.length);
                                    }
                                    next();
                                }
                            });
                        }
                    });
                } else {
                    next();
                }
            }

            private void next() {
                PushParser parser = new PushParser(emitter);
                DataCallback summer = new DataCallback() {

                    @Override
                    public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                        if (hcrc) {
                            while (bb.size() > 0) {
                                ByteBuffer b = bb.remove();
                                crc.update(b.array(), b.arrayOffset() + b.position(), b.remaining());
                                ByteBufferList.reclaim(b);
                            }
                        }
                        bb.recycle();
                        done();
                    }
                };
                if ((flags & FNAME) != 0) {
                    parser.until((byte) 0, summer);
                    return;
                }
                if ((flags & FCOMMENT) != 0) {
                    parser.until((byte) 0, summer);
                    return;
                }
                done();
            }

            private void done() {
                if (hcrc) {
                    parser.readByteArray(2, new ParseCallback<byte[]>() {

                        public void parsed(byte[] header) {
                            short crc16 = peekShort(header, 0, ByteOrder.LITTLE_ENDIAN);
                            if ((short) crc.getValue() != crc16) {
                                report(new IOException("CRC mismatch"));
                                return;
                            }
                            crc.reset();
                            mNeedsHeader = false;
                            setDataEmitter(emitter);
                        //                            emitter.setDataCallback(GZIPInputFilter.this);
                        }
                    });
                } else {
                    mNeedsHeader = false;
                    setDataEmitter(emitter);
                }
            }
        });
    } else {
        super.onDataAvailable(emitter, bb);
    }
}
Also used : PushParser(com.koushikdutta.async.PushParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) IOException(java.io.IOException) DataCallback(com.koushikdutta.async.callback.DataCallback) ByteBuffer(java.nio.ByteBuffer) ParseCallback(com.koushikdutta.async.PushParser.ParseCallback) DataEmitter(com.koushikdutta.async.DataEmitter)

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