Re: ObjectInputStream available() method



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
.



Relevant Pages

  • ObjectInputStream available() method
    ... 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. ... Now I want to write a DIFFERENT servlet-applet couple, which exchanges some complex binary data. ...
    (comp.lang.java.programmer)
  • Re: ObjectInputStream available() method
    ... 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. ...
    (comp.lang.java.programmer)
  • Re: [kde] How to place the desktop switcher in the taskbar instead of in the panel?
    ... selection dialog and on "add" it adds the applet to the child panel. ... it opens the usualy dialog which is also ... Kevin Krammer, KDE developer, xdg-utils developer ...
    (KDE)
  • Re: Add a Hyperlink to a File
    ... To open a PDF from within an applet might be ... show document method in web start reports ...
    (comp.lang.java.programmer)
  • Can not open the radio link
    ... some need java or flash for ... >I used listen to the radio on the Internet. ... radio (applet) opens and closes itself immediately. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)