Since there's no such thing as HttpServletRequest.available(), i'm going to assume you mean
ServletInputStream.available()
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 = input.read(b);
if(len == -1) {
// EOF, nothing left to read
return
}
// Process bytes read (you know the length now)
}
} catch (IOException ex) {
ex.printStackTrace();
}
}