Skip to content

Commit 8a0a46a

Browse files
committed
Fix ClassCastException introduced by AHC-78. Test still failling, but making progress
1 parent 287a0c2 commit 8a0a46a

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

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

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
package com.ning.http.multipart;
1414

1515
import com.ning.http.client.RandomAccessBody;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
1618

1719
import java.io.ByteArrayOutputStream;
1820
import java.io.File;
19-
import java.io.FileNotFoundException;
2021
import java.io.IOException;
22+
import java.io.InputStream;
2123
import java.io.RandomAccessFile;
2224
import java.nio.ByteBuffer;
2325
import java.nio.channels.FileChannel;
@@ -32,6 +34,7 @@ public class MultipartBody implements RandomAccessBody {
3234
private List<com.ning.http.client.Part> parts;
3335
private List<RandomAccessFile> files;
3436
private int startPart;
37+
private final static Logger logger = LoggerFactory.getLogger(MultipartBody.class);
3538

3639
public MultipartBody(List<com.ning.http.client.Part> parts, String boundary, String contentLength) {
3740
this.boundary = MultipartEncodingUtil.getAsciiBytes(boundary.substring("multipart/form-data; boundary=".length()));
@@ -133,16 +136,14 @@ private long handleByteArrayPart(WritableByteChannel target,
133136
private long handleFileEnd(WritableByteChannel target, FilePart filePart)
134137
throws IOException {
135138

136-
ByteArrayOutputStream endOverhead =
137-
new ByteArrayOutputStream();
139+
ByteArrayOutputStream endOverhead = new ByteArrayOutputStream();
138140

139141
filePart.sendEnd(endOverhead);
140142

141143
return this.writeToTarget(target, endOverhead);
142144
}
143145

144-
private long handleFileHeaders(WritableByteChannel target,
145-
FilePart filePart) throws IOException {
146+
private long handleFileHeaders(WritableByteChannel target, FilePart filePart) throws IOException {
146147
filePart.setPartBoundary(boundary);
147148

148149
ByteArrayOutputStream overhead = new ByteArrayOutputStream();
@@ -158,49 +159,72 @@ private long handleFileHeaders(WritableByteChannel target,
158159
return writeToTarget(target, overhead);
159160
}
160161

161-
private long handleFilePart(WritableByteChannel target, FilePart filePart)
162-
throws IOException, FileNotFoundException {
162+
private long handleFilePart(WritableByteChannel target, FilePart filePart) throws IOException {
163163

164-
int length = 0;
164+
if ( FilePartSource.class.isAssignableFrom( filePart.getSource().getClass())) {
165+
int length = 0;
165166

166-
length += handleFileHeaders(target, filePart);
167+
length += handleFileHeaders(target, filePart);
168+
FilePartSource source = (FilePartSource) filePart.getSource();
167169

168-
FilePartSource source = (FilePartSource) filePart.getSource();
170+
File file = source.getFile();
169171

170-
File file = source.getFile();
172+
RandomAccessFile raf = new RandomAccessFile(file, "r");
173+
files.add(raf);
171174

172-
RandomAccessFile raf = new RandomAccessFile(file, "r");
173-
files.add(raf);
175+
FileChannel fc = raf.getChannel();
174176

175-
FileChannel fc = raf.getChannel();
177+
long fileLength = fc.transferTo(0, file.length(), target);
176178

179+
if (fileLength != file.length()) {
180+
logger.info("Did not complete file.");
181+
}
177182

178-
long fileLength = fc.transferTo(0, file.length(), target);
183+
length += handleFileEnd(target, filePart);
179184

180-
if (fileLength != file.length()) {
181-
System.out.println("Did not complete file.");
185+
return length;
186+
} else {
187+
return handlePartSource(target, filePart);
182188
}
189+
}
190+
191+
private long handlePartSource(WritableByteChannel target, FilePart filePart) throws IOException {
192+
193+
int length = 0;
183194

195+
length += handleFileHeaders(target, filePart);
196+
197+
PartSource partSource = filePart.getSource();
198+
199+
InputStream stream = partSource.createInputStream();
200+
201+
int nRead = 0;
202+
byte[] bytes = new byte[(int)partSource.getLength()];
203+
while (nRead != -1) {
204+
nRead = stream.read(bytes);
205+
if (nRead > 0) {
206+
ByteArrayOutputStream bos = new ByteArrayOutputStream(nRead);
207+
bos.write(bytes, 0, nRead);
208+
writeToTarget(target, bos);
209+
}
210+
}
184211
length += handleFileEnd(target, filePart);
185212

186213
return length;
187214
}
188215

189-
private long handleStringPart(WritableByteChannel target, StringPart currentPart)
190-
throws IOException {
216+
private long handleStringPart(WritableByteChannel target, StringPart currentPart) throws IOException {
191217

192218
currentPart.setPartBoundary(boundary);
193219

194-
ByteArrayOutputStream outputStream =
195-
new ByteArrayOutputStream();
220+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
196221

197222
Part.sendPart(outputStream, currentPart, boundary);
198223

199224
return writeToTarget(target, outputStream);
200225
}
201226

202-
private long handleMultiPart(WritableByteChannel target, Part currentPart)
203-
throws IOException, FileNotFoundException {
227+
private long handleMultiPart(WritableByteChannel target, Part currentPart) throws IOException {
204228

205229
currentPart.setPartBoundary(boundary);
206230

@@ -222,13 +246,13 @@ private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byt
222246
while ((target.isOpen()) && (written < byteWriter.size())) {
223247
ByteBuffer message = ByteBuffer.wrap(byteWriter.toByteArray());
224248
written = target.write(message);
249+
// TODO: This is dangerous to spin
225250
if (written != byteWriter.size()) {
226-
System.out.println("Waiting...");
251+
logger.info("Waiting for writing...");
227252
try {
228253
byteWriter.wait(1000);
229254
} catch (InterruptedException e) {
230-
// TODO Auto-generated catch block
231-
e.printStackTrace();
255+
logger.trace(e.getMessage(), e);
232256
}
233257
}
234258
}

0 commit comments

Comments
 (0)