XorOrNor
XorOrNor

Reputation: 8978

How does InputStream buffer work?

I'm using a InputStream buffer as following. I'd like to know when it is actually buffering (filling itself with data)... I'm feeding it with internet stream. I put Logs before and after len = in.read(buffer) but they are logged in the same time (so the process is not here).

    conn = new URL(StringUrls[0]).openConnection();
    conn.setReadTimeout(5000);
    conn.setConnectTimeout(5000);
    in = conn.getInputStream();
    int len=-1;
    buffer = new byte[1024];

    Log.v("buffer", "buffering...");

    len = in.read(buffer);

    Log.v("buffer", "buffered");

Upvotes: 2

Views: 301

Answers (1)

pisek
pisek

Reputation: 1439

http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[]) states:

This method blocks until input data is available, end of file is detected, or an exception is thrown.

In other words, it is being filled when it has the data to be filed with. When you open a connection, the data are ready to be read. That is why InputStream does not have to wait for anything.

Upvotes: 2

Related Questions