0

I am using java nio filechannel transferFrom function along with Apache httpclient to download files from internet. Its starts normally but cpu usage suddenly grows after some time. And download speed decreases and eventually becomes zero.

try (CloseableHttpResponse response = client.execute(get);
                    ReadableByteChannel inputChannel = Channels.newChannel(
                            response.getEntity().getContent())) {

                while (start < end && currentState.get() == 1) {
                    delta = fileChannel.transferFrom(inputChannel, start, 8192);
                    start += delta;
                    bytesDone.addAndGet(delta);
                    intialState.set(name, start);
                }
            }

Sry cant post images due to low reputation

  • You need to stop when `delta == 0` even if the transfer isn't complete. – user207421 Aug 31 '14 at 09:41
  • @EJP your solution did reduced my cpu usage but now file download is not completing. For larger files after some time the speed drop to zero I think thats because all threads gets completed but bytesDownloaded – Mohsin Niazi Sep 01 '14 at 07:55

0 Answers0