1

For example,

Channels.newChannel(url.openStream());

This one line opens the stream and gets data from it, but there is no way I can find the progress for it.

Is there a way to do so?

user2519193
  • 211
  • 2
  • 14

2 Answers2

1

This one line opens the stream and gets data from it

No, it won't until you .read() from it. Therefore...

Is there a way to do so?

Yes there is. You .read(buf) where buf is a ByteBuffer.

Just grab that buffer's .position().

(and note that Channels.newChannel(InputStream) will not return a FileChannel but a ReadableByteChannel)

fge
  • 119,121
  • 33
  • 254
  • 329
1

When using the transferFrom() or transferTo() Method, you have to pass the starting point within the file, as well as the amount of Bytes to transfer.

What you could do, is to break up the file into, say 100 "parts". This is done very simply:

File f = new File(url);
int size = f.length();

Say our file has a size of 100KB so size would now be 102400. Just divide this by the amount of "parts" (100 in this example) and we have 1024 Bytes per "part". Now all we have to do is show the percentage of "parts" which we already transfered.

As an Example

int pos = 0;
for(int x = 0 ; x < 100 /*amount of parts*/ ; x++) {
    source.transferTo(0*x, 1024 /*part size*/, destination);
    progress.setValue(x/100 /*amount of parts*/ *100);
}
AndMim
  • 550
  • 1
  • 3
  • 11