Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ObjectInputStream available() method

Reply
Thread Tools

ObjectInputStream available() method

 
 
LC's No-Spam Newsreading account
Guest
Posts: n/a
 
      07-11-2008
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.

Now I want to write a DIFFERENT servlet-applet couple, which exchanges
some complex binary data.

In this case on the servlet side I thought I should have an
ObjectOutputStream

appletout = new ObjectOutputStream(clientSocket.getOutputStream()) ;

and on the applet side I should have

appletin = new ObjectInputStream(mySocket.getInputStream() );

I expected that, in order to test that the stream is ready, I had to
replace the ready() call with a appletin.available() != 0

It occurs instead that this is not true, apparently (I'm currently
testing between two standalone applications) available() always returns
zero, and the pseudo-applet can immediately issue a

s = (String) appletin.readObject() ;

as soon as the pseudo-servlet has e.g. issued

appletout.writeObject((String) "READY");
appletout.flush();

(is my assumption that each write shall be immediately followed by a
flush ?)

How does one use available() or any other way to find that there is
pending data on the input channel ?

--
----------------------------------------------------------------------
http://www.velocityreviews.com/forums/(E-Mail Removed) is a newsreading account used by more persons to
avoid unwanted spam. Any mail returning to this address will be rejected.
Users can disclose their e-mail address in the article if they wish so.
 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      07-12-2008
On Jul 11, 8:52*am, LC's No-Spam Newsreading account
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      07-13-2008
LC's No-Spam Newsreading account wrote:
> How does one use available() or any other way to find that there is
> pending data on the input channel ?
>


From the javadocs:
<http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html#available()>
> Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or another thread.
>
> The available method for class InputStream always returns 0.


What this means to me is that available tells you the largest number of
bytes that you can read/write without worrying about blocking. You may
be able to read/write more.

Anyway, in your situation, I would suggest having a thread for the
reading, and fire off an event when a new object comes through. That way
it is okay for the read calls to block, and you don't have to worry
about readiness/availability of data.

Hope this helps,
Daniel.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-13-2008
On Sat, 12 Jul 2008 13:50:47 -0700 (PDT), Owen Jacobson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>The only reliable way to test whether a stream has data ready or not
>is by reading it.


The problem is, even if a few bytes have come in, it does not mean
that is sufficient to satisfy the next read which has to do some fancy
footwork to reconstitute objects.

I do it in the Replicator with a background thread.

see http://mindprod.com/webstart/replicator.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      07-13-2008
LC's No-Spam Newsreading account 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.
>
> Now I want to write a DIFFERENT servlet-applet couple, which exchanges
> some complex binary data.
>
> In this case on the servlet side I thought I should have an
> ObjectOutputStream
>
> appletout = new ObjectOutputStream(clientSocket.getOutputStream()) ;
>
> and on the applet side I should have
>
> appletin = new ObjectInputStream(mySocket.getInputStream() );
>
> I expected that, in order to test that the stream is ready, I had to
> replace the ready() call with a appletin.available() != 0
>
> It occurs instead that this is not true, apparently (I'm currently
> testing between two standalone applications) available() always returns
> zero, and the pseudo-applet can immediately issue a
>
> s = (String) appletin.readObject() ;
>
> as soon as the pseudo-servlet has e.g. issued
>
> appletout.writeObject((String) "READY");
> appletout.flush();
>
> (is my assumption that each write shall be immediately followed by a
> flush ?)
>
> How does one use available() or any other way to find that there is
> pending data on the input channel ?


I will recommend avoiding .available() and .ready() completely.

Just read, use exception or return value to detect EOF, and if
you need not to block the thread, then make a blocking read
in a separate thread.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
EOFException at ObjectInputStream David Baer Java 2 02-26-2004 09:56 AM
How to convert a byte array to ObjectInputStream Ahmed Moustafa Java 5 11-16-2003 09:08 AM
Re: creating ObjectInputStream takes forever Roedy Green Java 2 08-05-2003 10:30 PM
Re: creating ObjectInputStream takes forever Lothar Kimmeringer Java 3 08-01-2003 04:46 AM
objectInputStream.readObject() does not work properly after certain period of inactivity. Serial # 19781010 Java 1 07-15-2003 10:20 PM



Advertisments