I\'m trying to upload a jpeg image buffer I got back from the Camera. I know the length, but if I pass just that length to the setFixedLengthStreamingMode() routine I get er
The URLConnection
buffers by default everything which is been written to its getOutputStream()
in client's memory until it's closed. This is mandatory because the HTTP Content-Length
response header needs to be set. But the content length is only known once all bytes have been written. This may be memory hogging when the response body is relatively large.
If you know beforehand the exact amount of bytes (note: bytes, not characters) being written, then you could use setFixedLengthStreamingMode()
to set it with exactly that amount of bytes, so that the Content-Length
header could be set much sooner and so that URLConnection
can flush more often. In your particular case, you apparently used the wrong value.
The setChunkedStreamingMode()
basically changes the transfer encoding to chunked
. This is basically one line with the byte length in hexcode and then one line of written bytes therafter and then a blank line (see also wikipedia). The last line has a byte length of 0 and so the server knows when it's the end of the body so that it doesn't have to wait for any incoming data. This allows the response body being flushed more often. You should only not set it with 0, but with a sensible value, such as 1000 or so. This way every 1000 written bytes will be sent as a new chunk.