I want to read the first x bytes from a java.net.URLConnection (although I’m not forced to use this class – other suggestions welcome).
My code looks like this:
val head = new Array[Byte](2000)
new BufferedInputStream(connection.getInputStream).read(head)
IOUtils.toString(new ByteArrayInputStream(head), charset)
It works, but does this code load only the first 2000 bytes from the network?
Next trial
As ‘JB Nizet’ said it is not useful to use a buffered input stream, so I tried it with an InputStreamReader:
val head = new Array[Char](2000)
new InputStreamReader(connection.getInputStream, charset).read(head)
new String(head)
This code may be better, but the load times are about the same. So does this procedure limit the transferred bytes ?
You can use
read(Reader, char[])from Apache Commons IO. Just pass a 2000-character buffer to it and it will fill it with as many characters as possible, up to 2000.Be sure you understand the objections in the other answers/comments, in particular:
Buffered...wrappers, it goes against your intentions.Readerto read 2000 characters instead ofInputStreamreading 2000 bytes. The proper procedure would be to determine the character encoding from the headers of a response (Content-Type) and set that encoding intoInputStreamReader.read(char[])on aReaderwill not fully fill the array you give to it. It can read as little as one character no matter how big the array is!Other than that, I’d strongly recommend you to use Apache HttpClient in favor of
java.net.URLConnection. It’s much more flexible.Edit: To understand the difference between
Reader.readandIOUtils.read, it’s worth examining the source of the latter:Since
Reader.readcan read less characters than a given length (we only know it’s at least 1 and at most the length), we need to iterate calling it until we get the amount we want.