All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dbstarll.utils.net.api.ResponseHandlerResponseConsumer Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy