In java, the difference between write (byte [] B) and write (byte [] B, int off, int len) is: bytelen

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.