Skip to content

Commit

Permalink
minor clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Aug 13, 2015
1 parent 7345850 commit 1bd2a25
Showing 1 changed file with 8 additions and 10 deletions.
Expand Up @@ -19,7 +19,6 @@
import java.nio.ByteBuffer;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

import org.asynchttpclient.request.body.Body;

Expand All @@ -31,7 +30,6 @@ public final class FeedableBodyGenerator implements BodyGenerator {
private final static byte[] END_PADDING = "\r\n".getBytes(US_ASCII);
private final static byte[] ZERO = "0".getBytes(US_ASCII);
private final Queue<BodyPart> queue = new ConcurrentLinkedQueue<>();
private final AtomicInteger queueSize = new AtomicInteger();
private FeedListener listener;

@Override
Expand All @@ -41,7 +39,6 @@ public Body createBody() {

public void feed(final ByteBuffer buffer, final boolean isLast) throws IOException {
queue.offer(new BodyPart(buffer, isLast));
queueSize.incrementAndGet();
if (listener != null) {
listener.onContentAdded();
}
Expand All @@ -55,12 +52,13 @@ public void setListener(FeedListener listener) {
this.listener = listener;
}

private static enum PushBodyState {
ONGOING, CLOSING, FINISHED;
}

private final class PushBody implements Body {
private final int ONGOING = 0;
private final int CLOSING = 1;
private final int FINISHED = 2;

private int finishState = 0;
private PushBodyState state = PushBodyState.ONGOING;

@Override
public long getContentLength() {
Expand All @@ -72,14 +70,14 @@ public long read(final ByteBuffer buffer) throws IOException {
BodyPart nextPart = queue.peek();
if (nextPart == null) {
// Nothing in the queue
switch (finishState) {
switch (state) {
case ONGOING:
return 0;
case CLOSING:
buffer.put(ZERO);
buffer.put(END_PADDING);
buffer.put(END_PADDING);
finishState = FINISHED;
state = PushBodyState.FINISHED;
return buffer.position();
case FINISHED:
return -1;
Expand All @@ -97,7 +95,7 @@ public long read(final ByteBuffer buffer) throws IOException {
}
if (!nextPart.buffer.hasRemaining()) {
if (nextPart.isLast) {
finishState = CLOSING;
state = PushBodyState.CLOSING;
}
queue.remove();
}
Expand Down

0 comments on commit 1bd2a25

Please sign in to comment.