com.github.fmjsjx.libnetty.resp.DefaultRespMessageDecoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of libnetty-resp Show documentation
Show all versions of libnetty-resp Show documentation
A set of some useful libraries based on netty4.1.x.
package com.github.fmjsjx.libnetty.resp;
import static com.github.fmjsjx.libnetty.resp.RespConstants.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiConsumer;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
/**
* The default implementation of {@link RespMessageDecoder}.
*
* @since 1.0
*
* @author MJ Fang
*/
public class DefaultRespMessageDecoder extends RespMessageDecoder {
static final class ArrayBuilder {
private final int length;
private final ArrayList values;
ArrayBuilder(int length) {
this.length = length;
this.values = new ArrayList<>(length);
}
boolean isReachedEnd() {
return values.size() == length;
}
boolean add(RespMessage msg) {
values.add(msg);
return isReachedEnd();
}
RespArrayMessage build() {
return wrappedArray(values);
}
}
private LinkedList nests;
private int currentBulkStringLength;
private final BiConsumer> arrayHeaderDecoder = this::decodeArrayHeader;
private final BiConsumer> bulkStringLengthDecoder = this::decodeBulkStringLength;
private final BiConsumer> simpleStringDecoder = this::decodeSimpleString;
private final BiConsumer> errorDecoder = this::decodeError;
private final BiConsumer> integerDecoder = this::decodeInteger;
/**
* Constructs a new {@link DefaultRespMessageDecoder} using default
* {@code maxInlineMessageLength} ({@code 65536}).
*/
public DefaultRespMessageDecoder() {
this(RespConstants.RESP_INLINE_MESSAGE_MAX_LENGTH);
}
/**
* Constructs a new {@link DefaultRespMessageDecoder} using specified
* {@code maxInlineMessageLength}.
*
* @param maxInlineMessageLength maxInlineMessageLength the maximum length of
* in-line messages
*/
public DefaultRespMessageDecoder(int maxInlineMessageLength) {
super(maxInlineMessageLength);
}
@Override
protected void resetDecoder() {
super.resetDecoder();
nests = null;
}
@Override
protected boolean decodeInline(ByteBuf in, List