Java IO readers - how do they work? -


i'm working on project need read several files server. wondering if can read output , connect somewhere else same instance. apparently can (see below).

tried pseudocode:

c.connect(google) bufferedreader r1 = ... c.getinputstream();  c.connect(somewhereelse) bufferedreader r2 = ... c.getinputstream(); print(r1) print(r2) 

and output correct.

i don't know how io streams works or conn function returns (i'll check during winter evenings :-))

real question: can rely on fact i'll correct data? i.e. buffered reader keep reference data object not anymore bound connection itself?

no, think can't trust code. inputstream needs connection fetch data. example, if open java.net.socketinputstream (url connections use stream) , see read method, see such code:

    // connection reset     if (impl.isconnectionreset()) {         throw new socketexception("connection reset");     } 

a bit lower can see, stream tries fetch buffered data if exists:

    /*      * receive "connection reset" there may bytes still      * buffered on socket      */     if (gotreset) {...} 

and lower checking connections lost:

   /*     * if here @ eof, socket has been closed,     * or connection has been reset.     */     if (impl.isclosedorpending()) {         throw new socketexception("socket closed");     } 

about working implementation:

  • first guess did not close first connection. linking c connection did not close previous one.
  • second guess data buffered. connection or buffer reader.

i wrong in thoughts, seams true @ first sight.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -