@@ -39,13 +39,11 @@ public class StreamEntityProducer implements AsyncEntityProducer {
39
39
* @param streamSupplier the input stream supplier, should be repeatable
40
40
* @param chunked whether the entity data should be sent in chunks
41
41
*/
42
- public StreamEntityProducer (Supplier <InputStream > streamSupplier , boolean chunked , String contentType ) throws IOException {
42
+ public StreamEntityProducer (Supplier <InputStream > streamSupplier , boolean chunked , String contentType ) {
43
43
this .streamSupplier = streamSupplier ;
44
44
this .chunked = chunked ;
45
45
this .contentType = contentType ;
46
- if (!chunked ) {
47
- content = (streamSupplier .get () instanceof ByteArrayListInputStream ) ? (ByteArrayListInputStream ) streamSupplier .get () : null ;
48
- }
46
+ if (!chunked ) loadContent ();
49
47
}
50
48
51
49
@ Override
@@ -132,7 +130,8 @@ public int available() {
132
130
@ Override
133
131
public void produce (DataStreamChannel channel ) throws IOException {
134
132
// handling of non-chunked request
135
- if (content != null ) {
133
+ if (!chunked ) {
134
+ if (content == null ) loadContent (); // might be necessary if the producer is reused
136
135
ByteBuffer buffer = content .getCurrentBuffer ();
137
136
while (channel .write (buffer ) > 0 ) {
138
137
if (!buffer .hasRemaining ()) {
@@ -148,7 +147,7 @@ public void produce(DataStreamChannel channel) throws IOException {
148
147
}
149
148
150
149
// handling of chunked request
151
- if (chunked && currentStream == null ) {
150
+ if (currentStream == null ) {
152
151
currentStream = streamSupplier .get ();
153
152
}
154
153
@@ -162,4 +161,8 @@ public void produce(DataStreamChannel channel) throws IOException {
162
161
channel .endStream ();
163
162
}
164
163
}
164
+
165
+ private void loadContent () {
166
+ content = (ByteArrayListInputStream ) streamSupplier .get ();
167
+ }
165
168
}
0 commit comments