@@ -28,12 +28,13 @@ public abstract class QueueBasedFeedableBodyGenerator<T extends Queue<BodyChunk>
28
28
29
29
@ Override
30
30
public Body createBody () {
31
- return new PushBody ();
31
+ return new PushBody (queue () );
32
32
}
33
33
34
34
protected abstract boolean offer (BodyChunk chunk ) throws Exception ;
35
+
35
36
protected abstract Queue <BodyChunk > queue ();
36
-
37
+
37
38
@ Override
38
39
public boolean feed (final ByteBuffer buffer , final boolean isLast ) throws Exception {
39
40
boolean offered = offer (new BodyChunk (buffer , isLast ));
@@ -48,10 +49,15 @@ public void setListener(FeedListener listener) {
48
49
this .listener = listener ;
49
50
}
50
51
51
- public final class PushBody implements Body {
52
+ public static final class PushBody implements Body {
52
53
54
+ private final Queue <BodyChunk > queue ;
53
55
private BodyState state = BodyState .CONTINUE ;
54
56
57
+ public PushBody (Queue <BodyChunk > queue ) {
58
+ this .queue = queue ;
59
+ }
60
+
55
61
@ Override
56
62
public long getContentLength () {
57
63
return -1 ;
@@ -72,13 +78,13 @@ public BodyState transferTo(final ByteBuf target) throws IOException {
72
78
private BodyState readNextChunk (ByteBuf target ) throws IOException {
73
79
BodyState res = BodyState .SUSPEND ;
74
80
while (target .isWritable () && state != BodyState .STOP ) {
75
- BodyChunk nextChunk = queue () .peek ();
81
+ BodyChunk nextChunk = queue .peek ();
76
82
if (nextChunk == null ) {
77
83
// Nothing in the queue. suspend stream if nothing was read. (reads == 0)
78
84
return res ;
79
85
} else if (!nextChunk .buffer .hasRemaining () && !nextChunk .isLast ) {
80
86
// skip empty buffers
81
- queue () .remove ();
87
+ queue .remove ();
82
88
} else {
83
89
res = BodyState .CONTINUE ;
84
90
readChunk (target , nextChunk );
@@ -94,22 +100,22 @@ private void readChunk(ByteBuf target, BodyChunk part) {
94
100
if (part .isLast ) {
95
101
state = BodyState .STOP ;
96
102
}
97
- queue () .remove ();
103
+ queue .remove ();
98
104
}
99
105
}
100
106
101
- @ Override
102
- public void close () {
107
+ private void move (ByteBuf target , ByteBuffer source ) {
108
+ int size = Math .min (target .writableBytes (), source .remaining ());
109
+ if (size > 0 ) {
110
+ ByteBuffer slice = source .slice ();
111
+ slice .limit (size );
112
+ target .writeBytes (slice );
113
+ source .position (source .position () + size );
114
+ }
103
115
}
104
- }
105
116
106
- private void move (ByteBuf target , ByteBuffer source ) {
107
- int size = Math .min (target .writableBytes (), source .remaining ());
108
- if (size > 0 ) {
109
- ByteBuffer slice = source .slice ();
110
- slice .limit (size );
111
- target .writeBytes (slice );
112
- source .position (source .position () + size );
117
+ @ Override
118
+ public void close () {
113
119
}
114
120
}
115
121
0 commit comments