8192 bytes when creating file

2020-06-18 04:32发布

问题:

In my Java code I have function that gets file from the client in http request and converts that in to the file. I have this line there:

byte[] buffer = new byte[8192];

what does 8192 bytes (8 kb) means here?

This is one of the responses that I got, and want to make sure that I understand that code.

回答1:

That it uses a buffer to read and write 8kB blocks at once. The number is fairly arbitary, but for performance reasons it makes sense to use a multiple of 512 bytes when writing a file, and preferably a multiple of the disks cluster size. 8kB is a reasonable buffer size for most purposes.



回答2:

This is the size of the array of bytes, meaning that your buffer will hold 8192 bytes at a time.



回答3:

If I had to guess, that is the amount of space you are using to read in the file. Without the rest of the code I can't tell if it is trying to read it all and cram it into 8k or if it is reading it in, 8k at a time, and then dumping it into the file.



回答4:

8192 is maximum size of a package sending via network. char buffer[8192]; /* single packets are usually not bigger than 8192 bytes */ 512 bytes is too small.