Lines 68-89
Link Here
|
68 |
char result[]=new char[BUFFER_SIZE]; |
68 |
char result[]=new char[BUFFER_SIZE]; |
69 |
|
69 |
|
70 |
/** Convert a buffer of bytes into a chars |
70 |
/** Convert a buffer of bytes into a chars |
|
|
71 |
* @deprecated |
71 |
*/ |
72 |
*/ |
72 |
public void convert( ByteChunk bb, CharChunk cb ) |
73 |
public void convert( ByteChunk bb, CharChunk cb ) |
73 |
throws IOException |
74 |
throws IOException |
74 |
{ |
75 |
{ |
75 |
// Set the ByteChunk as input to the Intermediate reader |
76 |
convert(bb, cb, cb.getLimit() - cb.getEnd()); |
76 |
iis.setByteChunk( bb ); |
|
|
77 |
convert(cb); |
78 |
} |
77 |
} |
79 |
|
78 |
|
80 |
private void convert(CharChunk cb) |
79 |
/** Convert a buffer of bytes into a chars |
|
|
80 |
*/ |
81 |
public void convert( ByteChunk bb, CharChunk cb, int limit) |
82 |
throws IOException |
83 |
{ |
84 |
// Set the ByteChunk as input to the Intermediate reader |
85 |
iis.setByteChunk( bb ); |
86 |
convert(cb, limit); |
87 |
} |
88 |
|
89 |
private void convert(CharChunk cb, int limit) |
81 |
throws IOException |
90 |
throws IOException |
82 |
{ |
91 |
{ |
83 |
try { |
92 |
try { |
84 |
// read from the reader |
93 |
// read from the reader |
85 |
while( iis.available()>0 ) { // conv.ready() ) { |
94 |
while( limit > 0 ) { |
86 |
int cnt=conv.read( result, 0, BUFFER_SIZE ); |
95 |
int size = limit < BUFFER_SIZE ? limit : BUFFER_SIZE; |
|
|
96 |
int cnt=conv.read( result, 0, size ); |
87 |
if( cnt <= 0 ) { |
97 |
if( cnt <= 0 ) { |
88 |
// End of stream ! - we may be in a bad state |
98 |
// End of stream ! - we may be in a bad state |
89 |
if( debug>0) |
99 |
if( debug>0) |
Lines 96-101
Link Here
|
96 |
|
106 |
|
97 |
// XXX go directly |
107 |
// XXX go directly |
98 |
cb.append( result, 0, cnt ); |
108 |
cb.append( result, 0, cnt ); |
|
|
109 |
limit -= cnt; |
99 |
} |
110 |
} |
100 |
} catch( IOException ex) { |
111 |
} catch( IOException ex) { |
101 |
if( debug>0) |
112 |
if( debug>0) |
Lines 183-200
Link Here
|
183 |
* |
194 |
* |
184 |
*/ |
195 |
*/ |
185 |
final class ReadConvertor extends InputStreamReader { |
196 |
final class ReadConvertor extends InputStreamReader { |
186 |
// stream with flush() and close(). overriden. |
|
|
187 |
private IntermediateInputStream iis; |
188 |
|
197 |
|
189 |
// Has a private, internal byte[8192] |
198 |
private IntermediateInputStream iis; |
190 |
|
199 |
|
191 |
/** Create a converter. |
200 |
/** Create a converter. |
192 |
*/ |
201 |
*/ |
193 |
public ReadConvertor( IntermediateInputStream in, String enc ) |
202 |
public ReadConvertor( IntermediateInputStream in, String enc ) |
194 |
throws UnsupportedEncodingException |
203 |
throws UnsupportedEncodingException |
195 |
{ |
204 |
{ |
196 |
super( in, enc ); |
205 |
super( in, enc ); |
197 |
iis=in; |
206 |
iis=in; |
198 |
} |
207 |
} |
199 |
|
208 |
|
200 |
/** Overriden - will do nothing but reset internal state. |
209 |
/** Overriden - will do nothing but reset internal state. |
Lines 211-223
Link Here
|
211 |
return super.read( cbuf, off, len ); |
220 |
return super.read( cbuf, off, len ); |
212 |
} |
221 |
} |
213 |
|
222 |
|
214 |
public final int read() throws IOException { |
223 |
/* |
215 |
return super.read(); |
224 |
* Reset the buffer |
216 |
} |
|
|
217 |
|
218 |
/** Reset the buffer |
219 |
*/ |
225 |
*/ |
220 |
public final void recycle() { |
226 |
public final void recycle() { |
|
|
227 |
iis.recycle(); |
228 |
try{ |
229 |
// Must clear super's buffer. |
230 |
while(ready()){ |
231 |
// InputStreamReader#skip(long) will allocate buffer to skip. |
232 |
read(); |
233 |
} |
234 |
}catch(IOException ioe){} |
221 |
} |
235 |
} |
222 |
} |
236 |
} |
223 |
|
237 |
|
Lines 229-238
Link Here
|
229 |
not be called if recycling the converter and if data was not flushed. |
243 |
not be called if recycling the converter and if data was not flushed. |
230 |
*/ |
244 |
*/ |
231 |
final class IntermediateInputStream extends InputStream { |
245 |
final class IntermediateInputStream extends InputStream { |
232 |
byte buf[]; |
246 |
ByteChunk bc = null; |
233 |
int pos; |
|
|
234 |
int len; |
235 |
int end; |
236 |
|
247 |
|
237 |
public IntermediateInputStream() { |
248 |
public IntermediateInputStream() { |
238 |
} |
249 |
} |
Lines 243-306
Link Here
|
243 |
} |
254 |
} |
244 |
|
255 |
|
245 |
public final int read(byte cbuf[], int off, int len) throws IOException { |
256 |
public final int read(byte cbuf[], int off, int len) throws IOException { |
246 |
if( pos >= end ) return -1; |
257 |
if(bc == null){ |
247 |
if (pos + len > end) { |
258 |
return -1; |
248 |
len = end - pos; |
259 |
} |
249 |
} |
260 |
return bc.substract(cbuf, off, len); |
250 |
if (len <= 0) { |
|
|
251 |
return 0; |
252 |
} |
253 |
System.arraycopy(buf, pos, cbuf, off, len); |
254 |
pos += len; |
255 |
return len; |
256 |
} |
261 |
} |
257 |
|
262 |
|
258 |
public final int read() throws IOException { |
263 |
public final int read() throws IOException { |
259 |
return (pos < end ) ? (buf[pos++] & 0xff) : -1; |
264 |
if(bc == null){ |
|
|
265 |
return -1; |
266 |
} |
267 |
return bc.substract(); |
260 |
} |
268 |
} |
261 |
|
269 |
|
262 |
// -------------------- Internal methods -------------------- |
|
|
263 |
|
264 |
void setBuffer( byte b[], int p, int l ) { |
265 |
buf=b; |
266 |
pos=p; |
267 |
len=l; |
268 |
end=pos+len; |
269 |
} |
270 |
|
271 |
void setByteChunk( ByteChunk mb ) { |
272 |
buf=mb.getBytes(); |
273 |
pos=mb.getStart(); |
274 |
len=mb.getLength(); |
275 |
end=pos+len; |
276 |
} |
277 |
|
278 |
public int available() throws IOException { |
270 |
public int available() throws IOException { |
279 |
return end-pos; |
271 |
if(bc == null){ |
|
|
272 |
return 0; |
273 |
} |
274 |
return bc.getLength(); |
280 |
} |
275 |
} |
281 |
|
276 |
|
282 |
public boolean markSupported() { |
|
|
283 |
return false; |
284 |
} |
285 |
|
277 |
|
286 |
public int read(byte[] b) throws IOException { |
278 |
// -------------------- Internal methods -------------------- |
287 |
return read(b,0,b.length); |
|
|
288 |
} |
289 |
|
279 |
|
290 |
/** |
280 |
void recycle(){ |
291 |
* Repositions this stream to the position at the time the <code>mark</code> method was last called on this input |
281 |
bc = null; |
292 |
* stream. |
|
|
293 |
* |
294 |
* @throws IOException if this stream has not been marked or if the mark has been invalidated. |
295 |
* @todo Implement this java.io.InputStream method |
296 |
*/ |
297 |
public synchronized void reset() throws IOException { |
298 |
//not implemented |
299 |
} |
282 |
} |
300 |
|
283 |
|
301 |
public long skip(long n) throws IOException { |
284 |
void setByteChunk( ByteChunk mb ) { |
302 |
//not implemented |
285 |
bc = mb; |
303 |
return 0L; |
|
|
304 |
} |
286 |
} |
305 |
|
287 |
|
306 |
} |
288 |
} |