3
3
4
4
import com .ning .http .client .RandomAccessBody ;
5
5
6
+ import java .io .ByteArrayInputStream ;
6
7
import java .io .ByteArrayOutputStream ;
7
8
import java .io .File ;
9
+ import java .io .FileNotFoundException ;
8
10
import java .io .IOException ;
9
11
import java .io .RandomAccessFile ;
10
12
import java .nio .ByteBuffer ;
@@ -49,78 +51,18 @@ public long transferTo(long position, long count, WritableByteChannel target)
49
51
return overallLength ;
50
52
}
51
53
52
- long availableLength = count ;
53
-
54
54
int tempPart = _startPart ;
55
- long totalLength = 0 ;
56
- boolean full = false ;
57
-
58
- while (!full && tempPart < _parts .size ()) {
59
- Part currentPart = (Part ) _parts .get (tempPart );
60
-
61
- currentPart .setPartBoundary (_boundary );
62
55
63
- long length = currentPart .length ();
64
-
65
- if ((length + totalLength ) < availableLength ) {
66
- totalLength += length ;
67
- tempPart ++;
68
-
69
- if (currentPart .getClass ().equals (StringPart .class )) {
70
-
71
- ByteArrayOutputStream outputStream =
72
- new ByteArrayOutputStream ();
73
-
74
- Part .sendPart (outputStream , currentPart , _boundary );
75
-
76
- overallLength += writeToTarget (target , outputStream );
77
- }
78
- else if (currentPart .getClass ().equals (FilePart .class )) {
79
-
80
- FilePart filePart = (FilePart )currentPart ;
81
-
82
- ByteArrayOutputStream overhead =
83
- new ByteArrayOutputStream ();
84
-
85
- filePart .setPartBoundary (_boundary );
86
-
87
- filePart .sendStart (overhead );
88
- filePart .sendDispositionHeader (overhead );
89
- filePart .sendContentTypeHeader (overhead );
90
- filePart .sendTransferEncodingHeader (overhead );
91
- filePart .sendEndOfHeader (overhead );
92
-
93
- overallLength += writeToTarget (target , overhead );
94
-
95
- FilePartSource source = (FilePartSource )filePart .getSource ();
96
-
97
- File file = source .getFile ();
98
-
99
- RandomAccessFile raf = new RandomAccessFile (file , "r" );
100
- _files .add (raf );
101
-
102
- FileChannel fc = raf .getChannel ();
103
-
104
-
105
- long fileLength = fc .transferTo (0 , file .length (), target );
106
-
107
- if (fileLength != file .length ()) {
108
- System .out .println ("Did not complete file." );
109
- }
110
-
111
- ByteArrayOutputStream endOverhead =
112
- new ByteArrayOutputStream ();
113
-
114
- filePart .sendEnd (endOverhead );
115
-
116
- overallLength += this .writeToTarget (target , endOverhead );
117
- }
56
+ for (com .ning .http .client .Part part : _parts ) {
57
+ if (part instanceof Part ) {
58
+ overallLength += handleMultiPart (target , (Part )part );
118
59
}
119
60
else {
120
- full = true ;
61
+ overallLength += handleClientPart ( target , part ) ;
121
62
}
122
- }
123
63
64
+ tempPart ++;
65
+ }
124
66
ByteArrayOutputStream endWriter =
125
67
new ByteArrayOutputStream ();
126
68
@@ -133,6 +75,140 @@ else if(currentPart.getClass().equals(FilePart.class)) {
133
75
return overallLength ;
134
76
}
135
77
78
+ private long handleClientPart (
79
+ WritableByteChannel target , com .ning .http .client .Part part ) throws IOException {
80
+
81
+ if (part .getClass ().equals (com .ning .http .client .StringPart .class )) {
82
+ com .ning .http .client .StringPart stringPart = (com .ning .http .client .StringPart )part ;
83
+
84
+ StringPart currentPart = new StringPart (stringPart .getName (), stringPart .getValue ());
85
+
86
+ return handleStringPart (target ,currentPart );
87
+ }
88
+ else if (part .getClass ().equals (com .ning .http .client .FilePart .class )) {
89
+ com .ning .http .client .FilePart currentPart = (com .ning .http .client .FilePart )part ;
90
+
91
+ FilePart filePart = new FilePart (currentPart .getName (), currentPart .getFile ());
92
+
93
+ return handleFilePart (target , filePart );
94
+ }
95
+ else if (part .getClass ().equals (com .ning .http .client .ByteArrayPart .class )) {
96
+ com .ning .http .client .ByteArrayPart bytePart = (com .ning .http .client .ByteArrayPart )part ;
97
+
98
+ ByteArrayPartSource source = new ByteArrayPartSource (bytePart .getFileName (), bytePart .getData ());
99
+
100
+ FilePart filePart = new FilePart (bytePart .getName (), source , bytePart .getMimeType (), bytePart .getCharSet ());
101
+
102
+ return handleByteArrayPart (target , filePart , bytePart .getData ());
103
+ }
104
+
105
+ return 0 ;
106
+ }
107
+
108
+ private long handleByteArrayPart (WritableByteChannel target ,
109
+ FilePart filePart , byte [] data ) throws IOException {
110
+
111
+ int length = 0 ;
112
+
113
+ //length += handleFileHeaders(target, filePart);
114
+
115
+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
116
+
117
+ Part .sendPart (output , filePart , _boundary );
118
+
119
+ length += writeToTarget (target , output );
120
+
121
+ //length += handleFileEnd(target, filePart);
122
+
123
+ return length ;
124
+
125
+ }
126
+
127
+ private long handleFileEnd (WritableByteChannel target , FilePart filePart )
128
+ throws IOException {
129
+
130
+ ByteArrayOutputStream endOverhead =
131
+ new ByteArrayOutputStream ();
132
+
133
+ filePart .sendEnd (endOverhead );
134
+
135
+ return this .writeToTarget (target , endOverhead );
136
+ }
137
+
138
+ private long handleFileHeaders (WritableByteChannel target ,
139
+ FilePart filePart ) throws IOException {
140
+ filePart .setPartBoundary (_boundary );
141
+
142
+ ByteArrayOutputStream overhead = new ByteArrayOutputStream ();
143
+
144
+ filePart .setPartBoundary (_boundary );
145
+
146
+ filePart .sendStart (overhead );
147
+ filePart .sendDispositionHeader (overhead );
148
+ filePart .sendContentTypeHeader (overhead );
149
+ filePart .sendTransferEncodingHeader (overhead );
150
+ filePart .sendEndOfHeader (overhead );
151
+
152
+ return writeToTarget (target , overhead );
153
+ }
154
+
155
+ private long handleFilePart (WritableByteChannel target , FilePart filePart )
156
+ throws IOException , FileNotFoundException {
157
+
158
+ int length = 0 ;
159
+
160
+ length += handleFileHeaders (target , filePart );
161
+
162
+ FilePartSource source = (FilePartSource )filePart .getSource ();
163
+
164
+ File file = source .getFile ();
165
+
166
+ RandomAccessFile raf = new RandomAccessFile (file , "r" );
167
+ _files .add (raf );
168
+
169
+ FileChannel fc = raf .getChannel ();
170
+
171
+
172
+ long fileLength = fc .transferTo (0 , file .length (), target );
173
+
174
+ if (fileLength != file .length ()) {
175
+ System .out .println ("Did not complete file." );
176
+ }
177
+
178
+ length += handleFileEnd (target , filePart );
179
+
180
+ return length ;
181
+ }
182
+
183
+ private long handleStringPart (WritableByteChannel target , StringPart currentPart )
184
+ throws IOException {
185
+
186
+ currentPart .setPartBoundary (_boundary );
187
+
188
+ ByteArrayOutputStream outputStream =
189
+ new ByteArrayOutputStream ();
190
+
191
+ Part .sendPart (outputStream , currentPart , _boundary );
192
+
193
+ return writeToTarget (target , outputStream );
194
+ }
195
+
196
+ private long handleMultiPart (WritableByteChannel target , Part currentPart )
197
+ throws IOException , FileNotFoundException {
198
+
199
+ currentPart .setPartBoundary (_boundary );
200
+
201
+ if (currentPart .getClass ().equals (StringPart .class )) {
202
+ return handleStringPart (target , (StringPart )currentPart );
203
+ }
204
+ else if (currentPart .getClass ().equals (FilePart .class )) {
205
+ FilePart filePart = (FilePart )currentPart ;
206
+
207
+ return handleFilePart (target , filePart );
208
+ }
209
+ return 0 ;
210
+ }
211
+
136
212
private long writeToTarget (
137
213
WritableByteChannel target , ByteArrayOutputStream byteWriter )
138
214
throws IOException {
0 commit comments