io.github.dbstarll.utils.net.api.ResponseHandlerResponseConsumer Maven / Gradle / Ivy
package io.github.dbstarll.utils.net.api;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicReference;
final class ResponseHandlerResponseConsumer extends AbstractResponseHandlerResponseConsumer {
private final AtomicReference refStringBuilder = new AtomicReference<>();
private ResponseHandlerResponseConsumer(final HttpClientResponseHandler responseHandler, final Charset charset) {
super(responseHandler, charset);
}
@Override
protected void start(final HttpResponse response, final ContentType contentType) {
super.start(response, contentType);
this.refStringBuilder.set(new StringBuilder());
}
@Override
protected T buildResult() throws IOException {
return handleResponse(refStringBuilder.get().toString(), true);
}
@Override
protected void data(final CharBuffer src, final boolean endOfStream) {
refStringBuilder.get().append(src);
}
@Override
public void releaseResources() {
super.releaseResources();
this.refStringBuilder.set(null);
}
static ResponseHandlerResponseConsumer create(final HttpClientResponseHandler responseHandler,
final Charset charset) {
return new ResponseHandlerResponseConsumer<>(responseHandler, charset);
}
}