Skip to content

Commit 1ce2e01

Browse files
author
Stephane Landelle
committed
No more ByteArrayInputStream
1 parent d70c47d commit 1ce2e01

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

api/src/main/java/org/asynchttpclient/multipart/MultipartBody.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313
package org.asynchttpclient.multipart;
1414

15-
import java.io.ByteArrayInputStream;
1615
import java.io.IOException;
1716
import java.io.RandomAccessFile;
1817
import java.nio.ByteBuffer;
@@ -38,8 +37,8 @@ public class MultipartBody implements RandomAccessBody {
3837
private boolean transfertDone = false;
3938

4039
private int currentPart = 0;
41-
private ByteArrayInputStream currentStream;
42-
private int currentStreamPosition = -1;
40+
private byte[] currentBytes;
41+
private int currentBytesPosition = -1;
4342
private boolean doneWritingParts = false;
4443
private FileLocation fileLocation = FileLocation.NONE;
4544
private FileChannel currentFileChannel;
@@ -112,17 +111,18 @@ public long read(ByteBuffer buffer) throws IOException {
112111
overallLength += writeCurrentFile(buffer);
113112
full = overallLength == maxLength;
114113

115-
} else if (currentStreamPosition > -1) {
116-
overallLength += writeCurrentStream(buffer, maxLength - overallLength);
114+
} else if (currentBytesPosition > -1) {
115+
overallLength += writeCurrentBytes(buffer, maxLength - overallLength);
117116
full = overallLength == maxLength;
118117

119-
if (currentPart == parts.size() && currentStream.available() == 0) {
118+
if (currentPart == parts.size() && currentBytesFullyRead()) {
120119
doneWritingParts = true;
121120
}
122121

123122
} else if (part instanceof StringPart) {
124123
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));
126126
currentPart++;
127127

128128
} else if (part instanceof AbstractFilePart) {
@@ -131,8 +131,8 @@ public long read(ByteBuffer buffer) throws IOException {
131131

132132
switch (fileLocation) {
133133
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));
136136
fileLocation = FileLocation.START;
137137
break;
138138
case START:
@@ -141,30 +141,30 @@ public long read(ByteBuffer buffer) throws IOException {
141141
fileLocation = FileLocation.MIDDLE;
142142
break;
143143
case MIDDLE:
144-
initializeNewCurrentStream(filePart.generateFileEnd());
144+
initializeCurrentBytes(filePart.generateFileEnd());
145145
fileLocation = FileLocation.END;
146146
break;
147147
case END:
148148
currentPart++;
149149
fileLocation = FileLocation.NONE;
150-
if (currentPart == parts.size() && currentStream.available() == 0) {
150+
if (currentPart == parts.size()) {
151151
doneWritingParts = true;
152152
}
153153
}
154154
}
155155
}
156156

157157
if (doneWritingParts) {
158-
if (currentStreamPosition == -1) {
159-
initializeNewCurrentStream(MultipartUtils.getMessageEnd(boundary));
158+
if (currentBytesPosition == -1) {
159+
initializeCurrentBytes(MultipartUtils.getMessageEnd(boundary));
160160
}
161161

162-
if (currentStreamPosition > -1) {
163-
overallLength += writeCurrentStream(buffer, maxLength - overallLength);
162+
if (currentBytesPosition > -1) {
163+
overallLength += writeCurrentBytes(buffer, maxLength - overallLength);
164164

165-
if (currentStream.available() == 0) {
166-
currentStream.close();
167-
currentStreamPosition = -1;
165+
if (currentBytesFullyRead()) {
166+
currentBytes = null;
167+
currentBytesPosition = -1;
168168
transfertDone = true;
169169
}
170170
}
@@ -177,6 +177,10 @@ public long read(ByteBuffer buffer) throws IOException {
177177
}
178178
}
179179

180+
private boolean currentBytesFullyRead() {
181+
return currentBytes == null || currentBytesPosition >= currentBytes.length - 1;
182+
}
183+
180184
private void initializeFileBody(AbstractFilePart part) throws IOException {
181185

182186
if (part instanceof FilePart) {
@@ -185,16 +189,16 @@ private void initializeFileBody(AbstractFilePart part) throws IOException {
185189
currentFileChannel = raf.getChannel();
186190

187191
} else if (part instanceof ByteArrayPart) {
188-
initializeNewCurrentStream(ByteArrayPart.class.cast(part).getBytes());
192+
initializeCurrentBytes(ByteArrayPart.class.cast(part).getBytes());
189193

190194
} else {
191195
throw new IllegalArgumentException("Unknow AbstractFilePart type");
192196
}
193197
}
194198

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;
198202
}
199203

200204
private int writeCurrentFile(ByteBuffer buffer) throws IOException {
@@ -214,23 +218,20 @@ private int writeCurrentFile(ByteBuffer buffer) throws IOException {
214218
return read;
215219
}
216220

217-
private int writeCurrentStream(ByteBuffer buffer, int length) throws IOException {
221+
private int writeCurrentBytes(ByteBuffer buffer, int length) throws IOException {
218222

219-
int available = currentStream.available();
223+
int available = currentBytes.length - currentBytesPosition;
220224

221225
int writeLength = Math.min(available, length);
222226

223227
if (writeLength > 0) {
224-
byte[] bytes = new byte[writeLength];
225-
226-
currentStream.read(bytes);
227-
buffer.put(bytes);
228+
buffer.put(currentBytes, currentBytesPosition, writeLength);
228229

229230
if (available <= length) {
230-
currentStream.close();
231-
currentStreamPosition = -1;
231+
currentBytesPosition = -1;
232+
currentBytes = null;
232233
} else {
233-
currentStreamPosition += writeLength;
234+
currentBytesPosition += writeLength;
234235
}
235236
}
236237

0 commit comments

Comments
 (0)