Y
In @zRrr, the following came out:Range Class, which develops a list of ranges with different data release options class Range
{
public List<String> rangeList = new ArrayList<>();
Range(int startBytes, int endBytes, int c_thread)
{
rangeList = getThreadsDiapason(startBytes,endBytes,c_thread);
}
public String getRangeString(int rangeId)
{
if(rangeList.size() < rangeId)
return null;
else
return rangeList.get(rangeId);
}
public int getFirstRangeInt(int rangeId)
{
if(rangeList.size() < rangeId)
return 0;
else
return Integer.parseInt(rangeList.get(rangeId).split("-")[0]);
}
public int getLastRangeInt(int rangeId)
{
if(rangeList.size() < rangeId)
return 0;
else
return Integer.parseInt(rangeList.get(rangeId).split("-")[1]);
}
private List<String> getThreadsDiapason(int startBytes, int endBytes, int c_thread)
{
int step = (int)(endBytes / c_thread);
List<Integer> temp_start = new ArrayList<>();
List<Integer> temp_end = new ArrayList<>();
temp_start.add(startBytes);
temp_end.add(step);
try
{
for(int i = 1; i < c_thread; i++)
{
temp_start.add(i,temp_end.get(i - 1) + 1);
temp_end.add(i,temp_end.get(i - 1) + step);
}
}
catch (Exception ex)
{
Log.e("getThreadsDiapason", ex.getMessage());
}
int last_value = temp_end.get(c_thread-1) + (endBytes - temp_end.get(c_thread-1));
temp_end.set(c_thread-1,last_value);
List<String> finalList = new ArrayList<>();
try
{
for(int i = 0; i < c_thread; i++)
{
finalList.add(temp_start.get(i)+"-"+temp_end.get(i));
}
}
catch (Exception ex)
{
Log.e("getThreadsDiapason",ex.getMessage());
}
return finalList;
}
}
And threadedDownload using RandomAccessFile: public void threadedDownload(final String url,final String path,final int size, int threads_count) throws Exception
{
final int stock_buffer = 4096 * threads_count;
final Range range = new Range(0,size,threads_count);
final List<Thread> threadList = new ArrayList<>();
for(int i = 0; i < threads_count; i++)
{
final int diap_i = i;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
byte[] buffer = new byte[stock_buffer];
int bytesRead = -1;
try
{
System.out.println("Thread "+diap_i+" Started : " + range.getRangeString(diap_i));
HttpURLConnection conn = null;
RandomAccessFile randomAccessFile = null;
try {
conn = (HttpURLConnection)new URL(url).openConnection();
} catch (IOException e) {
Log.e("threadedDownload",e.getMessage());
}
conn.setDoInput(true);
conn.setConnectTimeout(100000);
conn.setRequestProperty("Range", "bytes="+range.getRangeString(diap_i));
randomAccessFile = new RandomAccessFile(path, "rw");
randomAccessFile.seek(range.getFirstRangeInt(diap_i));
conn.connect();
InputStream inputStream = conn.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1)
{
randomAccessFile.write(buffer, 0, bytesRead);
}
randomAccessFile.close();
System.out.println("Thread "+diap_i+" Ended : " + range.getRangeString(diap_i));
}catch (Exception ex)
{
Log.e("Error",ex.getMessage());
}
}
});
threadList.add(t);
}
final List<Thread> finalThreadList = threadList;
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < finalThreadList.size(); i++)
{
finalThreadList.get(i).start();
}
}
}).start();
}