1 2 3 /* 4 * The contents of this file are subject to the terms 5 * of the Common Development and Distribution License 6 * (the "License"). You may not use this file except 7 * in compliance with the License. 8 * 9 * You can obtain a copy of the license at 10 * glassfish/bootstrap/legal/CDDLv1.0.txt or 11 * https://glassfish.dev.java.net/public/CDDLv1.0.html. 12 * See the License for the specific language governing 13 * permissions and limitations under the License. 14 * 15 * When distributing Covered Code, include this CDDL 16 * HEADER in each file and include the License file at 17 * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable, 18 * add the following below this CDDL HEADER, with the 19 * fields enclosed by brackets "[]" replaced with your 20 * own identifying information: Portions Copyright [yyyy] 21 * [name of copyright owner] 22 * 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * 25 * Portions Copyright Apache Software Foundation. 26 */ 27 28 package javax.servlet; 29 30 import java.io.InputStream; 31 import java.io.IOException; 32 33 /** 34 * 35 * Provides an input stream for reading binary data from a client 36 * request, including an efficient <code>readLine</code> method 37 * for reading data one line at a time. With some protocols, such 38 * as HTTP POST and PUT, a <code>ServletInputStream</code> 39 * object can be used to read data sent from the client. 40 * 41 * <p>A <code>ServletInputStream</code> object is normally retrieved via 42 * the {@link ServletRequest#getInputStream} method. 43 * 44 * 45 * <p>This is an abstract class that a servlet container implements. 46 * Subclasses of this class 47 * must implement the <code>java.io.InputStream.read()</code> method. 48 * 49 * 50 * @author Various 51 * 52 * @see ServletRequest 53 * 54 */ 55 56 public abstract class ServletInputStream extends InputStream { 57 58 59 60 /** 61 * Does nothing, because this is an abstract class. 62 * 63 */ 64 65 protected ServletInputStream() { } 66 67 68 69 70 /** 71 * 72 * Reads the input stream, one line at a time. Starting at an 73 * offset, reads bytes into an array, until it reads a certain number 74 * of bytes or reaches a newline character, which it reads into the 75 * array as well. 76 * 77 * <p>This method returns -1 if it reaches the end of the input 78 * stream before reading the maximum number of bytes. 79 * 80 * 81 * 82 * @param b an array of bytes into which data is read 83 * 84 * @param off an integer specifying the character at which 85 * this method begins reading 86 * 87 * @param len an integer specifying the maximum number of 88 * bytes to read 89 * 90 * @return an integer specifying the actual number of bytes 91 * read, or -1 if the end of the stream is reached 92 * 93 * @exception IOException if an input or output exception has occurred 94 * 95 */ 96 97 public int readLine(byte[] b, int off, int len) throws IOException { 98 99 if (len <= 0) { 100 return 0; 101 } 102 int count = 0, c; 103 104 while ((c = read()) != -1) { 105 b[off++] = (byte)c; 106 count++; 107 if (c == '\n' || count == len) { 108 break; 109 } 110 } 111 return count > 0 ? count : -1; 112 } 113 } 114 115 116