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