Skip to content

Commit d124b53

Browse files
author
Stephane Landelle
committed
Make explicit that writeToTarget uses byte[]
1 parent 475c599 commit d124b53

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,11 @@ public long transferTo(long position, long count, WritableByteChannel target)
329329

330330
tempPart++;
331331
}
332-
ByteArrayOutputStream endWriter =
333-
new ByteArrayOutputStream();
332+
ByteArrayOutputStream endWriter = new ByteArrayOutputStream();
334333

335334
Part.sendMessageEnd(endWriter, boundary);
336335

337-
overallLength += writeToTarget(target, endWriter);
336+
overallLength += writeToTarget(target, endWriter.toByteArray());
338337

339338
startPart = tempPart;
340339

@@ -391,15 +390,15 @@ private long handleByteArrayPart(WritableByteChannel target,
391390

392391
final ByteArrayOutputStream output = new ByteArrayOutputStream();
393392
Part.sendPart(output, filePart, boundary);
394-
return writeToTarget(target, output);
393+
return writeToTarget(target, output.toByteArray());
395394
}
396395

397396
private long handleFileEnd(WritableByteChannel target, FilePart filePart)
398397
throws IOException {
399398

400399
ByteArrayOutputStream endOverhead = generateFileEnd(filePart);
401400

402-
return this.writeToTarget(target, endOverhead);
401+
return this.writeToTarget(target, endOverhead.toByteArray());
403402
}
404403

405404
private ByteArrayOutputStream generateFileEnd(FilePart filePart)
@@ -415,7 +414,7 @@ private long handleFileHeaders(WritableByteChannel target, FilePart filePart) th
415414

416415
ByteArrayOutputStream overhead = generateFileStart(filePart);
417416

418-
return writeToTarget(target, overhead);
417+
return writeToTarget(target, overhead.toByteArray());
419418
}
420419

421420
private ByteArrayOutputStream generateFileStart(FilePart filePart)
@@ -525,7 +524,7 @@ private long handlePartSource(WritableByteChannel target, FilePart filePart) thr
525524
if (nRead > 0) {
526525
ByteArrayOutputStream bos = new ByteArrayOutputStream(nRead);
527526
bos.write(bytes, 0, nRead);
528-
writeToTarget(target, bos);
527+
writeToTarget(target, bos.toByteArray());
529528
}
530529
}
531530
} finally {
@@ -544,7 +543,7 @@ private long handleStringPart(WritableByteChannel target, StringPart currentPart
544543

545544
Part.sendPart(outputStream, currentPart, boundary);
546545

547-
return writeToTarget(target, outputStream);
546+
return writeToTarget(target, outputStream.toByteArray());
548547
}
549548

550549
private long handleMultiPart(WritableByteChannel target, Part currentPart) throws IOException {
@@ -561,21 +560,21 @@ private long handleMultiPart(WritableByteChannel target, Part currentPart) throw
561560
return 0;
562561
}
563562

564-
private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byteWriter)
563+
private long writeToTarget(WritableByteChannel target, byte[] bytes)
565564
throws IOException {
566565

567566
int written = 0;
568567
int maxSpin = 0;
569-
synchronized (byteWriter) {
570-
ByteBuffer message = ByteBuffer.wrap(byteWriter.toByteArray());
568+
synchronized (bytes) {
569+
ByteBuffer message = ByteBuffer.wrap(bytes);
571570

572571
if (target instanceof SocketChannel) {
573572
final Selector selector = Selector.open();
574573
try {
575574
final SocketChannel channel = (SocketChannel) target;
576575
channel.register(selector, SelectionKey.OP_WRITE);
577576

578-
while (written < byteWriter.size()) {
577+
while (written < bytes.length) {
579578
selector.select(1000);
580579
maxSpin++;
581580
final Set<SelectionKey> selectedKeys = selector.selectedKeys();
@@ -595,13 +594,13 @@ private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byt
595594
selector.close();
596595
}
597596
} else {
598-
while ((target.isOpen()) && (written < byteWriter.size())) {
597+
while ((target.isOpen()) && (written < bytes.length)) {
599598
long nWrite = target.write(message);
600599
written += nWrite;
601600
if (nWrite == 0 && maxSpin++ < 10) {
602601
logger.info("Waiting for writing...");
603602
try {
604-
byteWriter.wait(1000);
603+
bytes.wait(1000);
605604
} catch (InterruptedException e) {
606605
logger.trace(e.getMessage(), e);
607606
}
@@ -616,5 +615,4 @@ private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byt
616615
}
617616
return written;
618617
}
619-
620618
}

0 commit comments

Comments
 (0)