Skip to main content
Last updated March 29, 2012 19:24, by Rajiv Mordani
Feedicon  
We will introduce new classes for readers, writers, input and out streams for NIO. These would be exposed from ServletRequest and ServletResponse. === ReadListener: <pre name="java"> 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); } </pre> === AsyncIOInputSource <pre name="java"> 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); } </pre> === ServletReader <pre name="java"> package javax.servlet; import java.io.Reader; /** * @since Servlet 3.1 */ public abstract class ServletReader extends Reader implements AsyncIOInputSource { } </pre> === WriteListener <pre name="java"> 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); } } </pre> === AsyncIOOutputSink <pre name="java"> 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); } </pre> === ServletWriter <pre name="java"> package javax.servlet; import java.io.Writer; /** * @since Servlet 3.1 */ public abstract class ServletWriter extends Writer implements AsyncIOOutputSink { </pre> === ServletInputStream Changes <pre name="java"> 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 { </pre> === ServletOutputStream Changes <pre name="java"> 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 { </pre> === ServletRequest changes <pre name="java"> 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(); + } </pre> === ServletResponse Changes <pre name="java"> 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(); } </pre> === ServletWriter <pre name="java"> package javax.servlet; import java.io.Writer; /** * @since Servlet 3.1 */ public abstract class ServletWriter extends Writer implements AsyncIOOutputSink { } </pre> === Sample <pre name="java"> 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) throws IOException { final ServletInputStream sis = request.getInputStream(); final ServletOutputStream sos = response.getOutputStream(); sis.setReadListener( (new ReadListener() { public void onDataAvailable() { try { sis.read(new byte[sis.dataAvailable()]); } catch (IOException e) { e.printStackTrace(); } } public void onAllDataRead() { try { sis.close(); } catch (IOException e) { e.printStackTrace(); } } public void onError(Throwable t) { } })); sos.setWriteListener(new WriteListener() { public void onWritePossible() { } public void onError(Throwable t) { } }); } } </pre>
 
 
Close
loading
Please Confirm
Close