Skip to content

Commit 188f25b

Browse files
author
Stephane Landelle
committed
First step in making parts immutable and thread safe
1 parent 3a2a6d6 commit 188f25b

File tree

4 files changed

+23
-98
lines changed

4 files changed

+23
-98
lines changed

src/main/java/com/ning/http/client/ByteArrayPart.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
package com.ning.http.client;
1818

1919
public class ByteArrayPart implements Part {
20-
private String name;
21-
private String fileName;
22-
private byte[] data;
23-
private String mimeType;
24-
private String charSet;
20+
private final String name;
21+
private final String fileName;
22+
private final byte[] data;
23+
private final String mimeType;
24+
private final String charSet;
2525

2626
public ByteArrayPart(String name, String fileName, byte[] data, String mimeType, String charSet) {
2727
this.name = name;

src/main/java/com/ning/http/client/FilePart.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
* A file multipart part.
2323
*/
2424
public class FilePart implements Part {
25-
private String name;
26-
private File file;
27-
private String mimeType;
28-
private String charSet;
25+
private final String name;
26+
private final File file;
27+
private final String mimeType;
28+
private final String charSet;
2929

3030
public FilePart(String name, File file, String mimeType, String charSet) {
3131
this.name = name;
@@ -37,7 +37,6 @@ public FilePart(String name, File file, String mimeType, String charSet) {
3737
/**
3838
* {@inheritDoc}
3939
*/
40-
/* @Override */
4140
public String getName() {
4241
return name;
4342
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ private void initializeFileBody(FilePart currentPart)
251251
private void initializeFilePart(FilePart filePart)
252252
throws IOException {
253253

254-
filePart.setPartBoundary(boundary);
255-
256254
ByteArrayOutputStream output = generateFileStart(filePart);
257255

258256
initializeBuffer(output.toByteArray());
@@ -262,7 +260,6 @@ private void initializeFilePart(FilePart filePart)
262260

263261
private void initializeStringPart(StringPart currentPart)
264262
throws IOException {
265-
currentPart.setPartBoundary(boundary);
266263

267264
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
268265

@@ -404,7 +401,6 @@ private ByteArrayOutputStream generateFileEnd(FilePart filePart)
404401
}
405402

406403
private long handleFileHeaders(WritableByteChannel target, FilePart filePart) throws IOException {
407-
filePart.setPartBoundary(boundary);
408404

409405
ByteArrayOutputStream overhead = generateFileStart(filePart);
410406

@@ -415,9 +411,7 @@ private ByteArrayOutputStream generateFileStart(FilePart filePart)
415411
throws IOException {
416412
ByteArrayOutputStream overhead = new ByteArrayOutputStream();
417413

418-
filePart.setPartBoundary(boundary);
419-
420-
filePart.sendStart(overhead);
414+
filePart.sendStart(overhead, boundary);
421415
filePart.sendDispositionHeader(overhead);
422416
filePart.sendContentTypeHeader(overhead);
423417
filePart.sendTransferEncodingHeader(overhead);
@@ -531,8 +525,6 @@ private long handlePartSource(WritableByteChannel target, FilePart filePart) thr
531525

532526
private long handleStringPart(WritableByteChannel target, StringPart currentPart) throws IOException {
533527

534-
currentPart.setPartBoundary(boundary);
535-
536528
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
537529

538530
Part.sendPart(outputStream, currentPart, boundary);
@@ -542,8 +534,6 @@ private long handleStringPart(WritableByteChannel target, StringPart currentPart
542534

543535
private long handleMultiPart(WritableByteChannel target, Part currentPart) throws IOException {
544536

545-
currentPart.setPartBoundary(boundary);
546-
547537
if (currentPart.getClass().equals(StringPart.class)) {
548538
return handleStringPart(target, (StringPart) currentPart);
549539
} else if (currentPart.getClass().equals(FilePart.class)) {

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

Lines changed: 13 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@
2525
*/
2626
public abstract class Part implements com.ning.http.client.Part {
2727

28-
/**
29-
* The boundary
30-
*/
31-
protected static final String BOUNDARY = "----------------314159265358979323846";
32-
33-
/**
34-
* The default boundary to be used if etBoundaryBytes(byte[]) has not been called.
35-
*/
36-
private static final byte[] DEFAULT_BOUNDARY_BYTES = MultipartEncodingUtil.getAsciiBytes(BOUNDARY);
37-
3828
/**
3929
* Carriage return/linefeed
4030
*/
@@ -115,21 +105,6 @@ public abstract class Part implements com.ning.http.client.Part {
115105
*/
116106
static final byte[] CONTENT_ID_BYTES = MultipartEncodingUtil.getAsciiBytes(CONTENT_ID);
117107

118-
/**
119-
* Return the boundary string.
120-
*
121-
* @return the boundary string
122-
* @deprecated uses a constant string. Rather use {@link #getPartBoundary}
123-
*/
124-
public static String getBoundary() {
125-
return BOUNDARY;
126-
}
127-
128-
/**
129-
* The ASCII bytes to use as the multipart boundary.
130-
*/
131-
private byte[] boundaryBytes;
132-
133108
/**
134109
* Return the name of this part.
135110
*
@@ -165,31 +140,6 @@ public static String getBoundary() {
165140
*/
166141
public abstract String getContentId();
167142

168-
/**
169-
* Gets the part boundary to be used.
170-
*
171-
* @return the part boundary as an array of bytes.
172-
* @since 3.0
173-
*/
174-
protected byte[] getPartBoundary() {
175-
if (boundaryBytes == null) {
176-
// custom boundary bytes have not been set, use the default.
177-
return DEFAULT_BOUNDARY_BYTES;
178-
} else {
179-
return boundaryBytes;
180-
}
181-
}
182-
183-
/**
184-
* Sets the part boundary. Only meant to be used by {@link Part#sendParts(java.io.OutputStream, Part[], byte[])} and {@link Part#getLengthOfParts(Part[], byte[])}
185-
*
186-
* @param boundaryBytes An array of ASCII bytes.
187-
* @since 3.0
188-
*/
189-
void setPartBoundary(byte[] boundaryBytes) {
190-
this.boundaryBytes = boundaryBytes;
191-
}
192-
193143
/**
194144
* Tests if this part can be sent more than once.
195145
*
@@ -204,15 +154,16 @@ public boolean isRepeatable() {
204154
* Write the start to the specified output stream
205155
*
206156
* @param out The output stream
157+
* @param boundary the boundary
207158
* @throws java.io.IOException If an IO problem occurs.
208159
*/
209-
protected void sendStart(OutputStream out) throws IOException {
160+
protected void sendStart(OutputStream out, byte[] boundary) throws IOException {
210161
out.write(EXTRA_BYTES);
211-
out.write(getPartBoundary());
162+
out.write(boundary);
212163
}
213164

214-
private int startLength() {
215-
return EXTRA_BYTES.length + getPartBoundary().length;
165+
private int startLength(byte[] boundary) {
166+
return EXTRA_BYTES.length + boundary.length;
216167
}
217168

218169
/**
@@ -379,10 +330,11 @@ protected long endLength() {
379330
* Write all the data to the output stream. If you override this method make sure to override #length() as well
380331
*
381332
* @param out The output stream
333+
* @param boundary the boundary
382334
* @throws IOException If an IO problem occurs.
383335
*/
384-
public void send(OutputStream out) throws IOException {
385-
sendStart(out);
336+
public void send(OutputStream out, byte[] boundary) throws IOException {
337+
sendStart(out, boundary);
386338
sendDispositionHeader(out);
387339
sendContentTypeHeader(out);
388340
sendTransferEncodingHeader(out);
@@ -398,15 +350,15 @@ public void send(OutputStream out) throws IOException {
398350
* @return long The length.
399351
* @throws IOException If an IO problem occurs
400352
*/
401-
public long length() {
353+
public long length(byte[] boundary) {
402354

403355
long lengthOfData = lengthOfData();
404356

405357
if (lengthOfData < 0L) {
406358
return -1L;
407359
} else {
408360
return lengthOfData//
409-
+ startLength()//
361+
+ startLength(boundary)//
410362
+ dispositionHeaderLength()//
411363
+ contentTypeHeaderLength()//
412364
+ transferEncodingHeaderLength()//
@@ -426,17 +378,6 @@ public String toString() {
426378
return this.getName();
427379
}
428380

429-
/**
430-
* Write all parts and the last boundary to the specified output stream.
431-
*
432-
* @param out The stream to write to.
433-
* @param parts The parts to write.
434-
* @throws IOException If an I/O error occurs while writing the parts.
435-
*/
436-
public static void sendParts(OutputStream out, final Part[] parts) throws IOException {
437-
sendParts(out, parts, DEFAULT_BOUNDARY_BYTES);
438-
}
439-
440381
/**
441382
* Write all parts and the last boundary to the specified output stream.
442383
*
@@ -455,9 +396,7 @@ public static void sendParts(OutputStream out, Part[] parts, byte[] partBoundary
455396
throw new IllegalArgumentException("partBoundary may not be empty");
456397
}
457398
for (Part part : parts) {
458-
// set the part boundary before the part is sent
459-
part.setPartBoundary(partBoundary);
460-
part.send(out);
399+
part.send(out, partBoundary);
461400
}
462401
out.write(EXTRA_BYTES);
463402
out.write(partBoundary);
@@ -491,8 +430,7 @@ public static void sendPart(OutputStream out, Part part, byte[] partBoundary) th
491430
throw new IllegalArgumentException("Parts may not be null");
492431
}
493432

494-
part.setPartBoundary(partBoundary);
495-
part.send(out);
433+
part.send(out, partBoundary);
496434
}
497435

498436
/**
@@ -510,9 +448,7 @@ public static long getLengthOfParts(Part[] parts, byte[] partBoundary) {
510448
}
511449
long total = 0;
512450
for (Part part : parts) {
513-
// set the part boundary before we calculate the part's length
514-
part.setPartBoundary(partBoundary);
515-
long l = part.length();
451+
long l = part.length(partBoundary);
516452
if (l < 0) {
517453
return -1;
518454
}

0 commit comments

Comments
 (0)