Search in sources :

Example 11 with ByteBufferList

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

the class DnsResponse method parse.

public static DnsResponse parse(ByteBufferList bb) {
    ByteBuffer b = bb.getAll();
    bb.add(b.duplicate());
    // naive parsing...
    bb.order(ByteOrder.BIG_ENDIAN);
    // id
    bb.getShort();
    // flags
    bb.getShort();
    // number questions
    int questions = bb.getShort();
    // number answer rr
    int answers = bb.getShort();
    // number authority rr
    int authorities = bb.getShort();
    // number additional rr
    int additionals = bb.getShort();
    for (int i = 0; i < questions; i++) {
        parseName(bb, b);
        // type
        bb.getShort();
        // class
        bb.getShort();
    }
    DnsResponse response = new DnsResponse();
    for (int i = 0; i < answers; i++) {
        String name = parseName(bb, b);
        // type
        int type = bb.getShort();
        // class
        int clazz = bb.getShort();
        // ttl
        int ttl = bb.getInt();
        // length of address
        int length = bb.getShort();
        try {
            if (type == 1) {
                // data
                byte[] data = new byte[length];
                bb.get(data);
                response.addresses.add(InetAddress.getByAddress(data));
            } else if (type == 0x000c) {
                response.names.add(parseName(bb, b));
            } else if (type == 16) {
                ByteBufferList txt = new ByteBufferList();
                bb.get(txt, length);
                response.parseTxt(txt);
            } else {
                bb.get(new byte[length]);
            }
        } catch (Exception e) {
        //                e.printStackTrace();
        }
    }
    // authorities
    for (int i = 0; i < authorities; i++) {
        String name = parseName(bb, b);
        // type
        int type = bb.getShort();
        // class
        int clazz = bb.getShort();
        // ttl
        int ttl = bb.getInt();
        // length of address
        int length = bb.getShort();
        try {
            bb.get(new byte[length]);
        } catch (Exception e) {
        //                e.printStackTrace();
        }
    }
    // additionals
    for (int i = 0; i < additionals; i++) {
        String name = parseName(bb, b);
        // type
        int type = bb.getShort();
        // class
        int clazz = bb.getShort();
        // ttl
        int ttl = bb.getInt();
        // length of address
        int length = bb.getShort();
        try {
            if (type == 16) {
                ByteBufferList txt = new ByteBufferList();
                bb.get(txt, length);
                response.parseTxt(txt);
            } else {
                bb.get(new byte[length]);
            }
        } catch (Exception e) {
        //                e.printStackTrace();
        }
    }
    return response;
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) ByteBuffer(java.nio.ByteBuffer)

Example 12 with ByteBufferList

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

the class DnsResponse method parseName.

private static String parseName(ByteBufferList bb, ByteBuffer backReference) {
    bb.order(ByteOrder.BIG_ENDIAN);
    String ret = "";
    int len;
    while (0 != (len = bb.get() & 0x00FF)) {
        // compressed
        if ((len & 0x00c0) == 0x00c0) {
            int offset = ((len & ~0xFFFFFFc0) << 8) | (bb.get() & 0x00FF);
            if (ret.length() > 0)
                ret += ".";
            ByteBufferList sub = new ByteBufferList();
            ByteBuffer duplicate = backReference.duplicate();
            duplicate.get(new byte[offset]);
            sub.add(duplicate);
            return ret + parseName(sub, backReference);
        }
        byte[] bytes = new byte[len];
        bb.get(bytes);
        if (ret.length() > 0)
            ret += ".";
        ret += new String(bytes);
    }
    return ret;
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) ByteBuffer(java.nio.ByteBuffer)

Example 13 with ByteBufferList

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

the class GsonTests method testParserCastingCallbackError.

public void testParserCastingCallbackError() throws Exception {
    ByteBufferList b = new ByteBufferList(ByteBuffer.wrap("[]".getBytes()));
    FilteredDataEmitter emitter = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    GsonObjectParser g = new GsonObjectParser();
    Future<JsonObject> ret = g.parse(emitter);
    emitter.onDataAvailable(emitter, b);
    emitter.getEndCallback().onCompleted(null);
    final Semaphore s = new Semaphore(0);
    ret.setCallback(new FutureCallback<JsonObject>() {

        @Override
        public void onCompleted(Exception e, JsonObject result) {
            assertNull(result);
            assertNotNull(e);
            assertTrue(e instanceof ClassCastException);
            s.release();
        }
    });
    s.acquire();
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) GsonObjectParser(com.koushikdutta.ion.gson.GsonObjectParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) JsonObject(com.google.gson.JsonObject) Semaphore(java.util.concurrent.Semaphore) JsonParseException(com.google.gson.JsonParseException) ExecutionException(java.util.concurrent.ExecutionException)

Example 14 with ByteBufferList

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

the class GsonTests method testParserCastingError.

public void testParserCastingError() throws Exception {
    ByteBufferList b = new ByteBufferList(ByteBuffer.wrap("[]".getBytes()));
    FilteredDataEmitter emitter = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    GsonObjectParser g = new GsonObjectParser();
    Future<JsonObject> ret = g.parse(emitter);
    emitter.onDataAvailable(emitter, b);
    emitter.getEndCallback().onCompleted(null);
    try {
        JsonObject j = ret.get();
        fail(j.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) GsonObjectParser(com.koushikdutta.ion.gson.GsonObjectParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) ExecutionException(java.util.concurrent.ExecutionException)

Example 15 with ByteBufferList

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

the class RequestBodyUploadObserver method write.

@Override
public void write(AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
    final int length = body.length();
    body.write(request, new DataSink() {

        int totalWritten;

        @Override
        public void write(ByteBufferList bb) {
            int start = bb.remaining();
            sink.write(bb);
            int wrote = start - bb.remaining();
            totalWritten += wrote;
            callback.onProgress(totalWritten, length);
        }

        @Override
        public void setWriteableCallback(WritableCallback handler) {
            sink.setWriteableCallback(handler);
        }

        @Override
        public WritableCallback getWriteableCallback() {
            return sink.getWriteableCallback();
        }

        @Override
        public boolean isOpen() {
            return sink.isOpen();
        }

        @Override
        public void end() {
            sink.end();
        }

        @Override
        public void setClosedCallback(CompletedCallback handler) {
            sink.setClosedCallback(handler);
        }

        @Override
        public CompletedCallback getClosedCallback() {
            return sink.getClosedCallback();
        }

        @Override
        public AsyncServer getServer() {
            return sink.getServer();
        }
    }, completed);
}
Also used : DataSink(com.koushikdutta.async.DataSink) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncServer(com.koushikdutta.async.AsyncServer) WritableCallback(com.koushikdutta.async.callback.WritableCallback)

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