Skip to content

Commit 80fe8ff

Browse files
committed
Cleanup and removed memory duplication
1 parent cb3dd33 commit 80fe8ff

File tree

1 file changed

+64
-64
lines changed

1 file changed

+64
-64
lines changed

src/com/loopj/android/http/SimpleMultipartEntity.java

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -23,73 +23,53 @@
2323

2424
package com.loopj.android.http;
2525

26-
import java.io.ByteArrayInputStream;
26+
import org.apache.http.Header;
27+
import org.apache.http.HttpEntity;
28+
import org.apache.http.message.BasicHeader;
29+
2730
import java.io.ByteArrayOutputStream;
2831
import java.io.File;
2932
import java.io.FileInputStream;
3033
import java.io.FileNotFoundException;
31-
import java.io.InputStream;
3234
import java.io.IOException;
35+
import java.io.InputStream;
3336
import java.io.OutputStream;
3437
import java.util.Random;
3538

36-
import org.apache.http.Header;
37-
import org.apache.http.HttpEntity;
38-
import org.apache.http.message.BasicHeader;
39-
4039
class SimpleMultipartEntity implements HttpEntity {
40+
41+
private static final byte[] CR_LF = ("\r\n").getBytes();
42+
private static final byte[] TRANSFER_ENCODING_BINARY = "Content-Transfer-Encoding: binary\r\n"
43+
.getBytes();
44+
4145
private final static char[] MULTIPART_CHARS = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
4246

43-
private String boundary = null;
47+
private String boundary;
48+
private byte[] boundaryLine;
49+
private byte[] boundaryEnd;
4450

45-
ByteArrayOutputStream out = new ByteArrayOutputStream();
46-
boolean isSetLast = false;
47-
boolean isSetFirst = false;
51+
// The buffer we use for building the message excluding the last boundary
52+
private ByteArrayOutputStream out = new ByteArrayOutputStream();
4853

4954
public SimpleMultipartEntity() {
5055
final StringBuffer buf = new StringBuffer();
5156
final Random rand = new Random();
5257
for (int i = 0; i < 30; i++) {
5358
buf.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
5459
}
55-
this.boundary = buf.toString();
56-
57-
}
58-
59-
public void writeFirstBoundaryIfNeeds(){
60-
if(!isSetFirst){
61-
try {
62-
out.write(("--" + boundary + "\r\n").getBytes());
63-
} catch (final IOException e) {
64-
e.printStackTrace();
65-
}
66-
}
67-
68-
isSetFirst = true;
69-
}
70-
71-
public void writeLastBoundaryIfNeeds() {
72-
if(isSetLast){
73-
return;
74-
}
75-
76-
try {
77-
out.write(("\r\n--" + boundary + "--\r\n").getBytes());
78-
} catch (final IOException e) {
79-
e.printStackTrace();
80-
}
81-
82-
isSetLast = true;
60+
boundary = buf.toString();
61+
boundaryLine = ("--" + boundary + "\r\n").getBytes();
62+
boundaryEnd = ("\r\n--" + boundary + "--\r\n").getBytes();
8363
}
8464

8565
public void addPart(final String key, final String value) {
86-
writeFirstBoundaryIfNeeds();
8766
try {
88-
out.write(("Content-Disposition: form-data; name=\"" +key+"\"\r\n\r\n").getBytes());
67+
out.write(boundaryLine);
68+
out.write(createContentDisposition(key));
69+
out.write(CR_LF);
8970
out.write(value.getBytes());
90-
out.write(("\r\n--" + boundary + "\r\n").getBytes());
9171
} catch (final IOException e) {
92-
e.printStackTrace();
72+
// Can't happen on ByteArrayOutputStream
9373
}
9474
}
9575

@@ -98,44 +78,63 @@ public void addPart(final String key, final String fileName, final InputStream f
9878
}
9979

10080
public void addPart(final String key, final String fileName, final InputStream fin, String type, final boolean isLast){
101-
writeFirstBoundaryIfNeeds();
10281
try {
103-
type = "Content-Type: "+type+"\r\n";
104-
out.write(("Content-Disposition: form-data; name=\""+ key+"\"; filename=\"" + fileName + "\"\r\n").getBytes());
105-
out.write(type.getBytes());
106-
out.write("Content-Transfer-Encoding: binary\r\n\r\n".getBytes());
82+
out.write(boundaryLine);
83+
84+
// Headers
85+
out.write(createContentDisposition(key, fileName));
86+
out.write(createContentType(type));
87+
out.write(TRANSFER_ENCODING_BINARY);
88+
out.write(CR_LF);
10789

10890
final byte[] tmp = new byte[4096];
10991
int l = 0;
11092
while ((l = fin.read(tmp)) != -1) {
11193
out.write(tmp, 0, l);
11294
}
113-
if(!isLast)
114-
out.write(("\r\n--" + boundary + "\r\n").getBytes());
115-
out.flush();
95+
out.write(CR_LF);
11696
} catch (final IOException e) {
117-
e.printStackTrace();
97+
// Can't happen on ByteArrayOutputStream
11898
} finally {
11999
try {
120100
fin.close();
121101
} catch (final IOException e) {
122-
e.printStackTrace();
102+
// Ignore
123103
}
124104
}
125105
}
126106

127-
public void addPart(final String key, final File value, final boolean isLast) {
128-
try {
129-
addPart(key, value.getName(), new FileInputStream(value), isLast);
130-
} catch (final FileNotFoundException e) {
131-
e.printStackTrace();
132-
}
107+
public void addPart(final String key, final File value, final boolean isLast)
108+
throws FileNotFoundException {
109+
addPart(key, value.getName(), new FileInputStream(value), isLast);
110+
}
111+
112+
private byte[] createContentType(String type) {
113+
String result = "Content-Type: " + type + "\r\n";
114+
return result.getBytes();
115+
}
116+
117+
private byte[] createContentDisposition(final String key) {
118+
StringBuilder builder = new StringBuilder();
119+
builder.append("Content-Disposition: form-data; name=\"");
120+
builder.append(key);
121+
builder.append("\"\r\n");
122+
return builder.toString().getBytes();
123+
}
124+
125+
private byte[] createContentDisposition(final String key, final String fileName) {
126+
StringBuilder builder = new StringBuilder();
127+
builder.append("Content-Disposition: form-data; name=\"");
128+
builder.append(key);
129+
builder.append("\"; filename=\"");
130+
builder.append(fileName);
131+
builder.append("\"\r\n");
132+
return builder.toString().getBytes();
133133
}
134134

135135
@Override
136136
public long getContentLength() {
137-
writeLastBoundaryIfNeeds();
138-
return out.toByteArray().length;
137+
return out.size() + boundaryEnd.length;
139138
}
140139

141140
@Override
@@ -160,7 +159,8 @@ public boolean isStreaming() {
160159

161160
@Override
162161
public void writeTo(final OutputStream outstream) throws IOException {
163-
outstream.write(out.toByteArray());
162+
out.writeTo(outstream);
163+
outstream.write(boundaryEnd);
164164
}
165165

166166
@Override
@@ -178,8 +178,8 @@ public void consumeContent() throws IOException,
178178
}
179179

180180
@Override
181-
public InputStream getContent() throws IOException,
182-
UnsupportedOperationException {
183-
return new ByteArrayInputStream(out.toByteArray());
181+
public InputStream getContent() throws IOException, UnsupportedOperationException {
182+
throw new UnsupportedOperationException(
183+
"getContent() not supported. Use writeTo() instead.");
184184
}
185185
}

0 commit comments

Comments
 (0)