7

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.

Community
  • 1
  • 1
Maksim
  • 16,635
  • 27
  • 94
  • 135

4 Answers4

12

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.

Thorarin
  • 47,289
  • 11
  • 75
  • 111
2

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

Saulo Silva
  • 1,219
  • 1
  • 20
  • 37
1

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.

Edward Kmett
  • 29,632
  • 7
  • 85
  • 107
  • Check this answer: http://stackoverflow.com/questions/1111130/basic-file-upload-in-gwt/1111606#1111606 This is where I have that number – Maksim Jul 10 '09 at 20:01
  • 4
    Then it is reading in 8k at a time and sending that over to the file before grabbing the next 8k chunk. The justification is that 8k is a reasonable trade off between spinning your wheels calling functions, and wasting space when you get to the end of the file, because and it is reasonably close to the block size on many file systems. – Edward Kmett Jul 10 '09 at 20:05
  • How could we answer that when you show us no source code except for the array declaration/definition? – Ed S. Jul 10 '09 at 20:07
0

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.

nobugz
  • 25
  • 1
  • 5
    Can you provide some **references** about that claim: "`8192 is maximum size of a package sending via network`"? – Paul Vargas Jun 13 '16 at 17:38
  • 2
    That comment seems to have been copied from [*A brief programming tutorial in C for raw sockets*](http://www.cs.binghamton.edu/~steflik/cs455/rawip.txt), and misinterpreted as the "maximum size of a package[sic]," [which it is not](https://stackoverflow.com/questions/2613734). – ZachB Oct 16 '18 at 07:30