本文整理汇总了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;
}
示例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");
}
示例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, "æææ");
}
示例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());
}
示例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();
}
});
}
示例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));
}
});
}
示例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));
}
});
}
示例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)));
}
});
}
示例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));
}
});
}
示例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;
}
示例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;
}
示例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;
}
示例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]);
}
});
}
示例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();
}
}
示例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);
}