001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 //
019 // This source code implements specifications defined by the Java
020 // Community Process. In order to remain compliant with the specification
021 // DO NOT add / change / or delete method signatures!
022 //
023
024 package javax.servlet;
025
026 import java.io.IOException;
027 import java.io.InputStream;
028
029 /**
030 *
031 * Provides an input stream for reading binary data from a client
032 * request, including an efficient <code>readLine</code> method
033 * for reading data one line at a time. With some protocols, such
034 * as HTTP POST and PUT, a <code>ServletInputStream</code>
035 * object can be used to read data sent from the client.
036 *
037 * <p>A <code>ServletInputStream</code> object is normally retrieved via
038 * the {@link ServletRequest#getInputStream} method.
039 *
040 * <p>This is an abstract class that a servlet container implements.
041 * Subclasses of this class
042 * must implement the <code>java.io.InputStream.read()</code> method.
043 *
044 * @see ServletRequest
045 *
046 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
047 */
048 public abstract class ServletInputStream extends InputStream {
049 /**
050 * Does nothing, because this is an abstract class.
051 */
052 protected ServletInputStream() {
053 }
054
055 /**
056 * Reads the input stream, one line at a time. Starting at an
057 * offset, reads bytes into an array, until it reads a certain number
058 * of bytes or reaches a newline character, which it reads into the
059 * array as well.
060 *
061 * <p>This method returns -1 if it reaches the end of the input
062 * stream before reading the maximum number of bytes.
063 *
064 * @param b an array of bytes into which data is read
065 *
066 * @param off an integer specifying the character at which
067 * this method begins reading
068 *
069 * @param len an integer specifying the maximum number of
070 * bytes to read
071 *
072 * @return an integer specifying the actual number of bytes
073 * read, or -1 if the end of the stream is reached
074 *
075 * @exception IOException if an input or output exception has occurred
076 */
077 public int readLine(byte[] b, int off, int len) throws IOException {
078 if (len <= 0) {
079 return 0;
080 }
081 int count = 0, c;
082
083 while ((c = read()) != -1) {
084 b[off++] = (byte) c;
085 count++;
086 if (c == '\n' || count == len) {
087 break;
088 }
089 }
090 return count > 0 ? count : -1;
091 }
092 }
093
094
095