This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ObjectInputStream  RSS feed

 
conor murphy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i have a client server program. i can send and recieve text fine. i am trying to create a function that is able to send an image object. i create and objectoutputstream and it sends it properly however when the server recieves it,it prints out a lot of garbage which i thikn is the object. i dont want it to print out the object though. it kind of hard to explain. is there anyway to distinguish between an input stream reader and an object input stream reader so that it knows wen to print out text but not image objects?

thanks

here is a piece of the code i have. it creates a buffered input stream reader and also an object inputstream reader

in = new BufferedReader(new InputStreamReader(sock.getInputStream()));

ObjectInputStream obj = new ObjectInputStream(sock.getInputStream());

src= (PlanarImage)obj.readObject();
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do this, but you'd have to be careful. You'd want the client to send a message that says "The next thing is an object" or "The next thing is a String", and then you'd read from the appropriate InputStream. But you definitely can't use a BufferedReader or BufferedInputStream because, as its name suggests, it reads more than is requested and "buffers" it. If the BufferedReader is holding data needed by the ObjectInputStream in its buffer, then, well, you're stuck.

An easier way to do things would be to use an ObjectInputStream exclusively, even to send Strings. On the client end, write a String object as you'd write any other object, and on the server end, again, read the String as any other object. You'd still need some way to say "display this" vs. "do something else with this", but as there's only one stream, it's definitely less complex.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!