question about streams

Discussion in 'Java' started by Jackie, Apr 28, 2006.

  1. Jackie

    Jackie Guest

    Hello

    I have a little problem with the following code (s2c is a socket):

    is = s2c.getInputStream();
    br = new BufferedReader(new InputStreamReader(is));
    oi = new ObjectInputStream(is); //here my program hangs. How can I
    solve this? Or is it impossible to make 2 streams from one InputStream?

    As a student, I'm just starting with java. So maybe it's something very
    stupid:)
    Thanks, Chris
     
    Jackie, Apr 28, 2006
    #1
    1. Advertising

  2. On 2006-04-28, Jackie penned:
    > Hello
    >
    > I have a little problem with the following code (s2c is a socket):
    >
    > is = s2c.getInputStream();
    > br = new BufferedReader(new InputStreamReader(is));
    > oi = new ObjectInputStream(is); //here my program hangs. How can I
    > solve this? Or is it impossible to make 2 streams from one InputStream?
    >
    > As a student, I'm just starting with java. So maybe it's something very
    > stupid:)


    What are you trying to accomplish by doing this?

    Depending on what you're trying to do, would it make sense to do

    oi = new ObjectInputStream(br);

    ?

    --
    monique

    Help us help you:
    http://www.catb.org/~esr/faqs/smart-questions.html
     
    Monique Y. Mudama, Apr 28, 2006
    #2
    1. Advertising

  3. Jackie

    Jackie Guest

    br is a character stream, while oi (ObjectInputStream) is of course a
    bytestream, so i don't think oi = new ObjectInputStream(br); will work
    i have to send and receive tekst en objects through a socket. i can
    also recieve my Strings with an objectinputstream but i want to use a
    bufferedreader because i need some functions of it that
    objectinputstream doesn't have. So i just want to be able to set an
    objectinputstream on the inputstream of the socket while there is also
    constantly an bufferedreader on the inputstream. Is there a way to do
    so?
     
    Jackie, Apr 29, 2006
    #3
  4. On 28 Apr 2006 14:27:39 -0700, Jackie wrote:
    > I have a little problem with the following code (s2c is a socket):
    >
    > is = s2c.getInputStream();
    > br = new BufferedReader(new InputStreamReader(is));
    > oi = new ObjectInputStream(is); //here my program hangs. How can I
    > solve this? Or is it impossible to make 2 streams from one InputStream?


    You will likely create more problems than you solve by forking the
    stream that way. Reconsider your design.

    However the ObjectInputStream constructor hangs for a completely
    unrelated reason: it is waiting for corresponding ObjectOutputStream
    constructor to send some header information.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Apr 29, 2006
    #4
  5. Jackie

    Rhino Guest

    "Jackie" <> wrote in message
    news:...
    > br is a character stream, while oi (ObjectInputStream) is of course a
    > bytestream, so i don't think oi = new ObjectInputStream(br); will work
    > i have to send and receive tekst en objects


    "tekst en" objects??

    What's that in English?

    Even if I assume that this is just sloppy typing with transposed characters,
    I can't think of any English word that resembles this that would make sense
    in this context.....

    > through a socket. i can
    > also recieve my Strings with an objectinputstream but i want to use a
    > bufferedreader because i need some functions of it that
    > objectinputstream doesn't have. So i just want to be able to set an
    > objectinputstream on the inputstream of the socket while there is also
    > constantly an bufferedreader on the inputstream. Is there a way to do
    > so?
    >


    --
    Rhino
     
    Rhino, Apr 29, 2006
    #5
  6. Jackie

    Chris Uppal Guest

    Rhino wrote:

    > "tekst en" objects??
    >
    > What's that in English?


    Text and objects.

    -- chris
     
    Chris Uppal, Apr 29, 2006
    #6
  7. Jackie

    EJP Guest

    Jackie wrote:
    > br is a character stream, while oi (ObjectInputStream) is of course a
    > bytestream, so i don't think oi = new ObjectInputStream(br); will work
    > i have to send and receive tekst en objects through a socket. i can
    > also recieve my Strings with an objectinputstream but i want to use a
    > bufferedreader because i need some functions of it that
    > objectinputstream doesn't have. So i just want to be able to set an
    > objectinputstream on the inputstream of the socket while there is also
    > constantly an bufferedreader on the inputstream. Is there a way to do
    > so?


    Both ObjectInputStream and BufferedReader do buffering internally, so
    they are *bound* to interfere with each other no matter which way you
    stack them up. You might think that new BufferedReader(new
    InputStreamReader(new ObjectInputStream(socket.getInputStream()))) might
    be plausible but it isn't. You need to rethink your requirement.
     
    EJP, Apr 30, 2006
    #7
    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. Pawel Banys
    Replies:
    1
    Views:
    494
    Joe Smith
    Jul 8, 2004
  2. laniik
    Replies:
    4
    Views:
    412
    laniik
    Apr 8, 2005
  3. John Simeon

    Basic question on streams

    John Simeon, Jul 22, 2007, in forum: C++
    Replies:
    16
    Views:
    558
    Marcus Kwok
    Jul 25, 2007
  4. K S M

    Question on streams

    K S M, Aug 24, 2007, in forum: C Programming
    Replies:
    4
    Views:
    341
    Army1987
    Aug 25, 2007
  5. grocery_stocker

    closures and streams question

    grocery_stocker, Feb 5, 2009, in forum: Perl Misc
    Replies:
    3
    Views:
    114
    Tad J McClellan
    Feb 5, 2009
Loading...

Share This Page