question about streams

J

Jackie

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
 
M

Monique Y. Mudama

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);

?
 
J

Jackie

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?
 
G

Gordon Beaton

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
 
R

Rhino

Jackie said:
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.....
 
E

EJP

Jackie said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top