Created
May 24, 2012 14:37
-
-
Save steos/2781943 to your computer and use it in GitHub Desktop.
test apache httpasyncclient download progress (with mpcorb dataset)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import java.util.concurrent.Future; | |
import java.util.zip.GZIPInputStream; | |
import org.apache.http.Header; | |
import org.apache.http.HttpException; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.impl.nio.client.DefaultHttpAsyncClient; | |
import org.apache.http.nio.IOControl; | |
import org.apache.http.nio.client.HttpAsyncClient; | |
import org.apache.http.nio.client.methods.AsyncByteConsumer; | |
import org.apache.http.nio.client.methods.HttpAsyncMethods; | |
import org.apache.http.protocol.HttpContext; | |
public class TestAsyncHttpClientDownloadProgress { | |
private static class Consumer extends AsyncByteConsumer<Boolean> { | |
private long size; | |
private long received; | |
private ByteArrayOutputStream out; | |
public Consumer() throws IOException { | |
out = new ByteArrayOutputStream(); | |
} | |
@Override | |
protected void onByteReceived(ByteBuffer buf, IOControl ioctrl) | |
throws IOException { | |
while (buf.hasRemaining()) { | |
out.write(buf.get()); | |
received++; | |
} | |
System.out.print("received "); | |
System.out.print(received / (size/100)); | |
System.out.println("% received"); | |
} | |
@Override | |
protected Boolean buildResult(HttpContext arg0) throws Exception { | |
return Boolean.TRUE; | |
} | |
@Override | |
protected void onResponseReceived(HttpResponse res) | |
throws HttpException, IOException { | |
for (Header h : res.getAllHeaders()) { | |
System.out.print(h.getName()); | |
System.out.print(": "); | |
System.out.println(h.getValue()); | |
} | |
System.out.println("----------------------"); | |
size = Long.parseLong(res.getHeaders("Content-Length")[0].getValue()); | |
} | |
public void printit() throws IOException { | |
out.close(); | |
GZIPInputStream in = new GZIPInputStream( | |
new ByteArrayInputStream(out.toByteArray())); | |
int bite = -1; | |
while (-1 != (bite = in.read())) { | |
System.out.print((char)bite); | |
} | |
in.close(); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
HttpAsyncClient http = new DefaultHttpAsyncClient(); | |
http.start(); | |
System.out.println("fetching dataset ... "); | |
Consumer consumer = new Consumer(); | |
Future<Boolean> future = http.execute(HttpAsyncMethods.createGet( | |
"http://www.minorplanetcenter.net/iau/MPCORB/MPCORB.DAT.gz"), | |
consumer, null); | |
Boolean result = future.get(); | |
if (result != null && result.booleanValue()) { | |
System.out.println("----------------------"); | |
consumer.printit(); | |
} else { | |
System.out.println("failure"); | |
} | |
http.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment