当前位置: 首页>>代码示例>>Java>>正文


Java Future类代码示例

本文整理汇总了Java中com.koushikdutta.async.future.Future的典型用法代码示例。如果您正苦于以下问题:Java Future类的具体用法?Java Future怎么用?Java Future使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Future类属于com.koushikdutta.async.future包,在下文中一共展示了Future类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: connectSocket

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
public Cancellable connectSocket(final InetSocketAddress remote, final ConnectCallback callback) {
    if (!remote.isUnresolved())
        return connectResolvedInetSocketAddress(remote, callback);

    final SimpleFuture<AsyncNetworkSocket> ret = new SimpleFuture<AsyncNetworkSocket>();

    Future<InetAddress> lookup = getByName(remote.getHostName());
    ret.setParent(lookup);
    lookup
    .setCallback(new FutureCallback<InetAddress>() {
        @Override
        public void onCompleted(Exception e, InetAddress result) {
            if (e != null) {
                callback.onConnectCompleted(e, null);
                ret.setComplete(e);
                return;
            }

            ret.setComplete(connectResolvedInetSocketAddress(new InetSocketAddress(result, remote.getPort()), callback));
        }
    });
    return ret;
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:24,代码来源:AsyncServer.java

示例2: testString

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
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");
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:17,代码来源:ParserTests.java

示例3: testUtf8String

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
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, "æææ");
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:22,代码来源:ParserTests.java

示例4: testFileDataEmitter

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
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());
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:19,代码来源:FileTests.java

示例5: saveImage

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
public void saveImage(final Context context, String imageUrl, final String name, final String extension, final boolean share) {
    Future<?> ionRequest = fileCache.downloadFile(context, imageUrl, new FileCache.DownloadedCallback() {
        @Override
        @SuppressWarnings("deprecation")
        public void onProgress(long downloaded, long total, boolean done) {
        }

        @Override
        public void onSuccess(final File source) {
            onFileDownloaded(context, name, extension, source, share);
        }

        @Override
        public void onFail(boolean notFound) {
            Toast.makeText(context, R.string.image_open_failed, Toast.LENGTH_LONG).show();
        }
    });
}
 
开发者ID:eugenkiss,项目名称:chanobol,代码行数:19,代码来源:ImageSaver.java

示例6: parse

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
@Override
public Future<String> parse(DataEmitter emitter) {
    final String charset = emitter.charset();
    return new ByteBufferListParser().parse(emitter)
    .then(new TransformFuture<String, ByteBufferList>() {
        @Override
        protected void transform(ByteBufferList result) throws Exception {
            Charset charsetToUse = forcedCharset;
            if (charsetToUse == null && charset != null)
                charsetToUse = Charset.forName(charset);
            setComplete(result.readString(charsetToUse));
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:15,代码来源:StringParser.java

示例7: parse

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
@Override
public Future<JSONObject> parse(DataEmitter emitter) {
    return new StringParser().parse(emitter)
    .then(new TransformFuture<JSONObject, String>() {
        @Override
        protected void transform(String result) throws Exception {
            setComplete(new JSONObject(result));
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:11,代码来源:JSONObjectParser.java

示例8: parse

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
@Override
public Future<Document> parse(DataEmitter emitter) {
    return new ByteBufferListParser().parse(emitter)
    .then(new TransformFuture<Document, ByteBufferList>() {
        @Override
        protected void transform(ByteBufferList result) throws Exception {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            setComplete(db.parse(new ByteBufferListInputStream(result)));
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:13,代码来源:DocumentParser.java

示例9: parse

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
@Override
public Future<JSONArray> parse(DataEmitter emitter) {
    return new StringParser().parse(emitter)
    .then(new TransformFuture<JSONArray, String>() {
        @Override
        protected void transform(String result) throws Exception {
            setComplete(new JSONArray(result));
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:11,代码来源:JSONArrayParser.java

示例10: execute

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
public <T> SimpleFuture<T> execute(AsyncHttpRequest req, final AsyncParser<T> parser, final RequestCallback<T> callback) {
    final FutureAsyncHttpResponse cancel = new FutureAsyncHttpResponse();
    final SimpleFuture<T> ret = new SimpleFuture<T>();
    execute(req, 0, cancel, new HttpConnectCallback() {
        @Override
        public void onConnectCompleted(Exception ex, final AsyncHttpResponse response) {
            if (ex != null) {
                invoke(callback, ret, response, ex, null);
                return;
            }
            invokeConnect(callback, response);

            Future<T> parsed = parser.parse(response)
            .setCallback(new FutureCallback<T>() {
                @Override
                public void onCompleted(Exception e, T result) {
                    invoke(callback, ret, response, e, result);
                }
            });

            // reparent to the new parser future
            ret.setParent(parsed);
        }
    });
    ret.setParent(cancel);
    return ret;
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:28,代码来源:AsyncHttpClient.java

示例11: websocket

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
public Future<WebSocket> websocket(final AsyncHttpRequest req, String protocol, final WebSocketConnectCallback callback) {
    WebSocketImpl.addWebSocketUpgradeHeaders(req, protocol);
    final SimpleFuture<WebSocket> ret = new SimpleFuture<WebSocket>();
    Cancellable connect = execute(req, new HttpConnectCallback() {
        @Override
        public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
            if (ex != null) {
                if (ret.setComplete(ex)) {
                    if (callback != null)
                        callback.onCompleted(ex, null);
                }
                return;
            }
            WebSocket ws = WebSocketImpl.finishHandshake(req.getHeaders(), response);
            if (ws == null) {
                ex = new WebSocketHandshakeException("Unable to complete websocket handshake");
                if (!ret.setComplete(ex))
                    return;
            }
            else {
                if (!ret.setComplete(ws))
                    return;
            }
            if (callback != null)
                callback.onCompleted(ex, ws);
        }
    });

    ret.setParent(connect);
    return ret;
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:32,代码来源:AsyncHttpClient.java

示例12: getAllByName

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
public Future<InetAddress[]> getAllByName(final String host) {
    final SimpleFuture<InetAddress[]> ret = new SimpleFuture<InetAddress[]>();
    synchronousWorkers.execute(new Runnable() {
        @Override
        public void run() {
            try {
                final InetAddress[] result = InetAddress.getAllByName(host);
                if (result == null || result.length == 0)
                    throw new HostnameResolutionException("no addresses for host");
                post(new Runnable() {
                    @Override
                    public void run() {
                        ret.setComplete(null, result);
                    }
                });
            } catch (final Exception e) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        ret.setComplete(e, null);
                    }
                });
            }
        }
    });
    return ret;
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:28,代码来源:AsyncServer.java

示例13: getByName

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
public Future<InetAddress> getByName(String host) {
    return getAllByName(host)
    .then(new TransformFuture<InetAddress, InetAddress[]>() {
        @Override
        protected void transform(InetAddress[] result) throws Exception {
            setComplete(result[0]);
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:10,代码来源:AsyncServer.java

示例14: testProxy

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
public void testProxy() throws Exception {
        wasProxied = false;
        final AsyncServer proxyServer = new AsyncServer();
        try {
            AsyncProxyServer httpServer = new AsyncProxyServer(proxyServer) {
                @Override
                protected boolean onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                    wasProxied = true;
                    return super.onRequest(request, response);
                }
            };

            AsyncServerSocket socket = httpServer.listen(proxyServer, 0);

//            client.getSocketMiddleware().enableProxy("localhost", 5555);

            AsyncHttpGet get = new AsyncHttpGet("http://www.clockworkmod.com");
            get.enableProxy("localhost", socket.getLocalPort());

            Future<String> ret = client.executeString(get, null);
            String data;
            assertNotNull(data = ret.get(TIMEOUT, TimeUnit.MILLISECONDS));
            assertTrue(data.contains("ClockworkMod"));
            assertTrue(wasProxied);
        }
        finally {
            proxyServer.stop();
        }
    }
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:30,代码来源:HttpClientTests.java

示例15: testUpload

import com.koushikdutta.async.future.Future; //导入依赖的package包/类
public void testUpload() throws Exception {
        File dummy = getContext().getFileStreamPath("dummy.txt");
        final String FIELD_VAL = "bar";
        dummy.getParentFile().mkdirs();
        FileOutputStream fout = new FileOutputStream(dummy);
        byte[] zeroes = new byte[100000];
        for (int i = 0; i < 10; i++) {
            fout.write(zeroes);
        }
        fout.close();
//        StreamUtility.writeFile(dummy, DUMMY_VAL);
        
        AsyncHttpPost post = new AsyncHttpPost("http://localhost:5000");
        MultipartFormDataBody body = new MultipartFormDataBody();
        body.addStringPart("foo", FIELD_VAL);
        body.addFilePart("my-file", dummy);
        body.addStringPart("baz", FIELD_VAL);
        post.setBody(body);

        Future<String> ret = AsyncHttpClient.getDefaultInstance().executeString(post, new StringCallback() {
            @Override
            public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
            }
        });
        
        String data = ret.get(10000, TimeUnit.MILLISECONDS);
        assertEquals(data, FIELD_VAL + (zeroes.length * 10) + FIELD_VAL);
    }
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:29,代码来源:MultipartTests.java


注:本文中的com.koushikdutta.async.future.Future类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。