1 |
/* |
/* |
2 |
* $Id$ |
* MessageInputStream.java |
3 |
* Copyright (C) 2002 The Free Software Foundation |
* Copyright (C) 2002 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
48 |
* sequence LF,END,LF is read from the underlying stream. |
* sequence LF,END,LF is read from the underlying stream. |
49 |
* |
* |
50 |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
51 |
*/ |
*/ |
52 |
public class MessageInputStream extends FilterInputStream |
public class MessageInputStream |
53 |
|
extends FilterInputStream |
54 |
{ |
{ |
55 |
|
|
56 |
/** |
/** |
74 |
/** |
/** |
75 |
* Constructs a message input stream connected to the specified input stream. |
* Constructs a message input stream connected to the specified input stream. |
76 |
*/ |
*/ |
77 |
public MessageInputStream (InputStream in) |
public MessageInputStream(InputStream in) |
78 |
{ |
{ |
79 |
super (in); |
super(in); |
80 |
eof = false; |
eof = false; |
81 |
} |
} |
82 |
|
|
83 |
/** |
/** |
84 |
* Reads the next byte of data from this message input stream. |
* Reads the next byte of data from this message input stream. |
85 |
* Returns -1 if the end of the message stream has been reached. |
* Returns -1 if the end of the message stream has been reached. |
86 |
* @exception IOException if an I/O error occurs |
* @exception IOException if an I/O error occurs |
87 |
*/ |
*/ |
88 |
public int read () throws IOException |
public int read() |
89 |
{ |
throws IOException |
90 |
if (eof) |
{ |
91 |
{ |
if (eof) |
92 |
return -1; |
{ |
93 |
} |
return -1; |
94 |
int c; |
} |
95 |
if (buf1 != -1) |
int c; |
96 |
{ |
if (buf1 != -1) |
97 |
c = buf1; |
{ |
98 |
buf1 = buf2; |
c = buf1; |
99 |
buf2 = -1; |
buf1 = buf2; |
100 |
} |
buf2 = -1; |
101 |
else |
} |
102 |
{ |
else |
103 |
c = super.read (); |
{ |
104 |
} |
c = super.read(); |
105 |
if (c == LF) |
} |
106 |
{ |
if (c == LF) |
107 |
if (buf1 == -1) |
{ |
108 |
{ |
if (buf1 == -1) |
109 |
buf1 = super.read (); |
{ |
110 |
if (buf1 == END) |
buf1 = super.read(); |
111 |
{ |
if (buf1 == END) |
112 |
buf2 = super.read (); |
{ |
113 |
if (buf2 == LF) |
buf2 = super.read(); |
114 |
{ |
if (buf2 == LF) |
115 |
eof = true; |
{ |
116 |
// Allow the final LF to be read |
eof = true; |
117 |
} |
// Allow the final LF to be read |
118 |
} |
} |
119 |
} |
} |
120 |
else if (buf1 == END) |
} |
121 |
{ |
else if (buf1 == END) |
122 |
if (buf2 == -1) |
{ |
123 |
{ |
if (buf2 == -1) |
124 |
buf2 = super.read (); |
{ |
125 |
if (buf2 == LF) |
buf2 = super.read(); |
126 |
{ |
if (buf2 == LF) |
127 |
eof = true; |
{ |
128 |
} |
eof = true; |
129 |
} |
} |
130 |
else if (buf2 == LF) |
} |
131 |
{ |
else if (buf2 == LF) |
132 |
eof = true; |
{ |
133 |
} |
eof = true; |
134 |
} |
} |
135 |
} |
} |
136 |
return c; |
} |
137 |
} |
return c; |
138 |
|
} |
139 |
|
|
140 |
/** |
/** |
141 |
* Reads up to b.length bytes of data from this input stream into |
* Reads up to b.length bytes of data from this input stream into |
142 |
* an array of bytes. |
* an array of bytes. |
143 |
* Returns -1 if the end of the stream has been reached. |
* Returns -1 if the end of the stream has been reached. |
144 |
* @exception IOException if an I/O error occurs |
* @exception IOException if an I/O error occurs |
145 |
*/ |
*/ |
146 |
public int read (byte[] b) throws IOException |
public int read(byte[] b) |
147 |
{ |
throws IOException |
148 |
return read (b, 0, b.length); |
{ |
149 |
} |
return read(b, 0, b.length); |
150 |
|
} |
151 |
|
|
152 |
/** |
/** |
153 |
* Reads up to len bytes of data from this input stream into an |
* Reads up to len bytes of data from this input stream into an |
155 |
* Returns -1 if the end of the stream has been reached. |
* Returns -1 if the end of the stream has been reached. |
156 |
* @exception IOException if an I/O error occurs |
* @exception IOException if an I/O error occurs |
157 |
*/ |
*/ |
158 |
public int read (byte[] b, int off, int len) throws IOException |
public int read(byte[] b, int off, int len) |
159 |
{ |
throws IOException |
160 |
if (eof) |
{ |
161 |
{ |
if (eof) |
162 |
return -1; |
{ |
163 |
} |
return -1; |
164 |
int c, end = off + len; |
} |
165 |
for (int i = off; i < end; i++) |
int c, end = off + len; |
166 |
{ |
for (int i = off; i < end; i++) |
167 |
c = read (); |
{ |
168 |
if (c == -1) |
c = read(); |
169 |
{ |
if (c == -1) |
170 |
len = i - off; |
{ |
171 |
break; |
len = i - off; |
172 |
} |
break; |
173 |
else |
} |
174 |
{ |
else |
175 |
b[i] = (byte) c; |
{ |
176 |
} |
b[i] = (byte) c; |
177 |
} |
} |
178 |
return len; |
} |
179 |
} |
return len; |
180 |
|
} |
181 |
public boolean markSupported () |
|
182 |
{ |
public boolean markSupported() |
183 |
return in.markSupported (); |
{ |
184 |
} |
return in.markSupported(); |
185 |
|
} |
186 |
public void mark (int readlimit) |
|
187 |
{ |
public void mark(int readlimit) |
188 |
in.mark (readlimit); |
{ |
189 |
markBuf1 = buf1; |
in.mark(readlimit); |
190 |
markBuf2 = buf2; |
markBuf1 = buf1; |
191 |
} |
markBuf2 = buf2; |
192 |
|
} |
193 |
public void reset () throws IOException |
|
194 |
{ |
public void reset() |
195 |
in.reset (); |
throws IOException |
196 |
buf1 = markBuf1; |
{ |
197 |
buf2 = markBuf2; |
in.reset(); |
198 |
} |
buf1 = markBuf1; |
199 |
|
buf2 = markBuf2; |
200 |
|
} |
201 |
|
|
202 |
} |
} |
203 |
|
|