1

I'm receiving a file using dataInputStream.readFully, so i'd like to show the progress of the transmision. Is it possible to know the progress of readFully method? Thank you.

MM Manuel
  • 375
  • 2
  • 4
  • 16
  • No. To track progress, you need to do what it does. Look at the source: http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/io/DataInputStream.java#l172 – Andreas Apr 24 '16 at 15:07

1 Answers1

1

No - the whole point is that it's a single call to make it simpler to read.

If you want to read more gradually (e.g. to update a progress bar) you can just read a chunk at a time with InputStream.read(byte[], int, int) in a loop until you've read all the data. For example:

byte[] data = new byte[bytesToRead];
int offset = 0;
int bytesRead;
while ((bytesRead = stream.read(data, offset, data.length - offset)) > -1) {
    offset += bytesRead;
    // Update progress indicator
}

This is just like the JDK 8 code for readFully, but with progress indication in the loop.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194