Write (byte[] b) differs from write (byte[] b,int off,int len) in Java

Source: Internet
Author: User
Tags flushes

public static void Copyinputstreamt0outputstream (InputStream in, outputstream out) {
byte[] buffer = new byte[1024];
if (null! = IN) {
try {
while (true) {
int len = 0;
if (len = in.read (buffer)) = = =-1) {
Out.flush ();
Return
}
Out.write (buffer, 0, Len);

Out.write (buffer);
}

} catch (Exception e) {
E.printstacktrace ();
}

}

}

Method One:

public int read (byte[] b)


Method Two:

public int read (byte[] b, int off,int len)
This method reads a maximum of b.length bytes of data from this input stream into a byte array

If the file is greater than 1024 bytes, read 1024 bytes at a time, then write, write, read the rest, loop the output sequentially,

Discover uploaded files, using method one is always more than 10 K greater than method two

View the JDK documentation with write (byte[] b, int off, int len) and write (byte[] b). where write (byte[] b,int off,int Len) is commented as:

Writes Len bytes from offset off in the specified byte array to this buffered output stream. In general, this method stores the bytes of the given array into the buffer of this stream, flushes the buffer as needed, and goes to the underlying output stream. However, if the requested length is at least the same as the buffer size of this stream, this method flushes the buffer and writes the individual bytes directly to the underlying output stream. Therefore, redundant bufferedoutputstream will not have to replicate the data.

Sure enough, I tried. Write (byte[] b,int off,int len) The method file is not larger. Check the source code. Finally found write (byte[]) is called write (byte[] b,int off,int len), where Len is always the length of the array.

The problem is here. When the stream is last written, Len is not generally read into the length of the bytes. Unless the file size is exactly divisible by buffer_size. while (len = in.read (buffer)) > 0) {write (byte[] b), int Off,int len), where Len is the byte length of the actual read stream. So this method does not increase the file size and will not write the extra bytes in.

I don't know why Sun added the write (byte[] b) method to Java, but the method does not affect the file

Write (byte[] b) differs from write (byte[] b,int off,int len) in Java

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.