Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] request.available() == 0 in Servlet implementation

Since there's no such thing as HttpServletRequest.available(), i'm going to assume you mean


With Servlet 3.1 and async I/O everywhere, this isn't going to work the way you think.

Take a look at the javadoc for ServletInputStream.

Notice that it does not override the .available() call?

Now take a look at the InputStream.available() method javadoc

It says

The available method for class InputStream always returns 0.

Its doing exactly what its been designed to do, and ServletInputStream does nothing special about it.

If you are attempting to use ServletInputStream in a non-blocking way, then you should look into the Async I/O features.

It uses AsyncContext, with a ReadListener and events to onDataAvailable() to know if there is data available to read.
Then, internally, use the ServletInputStream.isReady() is  used to know if blocking would occur or not.
And then use the byte buffer read that returns a length, that way you know how much was read.

AsyncContext context = request.startAsync();
ServletInputStream input = request.getInputStream();
input.setReadListener(new MyReadListener(input, context));

MyReadListener.onDataAvailable() {
  try {
     int len = -1;
     byte b[] = new byte[1024];
     while (input.isReady())
       len =;
       if(len == -1) {
          // EOF, nothing left to read
       // Process bytes read (you know the length now)
  } catch (IOException ex) {

Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts

On Fri, Feb 6, 2015 at 7:19 AM, Guus der Kinderen <guus.der.kinderen@xxxxxxxxx> wrote:
Hi all,

Over the past few days, I have been fighting with the following issue. The scenario revolves around a Jetty 9.2.7 based application, in which a request gets POSTed to a servlet. The HTTP payload in the request is to be consumed. 

The servlet implementation overrides doPost(HttpServletRequest request, HttpServletResponse response). We experience that request.available() returns zero in this implementation (which causes problems when trying to consume the HTTP payload using the InputStream from the request). This happens with some frequency (approximately once in every 50 requests).

The work-around that we have in place: invoke Thread.sleep(100) when request.available() returns zero. This works, but is an undesirable solution.

The efforts to reproduce the problem in a more compact code-base than the elaborate servlet and client implementations in our products have not been successful.

I am looking for thoughts on the cause of this issue, suggestions for fixes, and/or approaches to create a portable reproduction path. Any help is much appreciated!



jetty-users mailing list
To change your delivery options, retrieve your password, or unsubscribe from this list, visit

Back to the top