Debian Codegraph @

ssl-proxies/src/main/java/org/globus/util/http/LineReader.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.util.http;
16
17 import java.io.InputStream;
18 import java.io.IOException;
19 import java.io.FilterInputStream;
20
21 public class LineReader extends FilterInputStream {
22
23 private static final int MAX_LEN = 16* 1024;
24
25 protected int _charsRead = 0;
26
27 public LineReader(InputStream is) {
28 super(is);
29 }
30
31 public InputStream getInputStream() {
32 return in;
33 }
34
35 public int getCharsRead() {
36 return _charsRead;
37 }
38
39 public String readLine()
40 throws IOException {
41 return readLine(in);
42 }
43
44 /**
45 * Read a line of text from the given Stream and return it
46 * as a String. Assumes lines end in CRLF.
47 * @param in a connected stream which contains the entire
48 * message being sen.
49 * @exception IOException if a connection fails or abnormal connection
50 * termination.
51 * @return the next line read from the stream.
52 */
53 protected String readLine(InputStream in)
54 throws IOException {
55 StringBuffer buf = new StringBuffer();
56 int c, length = 0;
57
58 while(true) {
59 c = in.read();
60 if (c == -1 || c == '\n' || length > MAX_LEN) {
61 _charsRead++;
62 break;
63 } else if (c == '\r') {
64 in.read();
65 _charsRead+=2;
66 break;
67 } else {
68 buf.append((char)c);
69 length++;
70 }
71 }
72 _charsRead += length;
73 return buf.toString();
74 }
75 }