0

I am encountering with a scenario like this.

My project receives a download request from perl

void downloadRequest(FileItemIterator items,
                             HttpServletResponse response) throws Exception
{ 
log.info("Start downloadRequest.......");
OutputStream os = response.getOutputStream();
File file = new File("D:\\clip.mp4");
            FileInputStream fileIn = new FileInputStream(file);
            //while ((datablock = dataOutputStreamServiceImpl.readBlock()) != null)
            byte[] outputByte = new byte[ONE_MEGABYE];
            while (fileIn.read(outputByte) != -1)
            {

                System.out.println("--------" + (i = i + 1) + "--------");
                System.out.println(new Date());
                //dataContent = datablock.getContent();
                System.out.println("Start write " + new Date());
                os.write(outputByte);
                System.out.println("End write " + new Date());
                //System.out.println("----------------------");
            }
            os.close();
        }
    }

I try to read and write blocks of 1MB from the file. However, it takes too long for downloading the whole file. ( my case is 20mins for file of 100MB)

I try to sysout and I saw a result like this:

The first few blocks can read, write data realy fast:

--------1--------
Mon Dec 07 16:24:20 ICT 2015
Start write Mon Dec 07 16:24:20 ICT 2015
End write Mon Dec 07 16:24:21 ICT 2015
--------2--------
Mon Dec 07 16:24:21 ICT 2015
Start write Mon Dec 07 16:24:21 ICT 2015
End write Mon Dec 07 16:24:21 ICT 2015
--------3--------
Mon Dec 07 16:24:21 ICT 2015
Start write Mon Dec 07 16:24:21 ICT 2015
End write Mon Dec 07 16:24:21 ICT 2015

But the next block is slower than the previous

--------72--------
Mon Dec 07 16:29:22 ICT 2015
Start write Mon Dec 07 16:29:22 ICT 2015
End write Mon Dec 07 16:29:29 ICT 2015
--------73--------
Mon Dec 07 16:29:29 ICT 2015
Start write Mon Dec 07 16:29:29 ICT 2015
End write Mon Dec 07 16:29:37 ICT 2015

--------124--------
Mon Dec 07 16:38:22 ICT 2015
Start write Mon Dec 07 16:38:22 ICT 2015
End write Mon Dec 07 16:38:35 ICT 2015
--------125--------
Mon Dec 07 16:38:35 ICT 2015
Start write Mon Dec 07 16:38:35 ICT 2015
End write Mon Dec 07 16:38:48 ICT 2015

I realy cannot understand how the outputStream write, why it take such a long time like that? or I made some mistakes?

Sorry for my bad english. I realy need your support. Thank in advance!

MCT
  • 21
  • 5

1 Answers1

1

There is no guaranty that the read(byte[] b) method will read b.length number of bytes from the file which mean that your code could be sending more bytes then the file actually has.

For example, if you're processing a 10 MB file and the read(byte[] b) always reads b.length/2 from the file, you will be sending 20 MB.

To address that, you could do something like this.

 byte[] outputByte = new byte[ONE_MEGABYE];
 int r = -1;
 while ((r = fileIn.read(outputByte)) != -1){            
      os.write(outputByte,0,r);            
 }

This will ensure that you'll be sending only as much bytes as there were read from the file.

Beside this issue, the speed can be influenced by a lot of other factors like the internet speed or the other program's implementation.

Titus
  • 22,031
  • 1
  • 23
  • 33
  • thank you that a good point but the performance is still the same. About the network, everything now is on my local, do you have any other idea? :( – MCT Dec 10 '15 at 07:36
  • @MCT try to change the buffer's size [here you can find more info about that](http://stackoverflow.com/questions/236861/how-do-you-determine-the-ideal-buffer-size-when-using-fileinputstream) also, you should consider using buffered streams like `BufferedInputStream` and `BufferedOutputStream`. – Titus Dec 10 '15 at 14:04