13
13
package com .ning .http .multipart ;
14
14
15
15
import com .ning .http .client .RandomAccessBody ;
16
+ import org .slf4j .Logger ;
17
+ import org .slf4j .LoggerFactory ;
16
18
17
19
import java .io .ByteArrayOutputStream ;
18
20
import java .io .File ;
19
- import java .io .FileNotFoundException ;
20
21
import java .io .IOException ;
22
+ import java .io .InputStream ;
21
23
import java .io .RandomAccessFile ;
22
24
import java .nio .ByteBuffer ;
23
25
import java .nio .channels .FileChannel ;
@@ -32,6 +34,7 @@ public class MultipartBody implements RandomAccessBody {
32
34
private List <com .ning .http .client .Part > parts ;
33
35
private List <RandomAccessFile > files ;
34
36
private int startPart ;
37
+ private final static Logger logger = LoggerFactory .getLogger (MultipartBody .class );
35
38
36
39
public MultipartBody (List <com .ning .http .client .Part > parts , String boundary , String contentLength ) {
37
40
this .boundary = MultipartEncodingUtil .getAsciiBytes (boundary .substring ("multipart/form-data; boundary=" .length ()));
@@ -133,16 +136,14 @@ private long handleByteArrayPart(WritableByteChannel target,
133
136
private long handleFileEnd (WritableByteChannel target , FilePart filePart )
134
137
throws IOException {
135
138
136
- ByteArrayOutputStream endOverhead =
137
- new ByteArrayOutputStream ();
139
+ ByteArrayOutputStream endOverhead = new ByteArrayOutputStream ();
138
140
139
141
filePart .sendEnd (endOverhead );
140
142
141
143
return this .writeToTarget (target , endOverhead );
142
144
}
143
145
144
- private long handleFileHeaders (WritableByteChannel target ,
145
- FilePart filePart ) throws IOException {
146
+ private long handleFileHeaders (WritableByteChannel target , FilePart filePart ) throws IOException {
146
147
filePart .setPartBoundary (boundary );
147
148
148
149
ByteArrayOutputStream overhead = new ByteArrayOutputStream ();
@@ -158,49 +159,72 @@ private long handleFileHeaders(WritableByteChannel target,
158
159
return writeToTarget (target , overhead );
159
160
}
160
161
161
- private long handleFilePart (WritableByteChannel target , FilePart filePart )
162
- throws IOException , FileNotFoundException {
162
+ private long handleFilePart (WritableByteChannel target , FilePart filePart ) throws IOException {
163
163
164
- int length = 0 ;
164
+ if ( FilePartSource .class .isAssignableFrom ( filePart .getSource ().getClass ())) {
165
+ int length = 0 ;
165
166
166
- length += handleFileHeaders (target , filePart );
167
+ length += handleFileHeaders (target , filePart );
168
+ FilePartSource source = (FilePartSource ) filePart .getSource ();
167
169
168
- FilePartSource source = ( FilePartSource ) filePart . getSource ();
170
+ File file = source . getFile ();
169
171
170
- File file = source .getFile ();
172
+ RandomAccessFile raf = new RandomAccessFile (file , "r" );
173
+ files .add (raf );
171
174
172
- RandomAccessFile raf = new RandomAccessFile (file , "r" );
173
- files .add (raf );
175
+ FileChannel fc = raf .getChannel ();
174
176
175
- FileChannel fc = raf . getChannel ( );
177
+ long fileLength = fc . transferTo ( 0 , file . length (), target );
176
178
179
+ if (fileLength != file .length ()) {
180
+ logger .info ("Did not complete file." );
181
+ }
177
182
178
- long fileLength = fc . transferTo ( 0 , file . length (), target );
183
+ length += handleFileEnd ( target , filePart );
179
184
180
- if (fileLength != file .length ()) {
181
- System .out .println ("Did not complete file." );
185
+ return length ;
186
+ } else {
187
+ return handlePartSource (target , filePart );
182
188
}
189
+ }
190
+
191
+ private long handlePartSource (WritableByteChannel target , FilePart filePart ) throws IOException {
192
+
193
+ int length = 0 ;
183
194
195
+ length += handleFileHeaders (target , filePart );
196
+
197
+ PartSource partSource = filePart .getSource ();
198
+
199
+ InputStream stream = partSource .createInputStream ();
200
+
201
+ int nRead = 0 ;
202
+ byte [] bytes = new byte [(int )partSource .getLength ()];
203
+ while (nRead != -1 ) {
204
+ nRead = stream .read (bytes );
205
+ if (nRead > 0 ) {
206
+ ByteArrayOutputStream bos = new ByteArrayOutputStream (nRead );
207
+ bos .write (bytes , 0 , nRead );
208
+ writeToTarget (target , bos );
209
+ }
210
+ }
184
211
length += handleFileEnd (target , filePart );
185
212
186
213
return length ;
187
214
}
188
215
189
- private long handleStringPart (WritableByteChannel target , StringPart currentPart )
190
- throws IOException {
216
+ private long handleStringPart (WritableByteChannel target , StringPart currentPart ) throws IOException {
191
217
192
218
currentPart .setPartBoundary (boundary );
193
219
194
- ByteArrayOutputStream outputStream =
195
- new ByteArrayOutputStream ();
220
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
196
221
197
222
Part .sendPart (outputStream , currentPart , boundary );
198
223
199
224
return writeToTarget (target , outputStream );
200
225
}
201
226
202
- private long handleMultiPart (WritableByteChannel target , Part currentPart )
203
- throws IOException , FileNotFoundException {
227
+ private long handleMultiPart (WritableByteChannel target , Part currentPart ) throws IOException {
204
228
205
229
currentPart .setPartBoundary (boundary );
206
230
@@ -222,13 +246,13 @@ private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byt
222
246
while ((target .isOpen ()) && (written < byteWriter .size ())) {
223
247
ByteBuffer message = ByteBuffer .wrap (byteWriter .toByteArray ());
224
248
written = target .write (message );
249
+ // TODO: This is dangerous to spin
225
250
if (written != byteWriter .size ()) {
226
- System . out . println ("Waiting..." );
251
+ logger . info ("Waiting for writing ..." );
227
252
try {
228
253
byteWriter .wait (1000 );
229
254
} catch (InterruptedException e ) {
230
- // TODO Auto-generated catch block
231
- e .printStackTrace ();
255
+ logger .trace (e .getMessage (), e );
232
256
}
233
257
}
234
258
}
0 commit comments