1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util;
20
21 import java.io.BufferedInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24
25
26
27
28
29
30 public class ReadLineInputStream extends BufferedInputStream
31 {
32 boolean _seenCRLF;
33 boolean _skipLF;
34
35 public ReadLineInputStream(InputStream in)
36 {
37 super(in);
38 }
39
40 public ReadLineInputStream(InputStream in, int size)
41 {
42 super(in,size);
43 }
44
45 public String readLine() throws IOException
46 {
47 mark(buf.length);
48
49 while (true)
50 {
51 int b=super.read();
52 if (b==-1)
53 {
54 int m=markpos;
55 markpos=-1;
56 if (pos>m)
57 return new String(buf,m,pos-m,StringUtil.__UTF8_CHARSET);
58
59 return null;
60 }
61
62 if (b=='\r')
63 {
64 int p=pos;
65
66
67 if (_seenCRLF && pos<count)
68 {
69 if (buf[pos]=='\n')
70 pos+=1;
71 }
72 else
73 _skipLF=true;
74 int m=markpos;
75 markpos=-1;
76 return new String(buf,m,p-m-1,StringUtil.__UTF8_CHARSET);
77 }
78
79 if (b=='\n')
80 {
81 if (_skipLF)
82 {
83 _skipLF=false;
84 _seenCRLF=true;
85 markpos++;
86 continue;
87 }
88 int m=markpos;
89 markpos=-1;
90 return new String(buf,m,pos-m-1,StringUtil.__UTF8_CHARSET);
91 }
92 }
93 }
94
95 @Override
96 public synchronized int read() throws IOException
97 {
98 int b = super.read();
99 if (_skipLF)
100 {
101 _skipLF=false;
102 if (_seenCRLF && b=='\n')
103 b=super.read();
104 }
105 return b;
106 }
107
108 @Override
109 public synchronized int read(byte[] buf, int off, int len) throws IOException
110 {
111 if (_skipLF && len>0)
112 {
113 _skipLF=false;
114 if (_seenCRLF)
115 {
116 int b = super.read();
117 if (b==-1)
118 return -1;
119
120 if (b!='\n')
121 {
122 buf[off]=(byte)(0xff&b);
123 return 1+super.read(buf,off+1,len-1);
124 }
125 }
126 }
127
128 return super.read(buf,off,len);
129 }
130
131
132 }