forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChunkedInputStream.java
More file actions
146 lines (126 loc) · 3.88 KB
/
Copy pathChunkedInputStream.java
File metadata and controls
146 lines (126 loc) · 3.88 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.bridge.http;
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import php.java.bridge.Util;
/**
* An output stream which reads data in HTTP chunks.
*
* @author jostb
*/
public class ChunkedInputStream extends FilterInputStream {
/**
* Maps ASCII to HEX code.
*/
public static final int[] ASCII_HEX = getAscii();
private boolean eof = false;
private static int[] getAscii() {
int[] ascii = new int[103];
for (int i=48; i<58; i++) {
ascii[i]=i-48;
};
for (int i=65; i<71; i++) {
ascii[i]=i-55;
};
for (int i=97; i<103; i++) {
ascii[i]=i-87;
};
return ascii;
}
/**
* Create a new chunked input stream
* @param in The input stream
*/
public ChunkedInputStream(InputStream in) {
super(new BufferedInputStream(in));
}
private byte[] rn = new byte[2];
private byte[] remaining;
private int remainPos, remainLen;
/**{@inheritDoc}*/
public int read(byte[] buf, int pos, int len) throws IOException {
int c, i;
int count;
if (len <= 0) return len;
if (eof) return -1;
// check remaining
if (remaining != null) {
if (len < remainLen) {
System.arraycopy(remaining, remainPos, buf, pos, len);
remainPos += len;
remainLen -= len;
return len;
} else {
System.arraycopy(remaining, remainPos, buf, pos, remainLen);
remaining = null;
return remainLen;
}
}
// read next packet
int packetLen = readPacketLength();
if (packetLen == 0) eof = true;
remaining = new byte[packetLen]; remainLen = 0;
for(c=0; (i=in.read(remaining, c, packetLen-c)) > 0; c+=i)
;
if ((c != packetLen)) throw new IOException("read chunked");
for(c=0; (i=in.read(rn, c, 2-c)) > 0; c+=i)
;
if ((c != 2)) throw new IOException("read \r\n");
// store remaining
if (len >= packetLen) {
count = packetLen;
System.arraycopy(remaining, 0, buf, pos, packetLen);
remaining = null;
} else {
count = len;
System.arraycopy(remaining, 0, buf, pos, len);
remainPos = len;
remainLen = packetLen - len;
}
return count;
}
private int readPacketLength() throws IOException {
int n = 0;
int c;
while ((c = in.read()) != '\r') {
if (c == -1) throw new IllegalStateException ("read chunked packet length");
if (c == 32) continue; // skip white space
n <<= 4;
n += ASCII_HEX[c];
}
if (in.read() == -1) throw new IOException ("read chunked packet length");
return n;
}
private byte[] buf = new byte[Util.BUF_SIZE];
/**
* read trailing 0\r\n
* @throws IOException
*/
public void eof() throws IOException {
// consume remaining 0\r\n
read(buf, 0, buf.length);
}
}