Java NIO read large file from inputstream -


i want read large inputstream , return file. need split inputstream(or should read inputstream in multiple threads). how can this? i'm trying this:

    url url = new url("path");     urlconnection connection = url.openconnection();     int filesize = connection.getcontentlength();      inputstream = connection.getinputstream();     readablebytechannel rbc1 = channels.newchannel(is);     readablebytechannel rbc2 = channels.newchannel(is);      fileoutputstream fos = new fileoutputstream("file.ext");      filechannel filechannel1 = fos.getchannel();     filechannel filechannel2 = fos.getchannel();     filechannel1.transferfrom(rbc1, 0, filesize/2);     filechannel2.transferfrom(rbc2, filesize/2, filesize/2);      fos.close(); 

but not affect on performance.

you can open multiple (http) connections same resource (url) use range: header of http make each stream begin read @ point. can speed data transfer, when high latency issue. should not overdo parallelism, aware puts additional load on server.

connection1.setrequestproperty("range", "bytes=0-" + half); connection2.setrequestproperty("range", "bytes=" + half+1 +"-"); 

this can used resume downloads. needs supported server. can announce accept-ranges: bytesbut not have . prepared first connection might return whole requested entity (status 200 vs. 206) instead.

you need read input streams urlconnections in separate threads blocking io (not sure if nio wrapping helps here).


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -