Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.murion.engine.network.main.packet;
- import com.murion.engine.network.utilities.ByteBufUtils;
- import io.netty.buffer.ByteBuf;
- /**
- * @author _Jordan <jordan.abraham1997@gmail.com>
- */
- public class PacketByteBuf {
- /**
- * @author _Jordan <jordan.abraham1997@gmail.com>
- */
- public enum PacketHeader {
- STANDARD, VAR_BYTE, VAR_SHORT;
- }
- /**
- * The bit masks.
- */
- private static final int[] BIT_MASK_OUT = new int[] { 0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff, 0x7ff, 0xfff, 0x1fff, 0x3fff, 0x7fff, 0xffff, 0x1ffff, 0x3ffff, 0x7ffff, 0xfffff, 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff, 0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, -1 };
- /**
- * Represents the fixed buffer.
- */
- private ByteBuf buffer;
- /**
- * The bit position.
- */
- private int bitPosition;
- /**
- * Constructs a new {@code PacketByteBuf} object.
- *
- * @param buffer The buffer buffer.
- */
- public PacketByteBuf(ByteBuf buffer) {
- this.buffer = buffer;
- }
- public PacketByteBuf writeByte(int value) {
- buffer.writeByte(value);
- return this;
- }
- public PacketByteBuf writeInt(int value) {
- buffer.writeInt(value);
- return this;
- }
- public PacketByteBuf writeShort(int value) {
- buffer.writeShort(value);
- return this;
- }
- public PacketByteBuf writeIntA(int value) {
- buffer.writeByte(value >> 8);
- buffer.writeByte(value);
- buffer.writeByte(value >> 24);
- buffer.writeByte(value >> 16);
- return this;
- }
- public PacketByteBuf writeIntB(int value) {
- buffer.writeByte(value >> 16);
- buffer.writeByte(value >> 24);
- buffer.writeByte(value);
- buffer.writeByte(value >> 8);
- return this;
- }
- public PacketByteBuf writeShortA(int value) {
- buffer.writeByte(value >> 8);
- buffer.writeByte(value + 128);
- return this;
- }
- public PacketByteBuf writeLEShort(int value) {
- buffer.writeByte(value);
- buffer.writeByte(value >> 8);
- return this;
- }
- public PacketByteBuf writeLEShortA(int value) {
- buffer.writeByte(value + 128);
- buffer.writeByte(value >> 8);
- return this;
- }
- public PacketByteBuf writeByteS(int value) {
- buffer.writeByte(128 - value);
- return this;
- }
- public PacketByteBuf writeByteA(int value) {
- buffer.writeByte(value + 128);
- return this;
- }
- public PacketByteBuf writeSmart(int value) {
- if (value >= 128) {
- buffer.writeShort(value + 32768);
- } else {
- buffer.writeByte(value);
- }
- return this;
- }
- public PacketByteBuf writeBits(int numBits, int value) {
- int bytePos = bitPosition >> 3;
- int bitOffset = 8 - (bitPosition & 7);
- bitPosition += numBits;
- int pos = (bitPosition + 7) / 8;
- buffer.ensureWritable(pos + 1); // pos + 1
- buffer.writerIndex(pos);
- byte b;
- for (; numBits > bitOffset; bitOffset = 8) {
- b = buffer.getByte(bytePos);
- buffer.setByte(bytePos, (byte) (b & ~BIT_MASK_OUT[bitOffset]));
- buffer.setByte(bytePos++, (byte) (b | (value >> (numBits - bitOffset)) & BIT_MASK_OUT[bitOffset]));
- numBits -= bitOffset;
- }
- b = buffer.getByte(bytePos);
- if (numBits == bitOffset) {
- buffer.setByte(bytePos, (byte) (b & ~BIT_MASK_OUT[bitOffset]));
- buffer.setByte(bytePos, (byte) (b | value & BIT_MASK_OUT[bitOffset]));
- } else {
- buffer.setByte(bytePos, (byte) (b & ~(BIT_MASK_OUT[numBits] << (bitOffset - numBits))));
- buffer.setByte(bytePos, (byte) (b | (value & BIT_MASK_OUT[numBits]) << (bitOffset - numBits)));
- }
- return this;
- }
- public PacketByteBuf initBitAccess() {
- bitPosition = buffer.writerIndex() * 8;
- return this;
- }
- public PacketByteBuf finishBitAccess() {
- buffer.writerIndex((bitPosition + 7) / 8);
- return this;
- }
- public PacketByteBuf writeGJString(String string) {
- ByteBufUtils.writeGJString(string, buffer);
- return this;
- }
- public PacketByteBuf writeGJString2(String string) {
- ByteBufUtils.writeGJString2(string, buffer);
- return this;
- }
- public int readInt() {
- return buffer.readInt();
- }
- public int readShort() {
- return buffer.readShort();
- }
- public String readRS2String() {
- return ByteBufUtils.readString(buffer);
- }
- public ByteBuf getBuffer() {
- return buffer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement