This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes available() method returning zero Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "available() method returning zero " Watch "available() method returning zero " New topic
Author

available() method returning zero

Michael Monchu
Greenhorn

Joined: Oct 20, 2010
Posts: 16
Hi

I am the first time on the forum and I am having a problem with retrieving the data from the InputStream. I have a Client program that sends the data to the ISeries Server and get a response and display it on a Web page. The problem is that when I run the program interactively, the available method returns zero, but when run in a debug mode, the available() returns the expected number of bytes. Can someone help me in explaining why the method would work only in a debug mode. Below is the source code used to read the data from the socket.



Please let me know if you need more information regarding this.

Thanks in advance.

Michael

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2970
    
    9
See this: AvailableDoesntDoWhatYouThinkItDoes.

Probably, in debug mode your code is running more slowly (especially if it's waiting for you to press buttons to step through something) and thus, whatever process is responsible for making bytes available has more time to achieve this. When you're not in debug mode, the code runs faster, and it goes through that code before bytes are actually available. Avoid using available(); it's fundamentally useless.
Michael Monchu
Greenhorn

Joined: Oct 20, 2010
Posts: 16
I changed the code not to use the available(), and I'm glad to tell you that it's working perfectly.

Thanks a lot for your assistance.

 
 
subject: available() method returning zero
 
Similar Threads
Thread Flow
Barcode reading using a blue tooth scanner
PC Connected to Internet and Internet Connectivity?
java.lang.IllegalArgumentException: im == null!
How to read "0000 0001" from keyboard?