Skip to content

Commit 050e534

Browse files
author
Stephane Landelle
committed
Minor clean up, turn Part into an interface
1 parent 3dcb179 commit 050e534

File tree

8 files changed

+204
-175
lines changed

8 files changed

+204
-175
lines changed

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

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

15-
import org.asynchttpclient.util.StandardCharsets;
15+
import static org.asynchttpclient.util.StandardCharsets.US_ASCII;
16+
import static org.asynchttpclient.util.StandardCharsets.ISO_8859_1;
1617

1718
import java.io.ByteArrayOutputStream;
1819
import java.io.IOException;
@@ -32,7 +33,7 @@ public abstract class AbstractFilePart extends PartBase {
3233
/**
3334
* Default charset of file attachments.
3435
*/
35-
public static final String DEFAULT_CHARSET = StandardCharsets.ISO_8859_1.name();
36+
public static final String DEFAULT_CHARSET = ISO_8859_1.name();
3637

3738
/**
3839
* Default transfer encoding of file attachments.
@@ -42,10 +43,12 @@ public abstract class AbstractFilePart extends PartBase {
4243
/**
4344
* Attachment's file name as a byte array
4445
*/
45-
private static final byte[] FILE_NAME_BYTES = "; filename=".getBytes(StandardCharsets.US_ASCII);
46+
private static final byte[] FILE_NAME_BYTES = "; filename=".getBytes(US_ASCII);
4647

4748
private long stalledTime = -1L;
4849

50+
private String fileName;
51+
4952
/**
5053
* FilePart Constructor.
5154
*
@@ -64,27 +67,16 @@ public AbstractFilePart(String name, String contentType, String charset, String
6467
DEFAULT_TRANSFER_ENCODING, contentId);
6568
}
6669

67-
public abstract String getFileName();
68-
6970
protected void visitDispositionHeader(PartVisitor visitor) throws IOException {
7071
super.visitDispositionHeader(visitor);
71-
String filename = getFileName();
72-
if (filename != null) {
72+
if (fileName != null) {
7373
visitor.withBytes(FILE_NAME_BYTES);
74-
visitor.withBytes(QUOTE_BYTES);
75-
visitor.withBytes(filename.getBytes(StandardCharsets.US_ASCII));
76-
visitor.withBytes(QUOTE_BYTES);
74+
visitor.withByte(QUOTE_BYTE);
75+
visitor.withBytes(fileName.getBytes(US_ASCII));
76+
visitor.withByte(QUOTE_BYTE);
7777
}
7878
}
7979

80-
public void setStalledTime(long ms) {
81-
stalledTime = ms;
82-
}
83-
84-
public long getStalledTime() {
85-
return stalledTime;
86-
}
87-
8880
protected byte[] generateFileStart(byte[] boundary) throws IOException {
8981
ByteArrayOutputStream out = new ByteArrayOutputStream();
9082
OutputStreamPartVisitor visitor = new OutputStreamPartVisitor(out);
@@ -104,4 +96,28 @@ protected byte[] generateFileEnd() throws IOException {
10496
visitEnd(visitor);
10597
return out.toByteArray();
10698
}
99+
100+
public void setStalledTime(long ms) {
101+
stalledTime = ms;
102+
}
103+
104+
public long getStalledTime() {
105+
return stalledTime;
106+
}
107+
108+
public void setFileName(String fileName) {
109+
this.fileName = fileName;
110+
}
111+
112+
public String getFileName() {
113+
return fileName;
114+
}
115+
116+
@Override
117+
public String toString() {
118+
return new StringBuilder()//
119+
.append(super.toString())//
120+
.append(" filename=").append(fileName)//
121+
.toString();
122+
}
107123
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
public class ByteArrayPart extends AbstractFilePart {
2020

2121
private final byte[] bytes;
22-
private final String fileName;
2322

2423
public ByteArrayPart(String name, byte[] bytes) {
2524
this(name, bytes, null);
@@ -43,12 +42,7 @@ public ByteArrayPart(String name, byte[] bytes, String contentType, String chars
4342
throw new NullPointerException("bytes");
4443
}
4544
this.bytes = bytes;
46-
this.fileName = fileName;
47-
}
48-
49-
@Override
50-
public String getFileName() {
51-
return fileName;
45+
setFileName(fileName);
5246
}
5347

5448
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public void withBytes(byte[] bytes) throws IOException {
2323
count += bytes.length;
2424
}
2525

26+
@Override
27+
public void withByte(byte b) throws IOException {
28+
count++;
29+
}
30+
2631
public long getCount() {
2732
return count;
2833
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class FilePart extends AbstractFilePart {
2929
private static final Logger LOGGER = LoggerFactory.getLogger(FilePart.class);
3030

3131
private final File file;
32-
private final String fileName;
3332

3433
public FilePart(String name, File file) {
3534
this(name, file, null, null);
@@ -59,12 +58,7 @@ public FilePart(String name, File file, String contentType, String charset, Stri
5958
if (!file.canRead()) {
6059
throw new IllegalArgumentException("File is not readable " + file.getAbsolutePath());
6160
}
62-
this.fileName = fileName != null ? fileName : file.getName();
63-
}
64-
65-
@Override
66-
public String getFileName() {
67-
return fileName;
61+
setFileName(fileName != null ? fileName : file.getName());
6862
}
6963

7064
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public void withBytes(byte[] bytes) throws IOException {
3131
out.write(bytes);
3232
}
3333

34+
@Override
35+
public void withByte(byte b) throws IOException {
36+
out.write(b);
37+
}
38+
3439
public OutputStream getOutputStream() {
3540
return out;
3641
}

0 commit comments

Comments
 (0)