内部的缓冲byte[]buffer,定义的大小为4096,如果要写的io流内容超过这个大小呢
贴个源码:
public static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}
public static long copyLarge(InputStream input, OutputStream output)
throws IOException {
return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]);//大小为4096
}
public static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
throws IOException {
long count = 0;
int n = 0;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
也没看见对buffer有什么别的处理呀?如果buffer大小不够呢?
The key is here
while (EOF != (n = input.read(buffer)))
This is what the documentation says:
That is to say, a maximum of 4096 bytes can be read each time. If more than 4096 bytes are read, the while loop will read multiple times
How big do you think the buffer should be? If you want to copy a file of several hundred MB, does the buffer also need to be several hundred MB in size?
The buffer is like a cart that travels to and from the I/O side to transport data. Its function is to reduce the number of transactions and reduce overhead.