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