
io.higgs.http.client.future.Reader Maven / Gradle / Ivy
package io.higgs.http.client.future;
import io.higgs.core.func.Function2;
import io.higgs.http.client.Response;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.Unpooled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Set;
/**
* @author Courtney Robinson
*/
public abstract class Reader {
protected final Logger log = LoggerFactory.getLogger(Reader.class.getName());
protected static final Charset utf8 = Charset.forName("UTF-8");
protected ByteBuf buffer = Unpooled.buffer();
protected ByteBufInputStream data = new ByteBufInputStream(buffer);
protected Set> functions = new HashSet<>();
private boolean completed;
protected Response response;
public Reader() {
}
public Reader(Function2 function) {
if (function == null) {
throw new IllegalArgumentException("Function cannot be null, use another constructor");
}
listen(function);
}
/**
* Invoked each time a block of data is received
*
* @param data the data
*/
public abstract void data(ByteBuf data);
/**
* Called once at the end of a stream when all data is received
*/
public abstract void done();
public void setCompleted(boolean completed) {
this.completed = completed;
if (completed) {
done();
}
}
public boolean isCompleted() {
return completed;
}
/**
* @param function Adds a function to be invoked by this reader
*/
public void listen(Function2 function) {
if (function != null) {
functions.add(function);
}
}
public Response response() {
return response;
}
public void response(Response response) {
this.response = response;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy