20
20
* =====
21
21
*/
22
22
23
- import java .io .IOException ;
24
- import java .io .OutputStream ;
23
+ import java .io .*;
25
24
import java .nio .ByteBuffer ;
26
25
import java .nio .CharBuffer ;
27
26
import java .nio .charset .Charset ;
@@ -133,10 +132,60 @@ public ContentType contentType() {
133
132
}
134
133
135
134
@ Override
136
- public long contentLength () throws IOException {
135
+ public long contentLength () {
137
136
return -1 ;
138
137
}
139
138
139
+ @ Override
140
+ public InputStream getContent () throws IOException {
141
+ SequenceInputStreamBuilder result = new SequenceInputStreamBuilder ();
142
+ try {
143
+ write (result );
144
+ return result .build ();
145
+ } catch (Exception e ) {
146
+ result .close ();
147
+ throw e ;
148
+ }
149
+ }
150
+
151
+ private void write (SequenceInputStreamBuilder result ) throws IOException {
152
+ ByteArrayBuffer boundaryEncoded = encode (StandardCharsets .US_ASCII , this .boundary );
153
+
154
+ for (int p = 0 , partCount = partHeaders .size (); p < partCount ; p ++) {
155
+ Headers headers = partHeaders .get (p );
156
+ RequestBody body = partBodies .get (p );
157
+
158
+ result .write (DASHDASH );
159
+ result .write (boundaryEncoded );
160
+ result .write (CRLF );
161
+
162
+ if (headers != null ) {
163
+ for (int h = 0 , headerCount = headers .size (); h < headerCount ; h ++) {
164
+ writeHeader (headers .name (h ), headers .value (h ), result );
165
+ }
166
+ }
167
+
168
+ ContentType contentType = body .contentType ();
169
+ if (contentType != null ) {
170
+ writeHeader ("Content-Type" , contentType .toString (), result );
171
+ }
172
+
173
+ long contentLength = body .contentLength ();
174
+ if (contentLength != -1 ) {
175
+ writeHeader ("Content-Length" , String .valueOf (contentLength ), result );
176
+ }
177
+
178
+ result .write (CRLF );
179
+ result .write (body .getContent ());
180
+ result .write (CRLF );
181
+ }
182
+
183
+ result .write (DASHDASH );
184
+ result .write (boundaryEncoded );
185
+ result .write (DASHDASH );
186
+ result .write (CRLF );
187
+ }
188
+
140
189
@ Override
141
190
public void writeTo (OutputStream out ) throws IOException {
142
191
ByteArrayBuffer boundaryEncoded = encode (StandardCharsets .US_ASCII , this .boundary );
@@ -178,6 +227,13 @@ public void writeTo(OutputStream out) throws IOException {
178
227
out .write (CRLF );
179
228
}
180
229
230
+ private void writeHeader (String name , String value , SequenceInputStreamBuilder out ) throws IOException {
231
+ out .write (encodeHeader (name ));
232
+ out .write (COLONSPACE );
233
+ out .write (encodeHeader (value ));
234
+ out .write (CRLF );
235
+ }
236
+
181
237
private void writeHeader (String name , String value , OutputStream out ) throws IOException {
182
238
write (encodeHeader (name ), out );
183
239
out .write (COLONSPACE );
@@ -200,4 +256,55 @@ private static void write(ByteArrayBuffer buff, OutputStream out) throws IOExcep
200
256
out .write (buff .array (), 0 , buff .length ());
201
257
}
202
258
}
259
+
260
+ static class SequenceInputStreamBuilder {
261
+
262
+ private final Vector <InputStream > streams = new Vector <>();
263
+ private final ByteArrayBuffer currentBuffer = new ByteArrayBuffer (1024 );
264
+
265
+ public void write (byte [] buff ) {
266
+ currentBuffer .append (buff );
267
+ }
268
+
269
+ public void write (ByteArrayBuffer buff ) {
270
+ currentBuffer .append (buff .array (), 0 , buff .length ());
271
+ }
272
+
273
+ public void write (InputStream stream ) {
274
+ flushCurrentBuffer ();
275
+
276
+ streams .add (stream );
277
+ }
278
+
279
+ public void close () throws IOException {
280
+ IOException ioe = null ;
281
+ for (InputStream in : streams ) {
282
+ try {
283
+ in .close ();
284
+ } catch (IOException e ) {
285
+ if (ioe == null ) {
286
+ ioe = e ;
287
+ } else {
288
+ ioe .addSuppressed (e );
289
+ }
290
+ }
291
+ }
292
+ if (ioe != null ) {
293
+ throw ioe ;
294
+ }
295
+ }
296
+
297
+ public InputStream build () {
298
+ flushCurrentBuffer ();
299
+
300
+ return new SequenceInputStream (streams .elements ());
301
+ }
302
+
303
+ private void flushCurrentBuffer () {
304
+ if (currentBuffer .length () > 0 ) {
305
+ streams .add (new ByteArrayInputStream (currentBuffer .toByteArray (), 0 , currentBuffer .length ()));
306
+ currentBuffer .clear ();
307
+ }
308
+ }
309
+ }
203
310
}
0 commit comments