Skip to main content
This revision made March 29, 2012 19:14, by Rajiv Mordani

We will introduce new classes for readers, writers, input and out streams for NIO. These would be exposed from ServletRequest and ServletResponse.

ReadListener:

package javax.servlet;

import java.util.EventListener;

/**
 * @since Servlet 3.1
 */

/**
 * <p>
 * This class represents a call-back mechanism that will notify implementations
 * as HTTP request data becomes available to read without blocking.
 * </p>
 *

 */
public interface ReadListener extends EventListener {

    /**
     * <p>
     * Invoked when data is available to be read without blocking.
     * This method will be invoked once for every set of data available.
     *
     */
    public void onDataAvailable();

    /**
     * <p>
     * Invoked when all data for the current request has been read.
     * </p>
     *
     */

    public void onAllDataRead();

    /**
     * <p>
     * Invoked when an error occurs processing the request.
     * </p>
     */
    public void onError(Throwable t);


}

AsyncIOInputSource

package javax.servlet;

/**
 * <p>
 * This interface defines methods to allow an {@link ServletInputStream} or
 * {@link ServletReader} to notify the developer <em>when</em> and <em>how much</em>
 * data is ready to be read without blocking.
 * </p>
 *
 * @since Servlet 3.1
 */

public interface AsyncIOInputSource {

    /**
     * @return the number of bytes (or characters) that may be obtained
     *  without blocking.
     */
    public int dataAvailable();

    /**
     * @return <code>true</code> when all data for this particular request
     *  has been read, otherwise returns <code>false</code>.
     */
    public boolean isFinished();

    /**
     * @return <code>true</code> if data can be obtained without blocking,
     *  otherwise returns <code>false</code>.
     */
    public boolean isReady();
}

ServletReader


package javax.servlet;

import java.io.Reader;

/**
 * @since Servlet 3.1
 */

public abstract class ServletReader extends Reader implements AsyncIOInputSource {
}

WriteListener

package javax.servlet;

import java.util.EventListener;

/**
 *
 * Callback notification mechanism that signals to the developer it's possible
 * to write content.
 *
 * @since Servlet 3.1
 */
public interface WriteListener extends EventListener {

    /**
     * This callback will be invoked when data can be written.
     *
     *
     * @throws Exception, {@link Exception} might be thrown by the custom
     *  handler code. This exception will be delegated for processing to
     *  {@link #onError(java.lang.Throwable)}.
     */
    public void onWritePossible(ServletResponse response);

    /**
     * <p>
     * Invoked when an error occurs processing the request asynchronously.
     * </p>
     */
    public void onError(final Throwable t);

    /**
     * Get the ServletResponse associated
     * @return
     */
}

AsyncIOOutputSink


package javax.servlet;

/**
 * <p>
 * This interface defines methods to allow an {@link javax.servlet.ServletOutputStream} or
 * {@link ServletWriter} to allow the developer to check with the runtime
 * whether or not it's possible to write data, or if it's not possible, to
 * be notified when it is.
 * </p>
 *
 * @since Servlet 3.1
 */

public interface AsyncIOOutputSink {


  /**
     *
     * @return <code>true</code> if a write to this <code>AsyncIOOutputSink</code>
     *  will succeed, otherwise returns <code>false</code>.
     */
    public boolean canWrite(int size);

}

ServletWriter


package javax.servlet;

import java.io.Writer;

/**
 * @since Servlet 3.1
 */

public abstract class ServletWriter extends Writer implements AsyncIOOutputSink {

ServletInputStream Changes

Index: ServletInputStream.java
===================================================================
--- ServletInputStream.java	(revision 48095)
+++ ServletInputStream.java	(working copy)
@@ -84,7 +84,7 @@
  *
  */
 
-public abstract class ServletInputStream extends InputStream {
+public abstract class ServletInputStream extends InputStream implements AsyncIOInputSource {

ServletOutputStream Changes

Index: ServletOutputStream.java
===================================================================
--- ServletOutputStream.java	(revision 48095)
+++ ServletOutputStream.java	(working copy)
@@ -81,7 +81,7 @@
  *
  */
 
-public abstract class ServletOutputStream extends OutputStream {
+public abstract class ServletOutputStream extends OutputStream implements AsyncIOOutputSink {

ServletRequest changes

Index: ServletRequest.java
===================================================================
--- ServletRequest.java	(revision 48095)
+++ ServletRequest.java	(working copy)
@@ -806,5 +806,38 @@
      * @since Servlet 3.0
      */
     public DispatcherType getDispatcherType();
+
+    /**
+     * Retrieves the body of the request as character data using
+     * a <code>BufferedReader</code>.  The reader translates the character
+     * data according to the character encoding used on the body.
+     * Either this method or {@link #getInputStream} may be called to read the
+     * body, not both.
+     *
+     * @return	 a <code>ServletReader</code> containing the body of the request
+     *
+     *
+     * @exception IllegalStateException if {@link #getInputStream} method
+     * has been called on this request
+     *
+     *
+     * @see #getInputStream
+     *
+     * @since Servlet 3.1
+     *
+     */
+
+    public ServletReader getServletReader();
+
+    /**
+       * Instructs the <code>ServletRequest</code> to invoke the provided
+       * {@link ReadListener} when it is possible to write
+       *
+       * @param listener the {@link ReadListener} that should be notified
+       *  when it's possible to read.
+       *
+       */
+
+    public void addReadListener(ReadListener listener);
 }

ServletResponse Changes

Index: ServletResponse.java
===================================================================
--- ServletResponse.java	(revision 48095)
+++ ServletResponse.java	(working copy)
@@ -154,7 +154,7 @@
     
     
 
-    /**
+  /**
      * Returns a {@link ServletOutputStream} suitable for writing binary 
      * data in the response. The servlet container does not encode the
      * binary data.  
@@ -486,7 +486,32 @@
     public Locale getLocale();
 
 
+    /**
+         * Returns a <code>ServletWriter</code> object that
+         * can send character text to the client.
+         *
+         *
+         * @return 		a <code>ServletWriter</code> object that
+         *			can return character data to the client
+         *
+         *
+         * @see 		#getOutputStream
+         *
+         */
+     public ServletWriter getServletWriter();
 
+    /**
+       * Instructs the <code>ServletResponse</code> to invoke the provided
+       * {@link WriteListener} when it is possible to write
+       *
+       *
+       * @param listener the {@link WriteListener} that should be notified
+       *  when it's possible to write.
+       *
+       */
+
+    public void addWriteListener(WriteListener listener);
+
 }

ServletWriter


package javax.servlet;

import java.io.Writer;

/**
 * @since Servlet 3.1
 */

public abstract class ServletWriter extends Writer implements AsyncIOOutputSink {
}

Sample

package servlet.samples.nio;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Rajiv Mordani
 */
public class NIOServletSample extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        AsyncContext ctx= request.startAsync();
        request.addReadListener(new ReadListener() {
            public void onDataAvailable(ServletRequest request) {
                ServletInputStream nis = null;
                try {
                    nis = request.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    nis.read(new byte[nis.dataAvailable()]);
                } catch (IOException e) {
                               e.printStackTrace();
                }
            }

            public void onAllDataRead(ServletRequest request) {
                try {
                    request.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            public void onError(Throwable t) {

                }
        });
         response.addWriteListener(new WriteListener() {
             public void onWritePossible(ServletResponse response) {
                 try {
                     ServletOutputStream nos = response.getOutputStream();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }

             public void onError(Throwable t) {

             }
         });

    }
}

Difference compared to previous revision
We will introduce new classes for readers, writers, input and out streams for NIO. These would be exposed from ServletRequest and ServletResponse. === ReadListener:
package javax.servlet;

import java.util.EventListener;

/**
 * @since Servlet 3.1
...
    /**
     * 

* Invoked when data is available to be read without blocking. * This method will be invoked once for every set of data available. * *We will introduce new classes for readers, writers, input and out streams for NIO. These would be exposed from ServletRequest and ServletResponse. === ReadListener:

...
     * Invoked when data is available to be read without blocking.
     *
     */
    public void onDataAvailable(ServletRequest request);

    /**
     * 

... * */ public void onAllDataRead(ServletRequest request); /** *

 
 
Close
loading
Please Confirm
Close