In java, the difference between write (byte [] B) and write (byte [] B, int off, int len) is: bytelen
Upload files or images to a project
Private static final int BUFFER_SIZE = 16*1024;
Private static void copy (File src, File dst ){
Try {
InputStream in = null;
OutputStream ut = null;
Try {
In = new BufferedInputStream (new FileInputStream (src), BUFFER_SIZE );
Ut = new BufferedOutputStream (new FileOutputStream (dst), BUFFER_SIZE );
Byte [] buffer = new byte [BUFFER_SIZE];
While (in. read (buffer)> 0 ){
Out. write (buffer );
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
If (null! = In ){
In. close ();
}
If (null! = Out ){
Out. close ();
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
File Upload is simple, but suddenly I found that the uploaded files are larger than a dozen K. because the number of users in the system is large, if there are 1 million images, the space occupied can be very large. view the jdk documentation, where the BufferedOutputStream documentation contains write (byte [] B, int off, int len) and write (byte [] B ). write (byte [] B, int off, int len) comments:
Write the len bytes starting from offset off in the specified byte array to the output stream of this buffer. In general, this method stores the bytes of the given array into the buffer zone of the stream, refreshes the buffer zone as needed, and forwards it to the underlying output stream. However, if the request length is at least the same as the buffer size of the stream, this method refreshes the buffer and writes each byte directly to the underlying output stream. Therefore, the redundant BufferedOutputStream does not have to be copied.
Sure enough, I tried the write (byte [] B, int off, int len) method file. due to habits, follow up to see what is going on. finally, we found that write (byte []) calls write (byte [] B, int off, int len), where the length of len array. the problem lies here. in the last write stream, len is generally not the length of the read bytes. unless the file size is divisible by BUFFER_SIZE. while (len = in. read (buffer)> 0) {write (byte [] B, int off, int len), in which len is the length of the bytes actually read into the stream. therefore, this method will not increase the file size, and will not write redundant bytes into it.
I don't know why sun added the write (byte [] B) method in Java, but this method does not affect the file. When I use MagickImage to process one, the file will be restored to its original state.