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