tar.gz file download using vert.x Pump API

359 views
Skip to first unread message

Naveen Kumar

unread,
Nov 16, 2014, 10:40:50 AM11/16/14
to ve...@googlegroups.com
Hi All,

Below is my code to download a file from remote server.
Based on the URL, either a text file or tar.gz file will be downloaded. I am trying to make an abstract implementation to download any type of file.
However, the below code is working fine for the txt file; but tar.gz are corrupted.
Can somebody help me to fix the issue?

httpClient.get(downloadUrl, new Handler<HttpClientResponse>() {
public void handle(final HttpClientResponse httpEvent) {
//pause the http response till we complete setting up our
//async file handler
httpEvent.pause();
//setup file open handler
vertx.fileSystem().open(target, null, false, true, true, true, new Handler<AsyncResult<AsyncFile>>() {
public void handle(AsyncResult<AsyncFile> fileEvent) {
if(fileEvent.failed()){
fileEvent.cause().printStackTrace();
return;
}
final AsyncFile asynFile = fileEvent.result();
final Pump downloadPump = Pump.createPump(httpEvent, asynFile);
downloadPump.start();
httpEvent.endHandler(new Handler<Void>() {
public void handle(Void event) {
//close the file
asynFile.flush().close(new Handler<AsyncResult<Void>>() {
public void handle(AsyncResult<Void> event) {
System.out.println("Is file close ok? = " + event.failed());
}
});
System.out.println("File download operation complets: Download size = " 
+ downloadPump.bytesPumped());
}
});
}
});
//resume the receive operation
httpEvent.resume();
}
}).exceptionHandler(new Handler<Throwable>() {
public void handle(Throwable event) {
System.out.println(event);
}
}).end();

Alexander Lehmann

unread,
Nov 16, 2014, 5:46:12 PM11/16/14
to ve...@googlegroups.com
It is much easier if you post code examples as complete classes and on gist, in addition gist supports diffs and pull requests (I think).

Having said that, the issue is that you are loosing a bit of the file at the beginning since you are starting the httpStream too early, when you start the stream at the point where you have created the Pump, it works.

https://gist.github.com/alexlehm/b5726ded353eaafd14f4/revisions
Reply all
Reply to author
Forward
0 new messages