vert.x - Vertx merge contents of multiple files in single file -


what best way append contents of multiple files single file in vertx? have tried vertx filesystem , asyncfile both not have option append file or did not know of any. there alternative approach merge or append files in vertx asynchronously.

the solution find make buffer list , write content on end of each previous buffer length using loop.

indeed, of vert.x 3.4, there no helper method on filesystem append file file.

you asyncfile , pump follows.

first create utility method open files:

future<asyncfile> openfile(filesystem filesystem, string path, openoptions openoptions) {   future<asyncfile> future = future.future();   filesystem.open(path, openoptions, future);   return future; } 

then 1 append file file pump:

future<asyncfile> append(asyncfile source, asyncfile destination) {   future<asyncfile> future = future.future();   pump pump = pump.pump(source, destination);   source.exceptionhandler(future::fail);   destination.exceptionhandler(future::fail);   source.endhandler(v -> future.complete(destination));   pump.start();   return future; } 

now can combine sequential composition:

void merge(filesystem filesystem, string output, list<string> sources) {   openfile(filesystem, output, new openoptions().setcreate(true).settruncateexisting(true).setwrite(true)).compose(outfile -> {     future<asyncfile> mergefuture = null;     (string source : sources) {       if (mergefuture == null) {         mergefuture = openfile(filesystem, source, new openoptions()).compose(sourcefile -> {           return append(sourcefile, outfile);         });       } else {         mergefuture = mergefuture.compose(v -> {           return openfile(filesystem, source, new openoptions()).compose(sourcefile -> {             return append(sourcefile, outfile);           });         });       }     }     return mergefuture;   }).sethandler(ar -> {     system.out.println("done");   }); } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -