Search in sources :

Example 1 with PushParser

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

the class ByteUtilTests method testPushParserTapUntil.

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

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    new PushParser(mock).until((byte) 0, new DataCallback.NullDataCallback()).readInt().tap(new TapCallback() {

        public void parsed(int 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) DataCallback(com.koushikdutta.async.callback.DataCallback) TapCallback(com.koushikdutta.async.TapCallback)

Example 2 with PushParser

use of com.koushikdutta.async.PushParser 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)

Example 3 with PushParser

use of com.koushikdutta.async.PushParser 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 4 with PushParser

use of com.koushikdutta.async.PushParser 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)4 PushParser (com.koushikdutta.async.PushParser)4 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)3 TapCallback (com.koushikdutta.async.TapCallback)2 DataCallback (com.koushikdutta.async.callback.DataCallback)2 DataEmitter (com.koushikdutta.async.DataEmitter)1 ParseCallback (com.koushikdutta.async.PushParser.ParseCallback)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1