Search in sources :

Example 1 with StringParser

use of com.koushikdutta.async.parser.StringParser 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 2 with StringParser

use of com.koushikdutta.async.parser.StringParser 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 3 with StringParser

use of com.koushikdutta.async.parser.StringParser in project AndroidAsync by koush.

the class FileTests method testFileDataEmitter.

public void testFileDataEmitter() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    File f = getContext().getFileStreamPath("test.txt");
    StreamUtility.writeFile(f, "hello world");
    FileDataEmitter fdm = new FileDataEmitter(AsyncServer.getDefault(), f);
    final Md5 md5 = Md5.createInstance();
    Future<String> stringBody = new StringParser().parse(fdm).setCallback(new FutureCallback<String>() {

        @Override
        public void onCompleted(Exception e, String result) {
            semaphore.release();
        }
    });
    fdm.resume();
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals("hello world", stringBody.get());
}
Also used : StringParser(com.koushikdutta.async.parser.StringParser) Semaphore(java.util.concurrent.Semaphore) File(java.io.File) FileDataEmitter(com.koushikdutta.async.FileDataEmitter)

Aggregations

StringParser (com.koushikdutta.async.parser.StringParser)3 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)2 FileDataEmitter (com.koushikdutta.async.FileDataEmitter)1 File (java.io.File)1 Semaphore (java.util.concurrent.Semaphore)1