Skip to content

Commit fb86b39

Browse files
author
Stephane Landelle
committed
initializeBuffer takes byte[], make immutable members final
1 parent d124b53 commit fb86b39

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

src/main/java/com/ning/http/multipart/MultipartBody.java

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,28 @@
3232

3333
public class MultipartBody implements RandomAccessBody {
3434

35-
private byte[] boundary;
36-
private long contentLength;
37-
private List<com.ning.http.client.Part> parts;
38-
private List<RandomAccessFile> files;
39-
private int startPart;
4035
private final static Logger logger = LoggerFactory.getLogger(MultipartBody.class);
41-
ByteArrayInputStream currentStream;
42-
int currentStreamPosition;
43-
boolean endWritten;
44-
boolean doneWritingParts;
45-
FileLocation fileLocation;
46-
FilePart currentFilePart;
47-
FileChannel currentFileChannel;
36+
37+
private final byte[] boundary;
38+
private final long contentLength;
39+
private final List<com.ning.http.client.Part> parts;
40+
private final List<RandomAccessFile> files = new ArrayList<RandomAccessFile>();
41+
42+
private int startPart = 0;
43+
private ByteArrayInputStream currentStream;
44+
private int currentStreamPosition = -1;
45+
private boolean endWritten = false;
46+
private boolean doneWritingParts = false;
47+
private FileLocation fileLocation = FileLocation.NONE;
48+
private FilePart currentFilePart;
49+
private FileChannel currentFileChannel;
4850

4951
enum FileLocation {NONE, START, MIDDLE, END}
5052

5153
public MultipartBody(List<com.ning.http.client.Part> parts, String contentType, long contentLength) {
5254
this.boundary = MultipartEncodingUtil.getAsciiBytes(contentType.substring(contentType.indexOf("boundary=") + "boundary=".length()));
5355
this.parts = parts;
5456
this.contentLength = contentLength;
55-
56-
files = new ArrayList<RandomAccessFile>();
57-
58-
startPart = 0;
59-
currentStreamPosition = -1;
60-
endWritten = false;
61-
doneWritingParts = false;
62-
fileLocation = FileLocation.NONE;
63-
currentFilePart = null;
6457
}
6558

6659
public void close() throws IOException {
@@ -180,7 +173,7 @@ public long read(ByteBuffer buffer) throws IOException {
180173

181174
Part.sendMessageEnd(endWriter, boundary);
182175

183-
initializeBuffer(endWriter);
176+
initializeBuffer(endWriter.toByteArray());
184177
}
185178

186179
if (currentStreamPosition > -1) {
@@ -207,7 +200,7 @@ private void initializeByteArrayBody(FilePart filePart)
207200
ByteArrayOutputStream output = new ByteArrayOutputStream();
208201
filePart.sendData(output);
209202

210-
initializeBuffer(output);
203+
initializeBuffer(output.toByteArray());
211204

212205
fileLocation = FileLocation.MIDDLE;
213206
}
@@ -217,7 +210,7 @@ private void initializeFileEnd(FilePart currentPart)
217210

218211
ByteArrayOutputStream output = generateFileEnd(currentPart);
219212

220-
initializeBuffer(output);
213+
initializeBuffer(output.toByteArray());
221214

222215
fileLocation = FileLocation.END;
223216

@@ -238,6 +231,7 @@ private void initializeFileBody(FilePart currentPart)
238231
currentFileChannel = raf.getChannel();
239232

240233
} else {
234+
// ByteArrayPartSource
241235
PartSource partSource = currentPart.getSource();
242236

243237
InputStream stream = partSource.createInputStream();
@@ -261,7 +255,7 @@ private void initializeFilePart(FilePart filePart)
261255

262256
ByteArrayOutputStream output = generateFileStart(filePart);
263257

264-
initializeBuffer(output);
258+
initializeBuffer(output.toByteArray());
265259

266260
fileLocation = FileLocation.START;
267261
}
@@ -274,7 +268,7 @@ private void initializeStringPart(StringPart currentPart)
274268

275269
Part.sendPart(outputStream, currentPart, boundary);
276270

277-
initializeBuffer(outputStream);
271+
initializeBuffer(outputStream.toByteArray());
278272
}
279273

280274
private int writeToBuffer(ByteBuffer buffer, int length)
@@ -300,10 +294,10 @@ private int writeToBuffer(ByteBuffer buffer, int length)
300294
return writeLength;
301295
}
302296

303-
private void initializeBuffer(ByteArrayOutputStream outputStream)
297+
private void initializeBuffer(byte[] bytes)
304298
throws IOException {
305299

306-
currentStream = new ByteArrayInputStream(outputStream.toByteArray());
300+
currentStream = new ByteArrayInputStream(bytes);
307301

308302
currentStreamPosition = 0;
309303

0 commit comments

Comments
 (0)