io.muserver.ChunkedHttpOutputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mu-server Show documentation
Show all versions of mu-server Show documentation
A simple but powerful web server framework
package io.muserver;
import io.netty.buffer.Unpooled;
import java.io.IOException;
import java.io.OutputStream;
class ChunkedHttpOutputStream extends OutputStream {
private final NettyResponseAdaptor response;
private boolean isClosed = false;
ChunkedHttpOutputStream(NettyResponseAdaptor response) {
this.response = response;
}
@Override
public void write(int b) throws IOException {
write(new byte[]{(byte) b}, 0, 1);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (isClosed) {
throw new IOException("Cannot write to closed output stream");
}
response.write(Unpooled.copiedBuffer(b, off, len), true);
}
public void close() {
isClosed = true;
}
}