1818 */
1919package co .elastic .clients .transport .rest5_client .low_level ;
2020
21+ import org .apache .hc .core5 .concurrent .FutureCallback ;
2122import org .apache .hc .core5 .http .ContentTooLongException ;
2223import org .apache .hc .core5 .http .ContentType ;
24+ import org .apache .hc .core5 .http .EntityDetails ;
2325import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
24- import org .apache .hc .core5 .http .nio .entity .AbstractBinAsyncEntityConsumer ;
26+ import org .apache .hc .core5 .http .nio .AsyncEntityConsumer ;
27+ import org .apache .hc .core5 .http .nio .entity .AbstractBinDataConsumer ;
2528import org .apache .hc .core5 .util .ByteArrayBuffer ;
2629
30+ import java .io .IOException ;
2731import java .nio .ByteBuffer ;
2832
2933import static co .elastic .clients .transport .rest5_client .low_level .Constants .DEFAULT_BUFFER_INITIAL_CAPACITY ;
3034
31- class BufferedByteConsumer extends AbstractBinAsyncEntityConsumer <ByteArrayEntity > {
35+ public class BufferedByteConsumer extends AbstractBinDataConsumer implements AsyncEntityConsumer <ByteArrayEntity > {
3236
33- private volatile ByteArrayBuffer buffer ;
3437 private final int limit ;
35- private ContentType contentType ;
38+ private volatile ByteArrayBuffer buffer ;
39+ private volatile FutureCallback <ByteArrayEntity > resultCallback ;
40+ private volatile ContentType contentType ;
41+ private volatile String contentEncoding ;
42+ private volatile ByteArrayEntity result ;
3643
3744 BufferedByteConsumer (int bufferLimit ) {
3845 super ();
@@ -44,8 +51,11 @@ class BufferedByteConsumer extends AbstractBinAsyncEntityConsumer<ByteArrayEntit
4451 }
4552
4653 @ Override
47- protected void streamStart (final ContentType contentType ) {
48- this .contentType = contentType ;
54+ public void streamStart (final EntityDetails entityDetails ,
55+ final FutureCallback <ByteArrayEntity > resultCallback ) {
56+ this .contentType = entityDetails != null ? ContentType .parse (entityDetails .getContentType ()) : null ;
57+ this .contentEncoding = entityDetails != null ? entityDetails .getContentEncoding () : null ;
58+ this .resultCallback = resultCallback ;
4959 }
5060
5161 @ Override
@@ -64,8 +74,25 @@ protected void data(final ByteBuffer src, final boolean endOfStream) throws Cont
6474 }
6575
6676 @ Override
67- protected ByteArrayEntity generateContent () {
68- return new ByteArrayEntity (buffer .toByteArray (), contentType );
77+ protected final void completed () throws IOException {
78+ result = new ByteArrayEntity (buffer .toByteArray (), contentType , contentEncoding );
79+ if (resultCallback != null ) {
80+ resultCallback .completed (result );
81+ }
82+ releaseResources ();
83+ }
84+
85+ @ Override
86+ public ByteArrayEntity getContent () {
87+ return result ;
88+ }
89+
90+ @ Override
91+ public final void failed (final Exception cause ) {
92+ if (resultCallback != null ) {
93+ resultCallback .failed (cause );
94+ }
95+ releaseResources ();
6996 }
7097
7198 @ Override
0 commit comments