19
19
import java .nio .ByteBuffer ;
20
20
import java .util .Queue ;
21
21
import java .util .concurrent .ConcurrentLinkedQueue ;
22
- import java .util .concurrent .atomic .AtomicInteger ;
23
22
24
23
import org .asynchttpclient .request .body .Body ;
25
24
@@ -31,7 +30,6 @@ public final class FeedableBodyGenerator implements BodyGenerator {
31
30
private final static byte [] END_PADDING = "\r \n " .getBytes (US_ASCII );
32
31
private final static byte [] ZERO = "0" .getBytes (US_ASCII );
33
32
private final Queue <BodyPart > queue = new ConcurrentLinkedQueue <>();
34
- private final AtomicInteger queueSize = new AtomicInteger ();
35
33
private FeedListener listener ;
36
34
37
35
@ Override
@@ -41,7 +39,6 @@ public Body createBody() {
41
39
42
40
public void feed (final ByteBuffer buffer , final boolean isLast ) throws IOException {
43
41
queue .offer (new BodyPart (buffer , isLast ));
44
- queueSize .incrementAndGet ();
45
42
if (listener != null ) {
46
43
listener .onContentAdded ();
47
44
}
@@ -55,12 +52,13 @@ public void setListener(FeedListener listener) {
55
52
this .listener = listener ;
56
53
}
57
54
55
+ private static enum PushBodyState {
56
+ ONGOING , CLOSING , FINISHED ;
57
+ }
58
+
58
59
private final class PushBody implements Body {
59
- private final int ONGOING = 0 ;
60
- private final int CLOSING = 1 ;
61
- private final int FINISHED = 2 ;
62
60
63
- private int finishState = 0 ;
61
+ private PushBodyState state = PushBodyState . ONGOING ;
64
62
65
63
@ Override
66
64
public long getContentLength () {
@@ -72,14 +70,14 @@ public long read(final ByteBuffer buffer) throws IOException {
72
70
BodyPart nextPart = queue .peek ();
73
71
if (nextPart == null ) {
74
72
// Nothing in the queue
75
- switch (finishState ) {
73
+ switch (state ) {
76
74
case ONGOING :
77
75
return 0 ;
78
76
case CLOSING :
79
77
buffer .put (ZERO );
80
78
buffer .put (END_PADDING );
81
79
buffer .put (END_PADDING );
82
- finishState = FINISHED ;
80
+ state = PushBodyState . FINISHED ;
83
81
return buffer .position ();
84
82
case FINISHED :
85
83
return -1 ;
@@ -97,7 +95,7 @@ public long read(final ByteBuffer buffer) throws IOException {
97
95
}
98
96
if (!nextPart .buffer .hasRemaining ()) {
99
97
if (nextPart .isLast ) {
100
- finishState = CLOSING ;
98
+ state = PushBodyState . CLOSING ;
101
99
}
102
100
queue .remove ();
103
101
}
0 commit comments