4

It is very interesting for me where all data is stored until I read request body.

For example, a file is uploading to the server. The Java program receives this file. It is impossible to store whole file content in buffers if file is very big - 100 GB.

Does Java streams this file from remote computer? I mean remote computer sends small part of data, Java receives this part and waits for next part. When remote computer decided that server read first part it sends second part of data and so on.

Does Java and its HttpServer works in this way or it stores whole file on the disk as Apache+PHP do?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Nicolai
  • 5,489
  • 1
  • 24
  • 31
  • what do you mean by java and its HttpServer?! which server are you using? and by the way, this is really non-logical to buffer whole stream! –  Oct 11 '13 at 19:57
  • Hi, if I implement HTTP server, like this one described in topic http://stackoverflow.com/questions/3732109/simple-http-server-in-java-using-only-java-se-api . When I receive an request for file upload, how does this work in low level. Where is placed file content until I read whole t.getRequestBody()? – Nicolai Oct 12 '13 at 07:32
  • it goes for memory until JVM throws out of memory exception, you know, the developer(you) should take care about how does this data need to manipulated, you would get the input stream and read the data as buffer, and in other word, the input stream reading methods DOES NOT block the thread if some(required) data is available –  Oct 12 '13 at 10:42

1 Answers1

1

The mechanism you're looking for is implemented by the TCP stack of the operating system. Buffers are used both on the sending and the receiving side.

TCP generally works like the receiving machine replies to the sender that "OK, got it, now send the next part" - also known as an ACK packet. This mechanism is also resposible to adjust transfer speed to the speed of your connection (instead of sending data too fast and resulting in packet loss).

It is a well-oiled machine, but if something goes wrong it's usually manifested by a TIMEOUT. (In your example if you are waiting a lot of time before processing the request body, and not reading, the sending machine will just give up).

vbence
  • 20,084
  • 9
  • 69
  • 118
  • The sending machine will block, not just give up. – user207421 Mar 16 '16 at 21:34
  • @EJP Two things can happen, neither of those involve considerable blocking time: **A)** If the client just unplugs the network cable, the server won't get any ACKs and SO_SNDBUF will get full quickly (e.g. instantly). At this point the OS (TCP stack) will tear down the connection. **B)** If the client application does not read the data SO_RCVBUF will get full (on the client side) and the client tears down the TCP channel. – vbence Mar 17 '16 at 09:40