When I want to write the full contents of a file into an OutputStream
, I usually allocate a buffer as a byte[]
, then make a for
loop to read
data from the file's InputStream
into the buffer and write the buffer contents into the OutputStream
, until the InputStream
has no more bytes available.
This seems rather clumsy to me. Is there a better way to do this?
Also, I am always unsure about the buffer size. Usually, I am allocating 1024 bytes, because it just feels good. Is there a better way to determine a reasonable buffer size?
In my current case, I want to copy the full contents of a file into the output stream that writes the contents of an HTTP response. So, this is not a question about how to copy files on the file system.