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

org.zodiac.sdk.nio.channeling.http.DefaultContentLengthStreamResponseHandler Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.sdk.nio.channeling.http;

import java.nio.ByteBuffer;

import org.zodiac.sdk.nio.channeling.ChannelingSocket;

/**
 Note that the Content-Length is equal to the length of the body after the Content-Encoding.
 This means if you have gzipped your response, then the length calculation happens after compression.
 You will need to be able to load the entire body in memory if you want to calculate the length
 (unless you have that information elsewhere).
 */
public class DefaultContentLengthStreamResponseHandler implements HttpStreamResponseHandler {
    private final  ResponseCallback callback;
    public DefaultContentLengthStreamResponseHandler(ResponseCallback callback) {
        this.callback = callback;
    }

    @Override
    public void accept(byte[] chunked, ChannelingSocket socket) {
        callback.streamWrite(ByteBuffer.wrap(chunked), clientSocket -> {
        });
    }

    @Override
    public void last(byte[] chunked, ChannelingSocket socket) {
        callback.streamWrite(ByteBuffer.wrap(chunked), this::close);
    }

    private void close(ChannelingSocket socket) {
        socket.close(s -> {
        });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy