12
12
*/
13
13
package org .asynchttpclient .multipart ;
14
14
15
- import java .io .ByteArrayInputStream ;
16
15
import java .io .IOException ;
17
16
import java .io .RandomAccessFile ;
18
17
import java .nio .ByteBuffer ;
@@ -38,8 +37,8 @@ public class MultipartBody implements RandomAccessBody {
38
37
private boolean transfertDone = false ;
39
38
40
39
private int currentPart = 0 ;
41
- private ByteArrayInputStream currentStream ;
42
- private int currentStreamPosition = -1 ;
40
+ private byte [] currentBytes ;
41
+ private int currentBytesPosition = -1 ;
43
42
private boolean doneWritingParts = false ;
44
43
private FileLocation fileLocation = FileLocation .NONE ;
45
44
private FileChannel currentFileChannel ;
@@ -112,17 +111,18 @@ public long read(ByteBuffer buffer) throws IOException {
112
111
overallLength += writeCurrentFile (buffer );
113
112
full = overallLength == maxLength ;
114
113
115
- } else if (currentStreamPosition > -1 ) {
116
- overallLength += writeCurrentStream (buffer , maxLength - overallLength );
114
+ } else if (currentBytesPosition > -1 ) {
115
+ overallLength += writeCurrentBytes (buffer , maxLength - overallLength );
117
116
full = overallLength == maxLength ;
118
117
119
- if (currentPart == parts .size () && currentStream . available () == 0 ) {
118
+ if (currentPart == parts .size () && currentBytesFullyRead () ) {
120
119
doneWritingParts = true ;
121
120
}
122
121
123
122
} else if (part instanceof StringPart ) {
124
123
StringPart stringPart = (StringPart ) part ;
125
- initializeNewCurrentStream (stringPart .getBytes (boundary ));
124
+ // set new bytes, not full, so will loop to writeCurrentBytes above
125
+ initializeCurrentBytes (stringPart .getBytes (boundary ));
126
126
currentPart ++;
127
127
128
128
} else if (part instanceof AbstractFilePart ) {
@@ -131,8 +131,8 @@ public long read(ByteBuffer buffer) throws IOException {
131
131
132
132
switch (fileLocation ) {
133
133
case NONE :
134
- // create new stream , not full, so will loop to writeCurrentStream above
135
- initializeNewCurrentStream (filePart .generateFileStart (boundary ));
134
+ // set new bytes , not full, so will loop to writeCurrentBytes above
135
+ initializeCurrentBytes (filePart .generateFileStart (boundary ));
136
136
fileLocation = FileLocation .START ;
137
137
break ;
138
138
case START :
@@ -141,30 +141,30 @@ public long read(ByteBuffer buffer) throws IOException {
141
141
fileLocation = FileLocation .MIDDLE ;
142
142
break ;
143
143
case MIDDLE :
144
- initializeNewCurrentStream (filePart .generateFileEnd ());
144
+ initializeCurrentBytes (filePart .generateFileEnd ());
145
145
fileLocation = FileLocation .END ;
146
146
break ;
147
147
case END :
148
148
currentPart ++;
149
149
fileLocation = FileLocation .NONE ;
150
- if (currentPart == parts .size () && currentStream . available () == 0 ) {
150
+ if (currentPart == parts .size ()) {
151
151
doneWritingParts = true ;
152
152
}
153
153
}
154
154
}
155
155
}
156
156
157
157
if (doneWritingParts ) {
158
- if (currentStreamPosition == -1 ) {
159
- initializeNewCurrentStream (MultipartUtils .getMessageEnd (boundary ));
158
+ if (currentBytesPosition == -1 ) {
159
+ initializeCurrentBytes (MultipartUtils .getMessageEnd (boundary ));
160
160
}
161
161
162
- if (currentStreamPosition > -1 ) {
163
- overallLength += writeCurrentStream (buffer , maxLength - overallLength );
162
+ if (currentBytesPosition > -1 ) {
163
+ overallLength += writeCurrentBytes (buffer , maxLength - overallLength );
164
164
165
- if (currentStream . available () == 0 ) {
166
- currentStream . close () ;
167
- currentStreamPosition = -1 ;
165
+ if (currentBytesFullyRead () ) {
166
+ currentBytes = null ;
167
+ currentBytesPosition = -1 ;
168
168
transfertDone = true ;
169
169
}
170
170
}
@@ -177,6 +177,10 @@ public long read(ByteBuffer buffer) throws IOException {
177
177
}
178
178
}
179
179
180
+ private boolean currentBytesFullyRead () {
181
+ return currentBytes == null || currentBytesPosition >= currentBytes .length - 1 ;
182
+ }
183
+
180
184
private void initializeFileBody (AbstractFilePart part ) throws IOException {
181
185
182
186
if (part instanceof FilePart ) {
@@ -185,16 +189,16 @@ private void initializeFileBody(AbstractFilePart part) throws IOException {
185
189
currentFileChannel = raf .getChannel ();
186
190
187
191
} else if (part instanceof ByteArrayPart ) {
188
- initializeNewCurrentStream (ByteArrayPart .class .cast (part ).getBytes ());
192
+ initializeCurrentBytes (ByteArrayPart .class .cast (part ).getBytes ());
189
193
190
194
} else {
191
195
throw new IllegalArgumentException ("Unknow AbstractFilePart type" );
192
196
}
193
197
}
194
198
195
- private void initializeNewCurrentStream (byte [] bytes ) throws IOException {
196
- currentStream = new ByteArrayInputStream ( bytes ) ;
197
- currentStreamPosition = 0 ;
199
+ private void initializeCurrentBytes (byte [] bytes ) throws IOException {
200
+ currentBytes = bytes ;
201
+ currentBytesPosition = 0 ;
198
202
}
199
203
200
204
private int writeCurrentFile (ByteBuffer buffer ) throws IOException {
@@ -214,23 +218,20 @@ private int writeCurrentFile(ByteBuffer buffer) throws IOException {
214
218
return read ;
215
219
}
216
220
217
- private int writeCurrentStream (ByteBuffer buffer , int length ) throws IOException {
221
+ private int writeCurrentBytes (ByteBuffer buffer , int length ) throws IOException {
218
222
219
- int available = currentStream . available () ;
223
+ int available = currentBytes . length - currentBytesPosition ;
220
224
221
225
int writeLength = Math .min (available , length );
222
226
223
227
if (writeLength > 0 ) {
224
- byte [] bytes = new byte [writeLength ];
225
-
226
- currentStream .read (bytes );
227
- buffer .put (bytes );
228
+ buffer .put (currentBytes , currentBytesPosition , writeLength );
228
229
229
230
if (available <= length ) {
230
- currentStream . close () ;
231
- currentStreamPosition = - 1 ;
231
+ currentBytesPosition = - 1 ;
232
+ currentBytes = null ;
232
233
} else {
233
- currentStreamPosition += writeLength ;
234
+ currentBytesPosition += writeLength ;
234
235
}
235
236
}
236
237
0 commit comments