Skip to main content
This revision made March 29, 2012 19:19, 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();

    /**
     * Instructs the <code>AsyncIOInputSource</code> to invoke the provided
     * {@link ReadListener} when it is possible to read
     *
     * @param readListener the {@link ReadListener} that should be notified
     *  when it's possible to read.
     *
     */

    public void setReadListener(ReadListener readListener);

}

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.
     * This method will be invoked once for every write operation possible
     *
     */
    public void onWritePossible();

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

}
}

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();

    /**
     * Instructs the <code>AsyncIOOutputSink</code> to invoke the provided
     * {@link WriteListener} when it is possible to write
     *
     *
     * @param writeListener the {@link WriteListener} that should be notified
     *  when it's possible to write.
     *
     */
    public void setWriteListener(WriteListener writeListener);

}

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();
+
 }

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();
 
 }

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
/** * This callback will be invoked when data can be written. * * * @tWe will introduce new classes for readers, writers, input and out streams for NIO. These would be exposed from ServletRequest and ServletResponse. === ReadListener:
...

    /**
     * This callback will be invoked when data can be written.
     * Throwis Excmeptiethon,d {@lwinkll Excbeption} might be invokede thrown by th once custfom
     *  handler code. This or evexceptionry wr will bite dopelegrated fiorn processing to possible
     *  {@link #onError(java.lang.Throwable)}.
     */
    public void onWritePossible(ServletResponse response);

    /**
     * 

... * Invoked when an error occurs processing the request asynchronously. *

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

*/ public void onError(final Throwable t);} * Get the ServletResponse associated * @return */ }
... * @return true if a write to this AsyncIOOutputSink * will succeed, otherwise returns false. */ public boolean canWrite(); /** * Instructs the AsyncIOOutputSink
to invoke the provided * {@link WriteListener} when } ... * @return true if a write to this AsyncIOOutputSink * will succeed, otherwise returns false. */ public boolean canWrite(int sizit is possible to write * * * @param writeListener the {@link WriteListener} that should be notified * when it's possible to write. * */ public void setWriteListener(WriteListener writeListenere); } === ServletWriter ... + */ + + public ServletReader getServletReader(); ++ /** + * Instructs the ServletRequest 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 ... + * + */ + public ServletWriter getServletWriter(); + /** + * Instructs the ServletResponse 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); + }
 
 
Close
loading
Please Confirm
Close