home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 September / PCWorld_2000-09_cd.bin / Komunik / sambar / _setup.1 / javaeng.jar / javax / servlet / ServletInputStream.java < prev    next >
Text File  |  2000-04-03  |  4KB  |  128 lines

  1. /*
  2.  * ServletInputStream.java -- InputStream for reading servlet requests
  3.  *
  4.  * Copyright (c) 1998, 1999 by Free Software Foundation, Inc.
  5.  * Written by Paul Siegmann (pauls@euronet.nl)
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU Library General Public License as published
  9.  * by the Free Software Foundation, version 2. (see COPYING.LIB)
  10.  *
  11.  * This program is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software Foundation
  18.  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
  19.  */
  20.  
  21. package javax.servlet;
  22.  
  23. import java.io.InputStream;
  24. import java.io.IOException;
  25.  
  26.  
  27. /**
  28.  * This class serves as a stream where servlets can read data supplied by
  29.  * the client from.
  30.  *
  31. #ifdef SERVLET_2_0
  32.  * @version Servlet API 2.0 
  33. #endif
  34. #ifdef SERVLET_2_1
  35.  * @version Servlet API 2.1
  36. #endif
  37. #ifdef SERVLET_2_2
  38.  * @version Servlet API 2.2
  39. #endif
  40.  * @since Servlet API 1.0
  41.  * @author Paul Siegmann (pauls@euronet.nl)
  42.  */
  43. public abstract class ServletInputStream
  44.     extends InputStream 
  45. {
  46.     /**
  47.      * Does nothing.
  48.      *
  49.      * @since Servlet API 1.0
  50.      */
  51.     protected ServletInputStream() {
  52.     }
  53.  
  54.     /**
  55.      * This method read bytes from a stream and stores them into a caller
  56.      * supplied buffer.  It starts storing the data at index
  57.      * <code>offset</code> into the buffer and attempts to read until a 
  58.      * end of line ('\n') is encountered or <code>length</code> bytes are
  59.      * read.
  60.      * This method can return before reading the number of bytes requested.
  61.      * The actual number of bytes read is returned as an int.
  62.      * A -1 is returned to indicate the end of the stream.
  63.      * 
  64.      * This method will block until some data can be read.
  65.      *
  66.      * This method operates by calling the single byte <code>read()</code>
  67.      * method in a loop until the desired number of bytes are read.
  68.      * The read loop stops short if the end of the stream is encountered
  69.      * or if an IOException is encountered on any read operation except
  70.      * the first.  If the first attempt to read a bytes fails, the
  71.      * IOException is allowed to propagate upward. And subsequent
  72.      * IOException is caught and treated identically to an end of stream
  73.      * condition.  Subclasses can (and should if possible)
  74.      * override this method to provide a more efficient implementation.
  75.      *
  76.      * @since Servlet API 1.0
  77.      *
  78.      * @param buffer The array into which the bytes read should be stored
  79.      * @param offset The offset into the array to start storing bytes
  80.      * @param length The maximum number of bytes to read
  81.      *
  82.      * @return The actual number of bytes read, or -1 if end of stream.
  83.      *
  84.      * @exception IOException If an error occurs.
  85.      */
  86.     public int readLine(byte[] buffer, int offset, int length) throws IOException {
  87.     /* Note:
  88.         Both the code and the javadocs are originally from Aaron
  89.         M. Renn's (arenn@urbanophile.com) java.io.InputStream code.
  90.         I have adapted it to my coding style and added the '\n'
  91.         detection. I like free software!
  92.     */
  93.         if (length == 0) {
  94.             return(0);
  95.         }
  96.  
  97.         // Read the first byte here in order to allow IOException's to 
  98.         // propagate up
  99.         int readChar = read();
  100.         if (readChar == -1) {
  101.             return(-1);
  102.         }
  103.         buffer[offset] = (byte)readChar;
  104.  
  105.         int totalRead = 1;
  106.  
  107.         // Read the rest of the bytes
  108.         try {
  109.             for (int i = 1; i < length; i++) {
  110.                 if(readChar == '\n') {
  111.                     return(totalRead);
  112.                 }
  113.                 readChar = read();
  114.                 if (readChar == -1) {
  115.                     return(totalRead);
  116.                 }
  117.                 buffer[offset + i] = (byte)readChar;
  118.                 totalRead++;
  119.             }
  120.         }
  121.         catch (IOException e) {
  122.             return(totalRead);
  123.         }
  124.  
  125.         return(totalRead);
  126.     }
  127. }
  128.