-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathChunkedInputStream.java
More file actions
104 lines (85 loc) · 1.96 KB
/
ChunkedInputStream.java
File metadata and controls
104 lines (85 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package HTTPClient;
import java.io.IOException;
import java.io.EOFException;
import java.io.InputStream;
import java.io.FilterInputStream;
/**
* This class de-chunks an input stream.
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschal�r
*/
class ChunkedInputStream extends FilterInputStream
{
/**
* @param is the input stream to dechunk
*/
ChunkedInputStream(InputStream is)
{
super(is);
}
byte[] one = new byte[1];
public synchronized int read() throws IOException
{
int b = read(one, 0, 1);
if (b == 1)
return (one[0] & 0xff);
else
return -1;
}
private long chunk_len = -1;
private boolean eof = false;
public synchronized int read(byte[] buf, int off, int len)
throws IOException
{
if (eof) return -1;
if (chunk_len == -1) // it's a new chunk
{
try
{ chunk_len = Codecs.getChunkLength(in); }
catch (ParseException pe)
{ throw new IOException(pe.toString()); }
}
if (chunk_len > 0) // it's data
{
if (len > chunk_len) len = (int) chunk_len;
int rcvd = in.read(buf, off, len);
if (rcvd == -1)
throw new EOFException("Premature EOF encountered");
chunk_len -= rcvd;
if (chunk_len == 0) // got the whole chunk
{
in.read(); // CR
in.read(); // LF
chunk_len = -1;
}
return rcvd;
}
else // the footers (trailers)
{
// discard
Request dummy =
new Request(null, null, null, null, null, null, false);
new Response(dummy, null).readTrailers(in);
eof = true;
return -1;
}
}
public synchronized long skip(long num) throws IOException
{
byte[] tmp = new byte[(int) num];
int got = read(tmp, 0, (int) num);
if (got > 0)
return (long) got;
else
return 0L;
}
public synchronized int available() throws IOException
{
if (eof) return 0;
if (chunk_len != -1)
return (int) chunk_len + in.available();
else
return in.available();
}
}