View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  //
19  // This source code implements specifications defined by the Java
20  // Community Process. In order to remain compliant with the specification
21  // DO NOT add / change / or delete method signatures!
22  //
23  
24  package javax.servlet;
25  
26  import java.io.IOException;
27  import java.io.InputStream;
28  
29  /**
30   *
31   * Provides an input stream for reading binary data from a client
32   * request, including an efficient <code>readLine</code> method
33   * for reading data one line at a time. With some protocols, such
34   * as HTTP POST and PUT, a <code>ServletInputStream</code>
35   * object can be used to read data sent from the client.
36   *
37   * <p>A <code>ServletInputStream</code> object is normally retrieved via
38   * the {@link ServletRequest#getInputStream} method.
39   *
40   * <p>This is an abstract class that a servlet container implements.
41   * Subclasses of this class
42   * must implement the <code>java.io.InputStream.read()</code> method.
43   *
44   * @see ServletRequest
45   *
46   * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
47   */
48  public abstract class ServletInputStream extends InputStream {
49      /**
50       * Does nothing, because this is an abstract class.
51       */
52      protected ServletInputStream() {
53      }
54  
55      /**
56       * Reads the input stream, one line at a time. Starting at an
57       * offset, reads bytes into an array, until it reads a certain number
58       * of bytes or reaches a newline character, which it reads into the
59       * array as well.
60       *
61       * <p>This method returns -1 if it reaches the end of the input
62       * stream before reading the maximum number of bytes.
63       *
64       * @param b an array of bytes into which data is read
65       *
66       * @param off an integer specifying the character at which
67       * this method begins reading
68       *
69       * @param len an integer specifying the maximum number of
70       * bytes to read
71       *
72       * @return an integer specifying the actual number of bytes
73       * read, or -1 if the end of the stream is reached
74       *
75       * @exception IOException if an input or output exception has occurred
76       */
77      public int readLine(byte[] b, int off, int len) throws IOException {
78          if (len <= 0) {
79              return 0;
80          }
81          int count = 0, c;
82  
83          while ((c = read()) != -1) {
84              b[off++] = (byte) c;
85              count++;
86              if (c == '\n' || count == len) {
87                  break;
88              }
89          }
90          return count > 0 ? count : -1;
91      }
92  }
93  
94  
95