org.zodiac.sdk.nio.channeling.http.DefaultContentLengthStreamResponseHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zodiac-sdk-nio Show documentation
Show all versions of zodiac-sdk-nio Show documentation
Zodiac SDK NIO2(New Non-Blocking IO)
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 -> {
});
}
}