socket and bufferedreader close()

Discussion in 'Java' started by -, Apr 6, 2005.

  1. -

    - Guest

    i have a

    socket = ...
    br = new BufferedReader(...


    when i close, should i close both br and socket? if so, in which order?
    if not, which one should be closed?
     
    -, Apr 6, 2005
    #1
    1. Advertising

  2. Hi,

    You should obviously close the reader first, because if the stream it is
    reading from is closed ,it will probably get you IOException .

    You should also close the socket.

    Regards,

    Arnaud


    "-" <> a écrit dans le message news:
    425347d9$...
    > i have a
    >
    > socket = ...
    > br = new BufferedReader(...
    >
    >
    > when i close, should i close both br and socket? if so, in which order?
    > if not, which one should be closed?
     
    Arnaud Berger, Apr 6, 2005
    #2
    1. Advertising

  3. - wrote:
    > i have a
    >
    > socket = ...
    > br = new BufferedReader(...
    >
    >
    > when i close, should i close both br and socket? if so, in which order?
    > if not, which one should be closed?


    Closing the BufferedReader should also close the underlying Reader, which
    should close the underlying InputStream which should close the underlying Socket.

    But if you also called the socket's getOutputStream() method, you should also
    close that stream (or any wrappers you put round it) to avoid leaving an open
    InputStream / file handle. I think that closing the socket doesn't close the
    input and output streams above it.

    Steve
     
    Steve Horsley, Apr 6, 2005
    #3
  4. -

    Esmond Pitt Guest

    Steve Horsley wrote:
    >>
    >> when i close, should i close both br and socket? if so, in which
    >> order? if not, which one should be closed?

    >
    > Closing the BufferedReader should also close the underlying Reader, which
    > should close the underlying InputStream which should close the
    > underlying Socket.
    >
    > But if you also called the socket's getOutputStream() method, you should
    > also
    > close that stream (or any wrappers you put round it) to avoid leaving an
    > open
    > InputStream / file handle. I think that closing the socket doesn't close
    > the input and output streams above it.


    Always close the output stream first, as this will flush any pending
    data. You can then close the input stream and the socket if you want to
    but there is no need, you won't be able to use them anyway.
     
    Esmond Pitt, Apr 7, 2005
    #4
  5. Esmond Pitt wrote:

    > Always close the output stream first, as this will flush any pending
    > data.


    Thanks for the tip.

    > You can then close the input stream and the socket if you want to
    > but there is no need, you won't be able to use them anyway.


    Are you sure this doesn't constitute a handle leak if you don't
    close the input stream too? I was sure I got "too many open files"
    errors once after opening and closing lots of sockets but forgetting
    to close the associated streams.

    Steve
     
    Steve Horsley, Apr 7, 2005
    #5
  6. -

    Esmond Pitt Guest

    Steve Horsley wrote:
    > Are you sure this doesn't constitute a handle leak if you don't
    > close the input stream too? I was sure I got "too many open files"
    > errors once after opening and closing lots of sockets but forgetting
    > to close the associated streams.


    No, closing either of the streams also closes the socket.
     
    Esmond Pitt, Apr 8, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,943
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    996
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    571
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    689
    Steve Holden
    Feb 1, 2009
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    898
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page