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 package javax.servlet;
021
022 import java.io.InputStream;
023 import java.io.IOException;
024
025 /**
026 *
027 * Provides an input stream for reading binary data from a client
028 * request, including an efficient <code>readLine</code> method
029 * for reading data one line at a time. With some protocols, such
030 * as HTTP POST and PUT, a <code>ServletInputStream</code>
031 * object can be used to read data sent from the client.
032 *
033 * <p>A <code>ServletInputStream</code> object is normally retrieved via
034 * the {@link ServletRequest#getInputStream} method.
035 *
036 *
037 * <p>This is an abstract class that a servlet container implements.
038 * Subclasses of this class
039 * must implement the <code>java.io.InputStream.read()</code> method.
040 *
041 *
042 * @author Various
043 * @version $Version$
044 *
045 * @see ServletRequest
046 *
047 */
048
049 public abstract class ServletInputStream extends InputStream {
050
051
052
053 /**
054 * Does nothing, because this is an abstract class.
055 *
056 */
057
058 protected ServletInputStream() { }
059
060
061
062
063 /**
064 *
065 * Reads the input stream, one line at a time. Starting at an
066 * offset, reads bytes into an array, until it reads a certain number
067 * of bytes or reaches a newline character, which it reads into the
068 * array as well.
069 *
070 * <p>This method returns -1 if it reaches the end of the input
071 * stream before reading the maximum number of bytes.
072 *
073 *
074 *
075 * @param b an array of bytes into which data is read
076 *
077 * @param off an integer specifying the character at which
078 * this method begins reading
079 *
080 * @param len an integer specifying the maximum number of
081 * bytes to read
082 *
083 * @return an integer specifying the actual number of bytes
084 * read, or -1 if the end of the stream is reached
085 *
086 * @exception IOException if an input or output exception has occurred
087 *
088 */
089
090 public int readLine(byte[] b, int off, int len) throws IOException {
091
092 if (len <= 0) {
093 return 0;
094 }
095 int count = 0, c;
096
097 while ((c = read()) != -1) {
098 b[off++] = (byte)c;
099 count++;
100 if (c == '\n' || count == len) {
101 break;
102 }
103 }
104 return count > 0 ? count : -1;
105 }
106 }
107
108
109