Skip to content

Commit e6c3761

Browse files
author
Stephane Landelle
committed
Get rid of code duplication for computing length and sending parts
1 parent 757fcb7 commit e6c3761

File tree

10 files changed

+192
-337
lines changed

10 files changed

+192
-337
lines changed

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

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.asynchttpclient.multipart;
1717

1818
import java.io.IOException;
19-
import java.io.OutputStream;
2019

2120
import org.asynchttpclient.util.StandardCharsets;
2221

@@ -42,15 +41,10 @@ public abstract class AbstractFilePart extends PartBase {
4241
*/
4342
public static final String DEFAULT_TRANSFER_ENCODING = "binary";
4443

45-
/**
46-
* Attachment's file name
47-
*/
48-
protected static final String FILE_NAME = "; filename=";
49-
5044
/**
5145
* Attachment's file name as a byte array
5246
*/
53-
private static final byte[] FILE_NAME_BYTES = FILE_NAME.getBytes(StandardCharsets.US_ASCII);
47+
private static final byte[] FILE_NAME_BYTES = "; filename=".getBytes(StandardCharsets.US_ASCII);
5448

5549
private long stalledTime = -1L;
5650

@@ -73,35 +67,15 @@ public AbstractFilePart(String name, String contentType, String charset, String
7367

7468
public abstract String getFileName();
7569

76-
/**
77-
* Write the disposition header to the output stream
78-
*
79-
* @param out
80-
* The output stream
81-
* @throws java.io.IOException
82-
* If an IO problem occurs
83-
*/
84-
protected void sendDispositionHeader(OutputStream out) throws IOException {
85-
super.sendDispositionHeader(out);
70+
protected void visitDispositionHeader(PartVisitor visitor) throws IOException {
71+
super.visitDispositionHeader(visitor);
8672
String filename = getFileName();
8773
if (filename != null) {
88-
out.write(FILE_NAME_BYTES);
89-
out.write(QUOTE_BYTES);
90-
out.write(filename.getBytes(StandardCharsets.US_ASCII));
91-
out.write(QUOTE_BYTES);
92-
}
93-
}
94-
95-
protected long dispositionHeaderLength() {
96-
String filename = this.getFileName();
97-
long length = super.dispositionHeaderLength();
98-
if (filename != null) {
99-
length += FILE_NAME_BYTES.length;
100-
length += QUOTE_BYTES.length;
101-
length += filename.getBytes(StandardCharsets.US_ASCII).length;
102-
length += QUOTE_BYTES.length;
74+
visitor.withBytes(FILE_NAME_BYTES);
75+
visitor.withBytes(QUOTE_BYTES);
76+
visitor.withBytes(filename.getBytes(StandardCharsets.US_ASCII));
77+
visitor.withBytes(QUOTE_BYTES);
10378
}
104-
return length;
10579
}
10680

10781
public void setStalledTime(long ms) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected void sendData(OutputStream out) throws IOException {
5959
}
6060

6161
@Override
62-
protected long lengthOfData() {
62+
protected long getDataLength() {
6363
return bytes.length;
6464
}
6565

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2010 Ning, Inc.
3+
*
4+
* Ning licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package org.asynchttpclient.multipart;
17+
18+
import java.io.IOException;
19+
20+
public class CounterPartVisitor implements PartVisitor {
21+
22+
private long count = 0L;
23+
24+
@Override
25+
public void withBytes(byte[] bytes) throws IOException {
26+
count += bytes.length;
27+
}
28+
29+
public long getCount() {
30+
return count;
31+
}
32+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public String getFileName() {
6464

6565
@Override
6666
protected void sendData(OutputStream out) throws IOException {
67-
if (lengthOfData() == 0) {
67+
if (getDataLength() == 0) {
6868

6969
// this file contains no data, so there is nothing to send.
7070
// we don't want to create a zero length buffer as this will
@@ -86,7 +86,7 @@ protected void sendData(OutputStream out) throws IOException {
8686
}
8787

8888
@Override
89-
protected long lengthOfData() {
89+
protected long getDataLength() {
9090
return file.length();
9191
}
9292

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,10 @@ private long handleFileEnd(WritableByteChannel target, AbstractFilePart filePart
264264
}
265265

266266
private ByteArrayOutputStream generateFileEnd(AbstractFilePart filePart) throws IOException {
267-
ByteArrayOutputStream endOverhead = new ByteArrayOutputStream();
268-
filePart.sendEnd(endOverhead);
269-
return endOverhead;
267+
ByteArrayOutputStream out = new ByteArrayOutputStream();
268+
OutputStreamPartVisitor visitor = new OutputStreamPartVisitor(out);
269+
filePart.visitEnd(visitor);
270+
return out;
270271
}
271272

272273
private long handleFileHeaders(WritableByteChannel target, AbstractFilePart filePart) throws IOException {
@@ -275,15 +276,16 @@ private long handleFileHeaders(WritableByteChannel target, AbstractFilePart file
275276
}
276277

277278
private ByteArrayOutputStream generateFileStart(AbstractFilePart filePart) throws IOException {
278-
ByteArrayOutputStream overhead = new ByteArrayOutputStream();
279-
280-
filePart.sendStart(overhead, boundary);
281-
filePart.sendDispositionHeader(overhead);
282-
filePart.sendContentTypeHeader(overhead);
283-
filePart.sendTransferEncodingHeader(overhead);
284-
filePart.sendContentIdHeader(overhead);
285-
filePart.sendEndOfHeader(overhead);
286-
return overhead;
279+
ByteArrayOutputStream out = new ByteArrayOutputStream();
280+
OutputStreamPartVisitor visitor = new OutputStreamPartVisitor(out);
281+
filePart.visitStart(visitor, boundary);
282+
filePart.visitDispositionHeader(visitor);
283+
filePart.visitContentTypeHeader(visitor);
284+
filePart.visitTransferEncodingHeader(visitor);
285+
filePart.visitContentIdHeader(visitor);
286+
filePart.visitEndOfHeader(visitor);
287+
288+
return out;
287289
}
288290

289291
private long handleFilePart(WritableByteChannel target, FilePart filePart) throws IOException {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2010 Ning, Inc.
3+
*
4+
* Ning licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package org.asynchttpclient.multipart;
17+
18+
import java.io.IOException;
19+
import java.io.OutputStream;
20+
21+
public class OutputStreamPartVisitor implements PartVisitor {
22+
23+
private final OutputStream out;
24+
25+
public OutputStreamPartVisitor(OutputStream out) {
26+
this.out = out;
27+
}
28+
29+
@Override
30+
public void withBytes(byte[] bytes) throws IOException{
31+
out.write(bytes);
32+
}
33+
34+
public OutputStream getOutputStream() {
35+
return out;
36+
}
37+
}

0 commit comments

Comments
 (0)