Added ByteBuf read-only invariant

This commit is contained in:
Collin Smith 2020-08-08 14:29:33 -07:00
parent 683d592c55
commit 7006ce9dc0

View File

@ -18,19 +18,20 @@ import com.riiablo.util.DebugUtils;
*/ */
// TODO: improve placeholder documentation // TODO: improve placeholder documentation
public class ByteInput { public class ByteInput {
private static final ByteInput EMPTY_BYTEINPUT = new ByteInput(Unpooled.EMPTY_BUFFER); private static final ByteInput EMPTY_BYTEINPUT = new ByteInput(Unpooled.EMPTY_BUFFER.asReadOnly());
public static ByteInput emptyByteInput() { public static ByteInput emptyByteInput() {
return EMPTY_BYTEINPUT; return EMPTY_BYTEINPUT;
} }
public static ByteInput wrap(byte[] bytes) { public static ByteInput wrap(byte[] bytes) {
return bytes == null ? emptyByteInput() : new ByteInput(Unpooled.wrappedBuffer(bytes)); return bytes == null ? emptyByteInput() : new ByteInput(Unpooled.wrappedBuffer(bytes).asReadOnly());
} }
private final ByteBuf buffer; private final ByteBuf buffer;
private BitInput bitInput; private BitInput bitInput;
ByteInput(ByteBuf buffer) { ByteInput(ByteBuf buffer) {
assert buffer.isReadOnly() : "buffer should be tagged ByteBuf#asReadOnly()";
this.buffer = buffer; this.buffer = buffer;
} }