Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit188f25b

Browse files
author
Stephane Landelle
committed
First step in making parts immutable and thread safe
1 parent3a2a6d6 commit188f25b

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
packagecom.ning.http.client;
1818

1919
publicclassByteArrayPartimplementsPart {
20-
privateStringname;
21-
privateStringfileName;
22-
privatebyte[]data;
23-
privateStringmimeType;
24-
privateStringcharSet;
20+
privatefinalStringname;
21+
privatefinalStringfileName;
22+
privatefinalbyte[]data;
23+
privatefinalStringmimeType;
24+
privatefinalStringcharSet;
2525

2626
publicByteArrayPart(Stringname,StringfileName,byte[]data,StringmimeType,StringcharSet) {
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
publicclassFilePartimplementsPart {
25-
privateStringname;
26-
privateFilefile;
27-
privateStringmimeType;
28-
privateStringcharSet;
25+
privatefinalStringname;
26+
privatefinalFilefile;
27+
privatefinalStringmimeType;
28+
privatefinalStringcharSet;
2929

3030
publicFilePart(Stringname,Filefile,StringmimeType,StringcharSet) {
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
publicStringgetName() {
4241
returnname;
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
privatevoidinitializeFilePart(FilePartfilePart)
252252
throwsIOException {
253253

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

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

263261
privatevoidinitializeStringPart(StringPartcurrentPart)
264262
throwsIOException {
265-
currentPart.setPartBoundary(boundary);
266263

267264
ByteArrayOutputStreamoutputStream =newByteArrayOutputStream();
268265

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

406403
privatelonghandleFileHeaders(WritableByteChanneltarget,FilePartfilePart)throwsIOException {
407-
filePart.setPartBoundary(boundary);
408404

409405
ByteArrayOutputStreamoverhead =generateFileStart(filePart);
410406

@@ -415,9 +411,7 @@ private ByteArrayOutputStream generateFileStart(FilePart filePart)
415411
throwsIOException {
416412
ByteArrayOutputStreamoverhead =newByteArrayOutputStream();
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
privatelonghandleStringPart(WritableByteChanneltarget,StringPartcurrentPart)throwsIOException {
533527

534-
currentPart.setPartBoundary(boundary);
535-
536528
ByteArrayOutputStreamoutputStream =newByteArrayOutputStream();
537529

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

543535
privatelonghandleMultiPart(WritableByteChanneltarget,PartcurrentPart)throwsIOException {
544536

545-
currentPart.setPartBoundary(boundary);
546-
547537
if (currentPart.getClass().equals(StringPart.class)) {
548538
returnhandleStringPart(target, (StringPart)currentPart);
549539
}elseif (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
publicabstractclassPartimplementscom.ning.http.client.Part {
2727

28-
/**
29-
* The boundary
30-
*/
31-
protectedstaticfinalStringBOUNDARY ="----------------314159265358979323846";
32-
33-
/**
34-
* The default boundary to be used if etBoundaryBytes(byte[]) has not been called.
35-
*/
36-
privatestaticfinalbyte[]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
staticfinalbyte[]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-
publicstaticStringgetBoundary() {
125-
returnBOUNDARY;
126-
}
127-
128-
/**
129-
* The ASCII bytes to use as the multipart boundary.
130-
*/
131-
privatebyte[]boundaryBytes;
132-
133108
/**
134109
* Return the name of this part.
135110
*
@@ -165,31 +140,6 @@ public static String getBoundary() {
165140
*/
166141
publicabstractStringgetContentId();
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-
protectedbyte[]getPartBoundary() {
175-
if (boundaryBytes ==null) {
176-
// custom boundary bytes have not been set, use the default.
177-
returnDEFAULT_BOUNDARY_BYTES;
178-
}else {
179-
returnboundaryBytes;
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-
voidsetPartBoundary(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-
protectedvoidsendStart(OutputStreamout)throwsIOException {
160+
protectedvoidsendStart(OutputStreamout,byte[]boundary)throwsIOException {
210161
out.write(EXTRA_BYTES);
211-
out.write(getPartBoundary());
162+
out.write(boundary);
212163
}
213164

214-
privateintstartLength() {
215-
returnEXTRA_BYTES.length +getPartBoundary().length;
165+
privateintstartLength(byte[]boundary) {
166+
returnEXTRA_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-
publicvoidsend(OutputStreamout)throwsIOException {
385-
sendStart(out);
336+
publicvoidsend(OutputStreamout,byte[]boundary)throwsIOException {
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-
publiclonglength() {
353+
publiclonglength(byte[]boundary) {
402354

403355
longlengthOfData =lengthOfData();
404356

405357
if (lengthOfData <0L) {
406358
return -1L;
407359
}else {
408360
returnlengthOfData//
409-
+startLength()//
361+
+startLength(boundary)//
410362
+dispositionHeaderLength()//
411363
+contentTypeHeaderLength()//
412364
+transferEncodingHeaderLength()//
@@ -426,17 +378,6 @@ public String toString() {
426378
returnthis.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-
publicstaticvoidsendParts(OutputStreamout,finalPart[]parts)throwsIOException {
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
thrownewIllegalArgumentException("partBoundary may not be empty");
456397
}
457398
for (Partpart :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
thrownewIllegalArgumentException("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
longtotal =0;
512450
for (Partpart :parts) {
513-
// set the part boundary before we calculate the part's length
514-
part.setPartBoundary(partBoundary);
515-
longl =part.length();
451+
longl =part.length(partBoundary);
516452
if (l <0) {
517453
return -1;
518454
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp