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