Sunday 15 September 2013

java - FileOutputStream.close is really slow when writing large file -


I have a method that receives the file on a TCP socket using this code:

  FileOutputStream file stream = new FileOutputStream (filename.getName ()); While (total read ) {if (size - totalRead> CHUNKSIZE) {read = getInputStream () Read (buffer, 0, CHUNKSIZE); } Else {read = getInputStream () Read (buffer, 0, size - total read); } TotalRead + = reading; FileStream.write (buffer, 0, reading); FileStream.flush (); If (System.currentTimeMillis ()> Next Print) {nextPrint + = 1000; Int speed = (int) (total read / (system quartime milliseconds) - starttime); Double content = ((double) total read / size) * 100; Gui.setStatus ("Receiving:" + filename + "" + Speed ​​+ "KB / S," + + "+"% Completed ");}} Gui.setStatus (" Resizing: "+ Filename +" Completed. "); FileStream.close ();   

FileOutputStream.close is actually taking a long time when large files are received, why is it? As you are seeing that I Flush the stream on the received piece.

Depending on the OS, the flush () More data on the OS In the case of FileOutputStream, writes () passes all the data on the OS, so flush () does not do anything where close () can ensure that the file is actually Written on the disk (or not dependent on the OS) you can see that the disk is still busy while writing data.

500 MB files that take up to 30 seconds means that you have 17 Writing MB / S. It is like a very slow disk or That's the file on the network share / drive.


You can try it

  file file = file. CreateTemplate file ("Deleteme", "dat"); // Put your file here FileOutputStream fos = New FileOutputStream (file); Long start = System.nanoTime (); Byte [] bytes = new byte [32 * 1024]; For (long L = 0; L & L; 500 * 1000 * 1000; L + = bytes. Lamps) fos.write (bytes); Long Middle = System.nanoTime (); To write System.out.printf ("%, D bytes% n" takes% .3f sec, (middle start) / 1e 9, file. Length ()); Fos.close (); Long end = System.nanoTime (); System.out.printf ("took% .3 seconds to close% n", (end-middle) / 1e 9);   

print

  to write 500,006,912 bytes took 0.116 seconds to close 0.002 seconds   

you see The speed is that the data on this system is not even writing on a close i.e. this drive is not fast.

No comments:

Post a Comment