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

ru.tinkoff.kora.http.common.body.StreamingHttpBodyInput Maven / Gradle / Ivy

The newest version!
package ru.tinkoff.kora.http.common.body;

import jakarta.annotation.Nullable;
import ru.tinkoff.kora.common.util.FlowUtils;

import java.nio.ByteBuffer;
import java.util.concurrent.Flow;
import java.util.concurrent.atomic.AtomicBoolean;

public class StreamingHttpBodyInput extends AtomicBoolean implements HttpBodyInput {
    @Nullable
    private final String contentType;
    private final long contentLength;
    private final Flow.Publisher content;

    public StreamingHttpBodyInput(@Nullable String contentType, long contentLength, Flow.Publisher content) {
        this.contentType = contentType;
        this.contentLength = contentLength;
        this.content = content;
    }

    @Override
    public void subscribe(Flow.Subscriber subscriber) {
        if (this.compareAndSet(false, true)) {
            content.subscribe(subscriber);
        } else {
            throw new IllegalStateException("Body was already subscribed");
        }
    }

    @Override
    public void close() {
        if (this.compareAndSet(false, true)) {
            content.subscribe(FlowUtils.drain());
        }
    }

    @Override
    public long contentLength() {
        return this.contentLength;
    }

    @Nullable
    @Override
    public String contentType() {
        return this.contentType;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy