Re: ObjectInputStream available() method
- From: Owen Jacobson <angrybaldguy@xxxxxxxxx>
- Date: Sat, 12 Jul 2008 13:50:47 -0700 (PDT)
On Jul 11, 8:52 am, LC's No-Spam Newsreading account
<nos...@xxxxxxxxxxxxxx> wrote:
I have a servlet-applet couple which communicate via a server Socket and
client Socket couple. In the functioning implementation the servlet
listens on the ServerSocket, opens a clientSocket when the applet
connects, and opens onto it two streams. The applet does the same on its
side.
PrintWriter appletout = new
PrintWriter(mySocket.getOutputStream(), true);
BufferedReader appletin = new BufferedReader(new
InputStreamReader(mySocket.getInputStream()));
The applet tests appletin.ready() before reading with
appletin.readLine(). In some cases it has to test a few times before it
becomes ready, but it works.
What does it do if it discovers the stream is *not* ready()? If it
waits, you're doing exactly what would happen if you skipped the
ready() check, but at a higher cost in CPU power. If you're trying to
avoid blocking another ongoing task (like the Swing EDT), you may be
better off doing the IO on a background thread via something like
SwingWorker.
The only reliable way to test whether a stream has data ready or not
is by reading it. All else is inaccurate and underinformative. In
particular, the available() method almost universally returns zero,
except for BufferedInputStreams with data buffered up. Similarly,
ready() may "lie" if the amount of data available without blocking is
insufficient to rebuild the entire object graph -- in which case
readObject() will block waiting for the rest of the data.
-o
.
- Follow-Ups:
- Re: ObjectInputStream available() method
- From: Roedy Green
- Re: ObjectInputStream available() method
- References:
- ObjectInputStream available() method
- From: LC's No-Spam Newsreading account
- ObjectInputStream available() method
- Prev by Date: Re: to get environment variables...
- Next by Date: Re: singe thread per connection
- Previous by thread: ObjectInputStream available() method
- Next by thread: Re: ObjectInputStream available() method
- Index(es):
Relevant Pages
|