-
Notifications
You must be signed in to change notification settings - Fork 1
/
StreamSplit.java
183 lines (162 loc) · 4.83 KB
/
StreamSplit.java
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
** com/charliemouse/cambozola/shared/StreamSplit.java
** Copyright (C) Andy Wilcock, 2001.
** Available from http://www.charliemouse.com
**
** This file m_inputStream part of the CamViewer package (c) Andy Wilcock, 2001.
** Available from http://www.charliemouse.com
**
** Cambozola m_inputStream free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** Cambozola m_inputStream distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Cambozola; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**/
//package com.charliemouse.cambozola.shared;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.Hashtable;
import java.net.URLConnection;
public class StreamSplit {
public static final String BOUNDARY_MARKER_PREFIX = "--";
public static final String BOUNDARY_MARKER_TERM = BOUNDARY_MARKER_PREFIX;
protected DataInputStream m_dis;
private boolean m_streamEnd;
public StreamSplit(DataInputStream dis)
{
m_dis = dis;
m_streamEnd = false;
}
public Hashtable readHeaders() throws IOException
{
Hashtable ht = new Hashtable();
String response;
boolean satisfied = false;
do {
//noinspection deprecation
response = m_dis.readLine();
if (response == null) {
m_streamEnd = true;
break;
} else if (response.equals("")) {
if (satisfied) {
break;
} else {
// Carry on...
}
} else {
satisfied = true;
}
addPropValue(response, ht);
} while (true);
return ht;
}
protected static void addPropValue(String response, Hashtable ht)
{
int idx = response.indexOf(":");
if (idx == -1) {
return;
}
String tag = response.substring(0, idx);
String val = response.substring(idx + 1).trim();
ht.put(tag.toLowerCase(), val);
}
public static Hashtable readHeaders(URLConnection conn)
{
Hashtable ht = new Hashtable();
int i = 0;
do {
String key = conn.getHeaderFieldKey(i);
if (key == null) {
if (i == 0) {
i++;
continue;
} else {
break;
}
}
String val = conn.getHeaderField(i);
ht.put(key.toLowerCase(), val);
i++;
} while (true);
return ht;
}
public void skipToBoundary(String boundary) throws IOException
{
readToBoundary(boundary);
}
public byte[] readToBoundary(String boundary) throws IOException
{
ResizableByteArrayOutputStream baos = new ResizableByteArrayOutputStream();
StringBuffer lastLine = new StringBuffer();
int lineidx = 0;
int chidx = 0;
byte ch;
do {
try {
ch = m_dis.readByte();
} catch (EOFException e) {
m_streamEnd = true;
break;
}
if (ch == '\n' || ch == '\r') {
//
// End of line... Note, this will now look for the boundary
// within the line - more flexible as it can habdle
// arfle--boundary\n as well as
// arfle\n--boundary\n
//
String lls = lastLine.toString();
int idx = lls.indexOf(BOUNDARY_MARKER_PREFIX);
if (idx != -1) {
lls = lastLine.substring(idx);
if (lls.startsWith(boundary)) {
//
// Boundary found - check for termination
//
String btest = lls.substring(boundary.length());
if (btest.equals(BOUNDARY_MARKER_TERM)) {
m_streamEnd = true;
}
chidx = lineidx+idx;
break;
}
}
lastLine = new StringBuffer();
lineidx = chidx + 1;
} else {
lastLine.append((char) ch);
}
chidx++;
baos.write(ch);
} while (true);
//
baos.close();
baos.resize(chidx);
return baos.toByteArray();
}
public boolean isAtStreamEnd()
{
return m_streamEnd;
}
}
class ResizableByteArrayOutputStream extends ByteArrayOutputStream {
public ResizableByteArrayOutputStream()
{
super();
}
public void resize(int size)
{
count = size;
}
}