Advertisement
Guest User

Untitled

a guest
Dec 16th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. package com.murion.engine.network.main.packet;
  2.  
  3. import com.murion.engine.network.utilities.ByteBufUtils;
  4.  
  5. import io.netty.buffer.ByteBuf;
  6.  
  7. /**
  8.  * @author _Jordan <jordan.abraham1997@gmail.com>
  9.  */
  10. public class PacketByteBuf {
  11.  
  12.     /**
  13.      * @author _Jordan <jordan.abraham1997@gmail.com>
  14.      */
  15.     public enum PacketHeader {
  16.         STANDARD, VAR_BYTE, VAR_SHORT;
  17.     }
  18.  
  19.     /**
  20.      * The bit masks.
  21.      */
  22.     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 };
  23.  
  24.     /**
  25.      * Represents the fixed buffer.
  26.      */
  27.     private ByteBuf buffer;
  28.  
  29.     /**
  30.      * The bit position.
  31.      */
  32.     private int bitPosition;
  33.  
  34.     /**
  35.      * Constructs a new {@code PacketByteBuf} object.
  36.      *
  37.      * @param buffer The buffer buffer.
  38.      */
  39.     public PacketByteBuf(ByteBuf buffer) {
  40.         this.buffer = buffer;
  41.     }
  42.  
  43.     public PacketByteBuf writeByte(int value) {
  44.         buffer.writeByte(value);
  45.         return this;
  46.     }
  47.  
  48.     public PacketByteBuf writeInt(int value) {
  49.         buffer.writeInt(value);
  50.         return this;
  51.     }
  52.  
  53.     public PacketByteBuf writeShort(int value) {
  54.         buffer.writeShort(value);
  55.         return this;
  56.     }
  57.  
  58.     public PacketByteBuf writeIntA(int value) {
  59.         buffer.writeByte(value >> 8);
  60.         buffer.writeByte(value);
  61.         buffer.writeByte(value >> 24);
  62.         buffer.writeByte(value >> 16);
  63.         return this;
  64.     }
  65.  
  66.     public PacketByteBuf writeIntB(int value) {
  67.         buffer.writeByte(value >> 16);
  68.         buffer.writeByte(value >> 24);
  69.         buffer.writeByte(value);
  70.         buffer.writeByte(value >> 8);
  71.         return this;
  72.     }
  73.  
  74.     public PacketByteBuf writeShortA(int value) {
  75.         buffer.writeByte(value >> 8);
  76.         buffer.writeByte(value + 128);
  77.         return this;
  78.     }
  79.  
  80.     public PacketByteBuf writeLEShort(int value) {
  81.         buffer.writeByte(value);
  82.         buffer.writeByte(value >> 8);
  83.         return this;
  84.     }
  85.  
  86.     public PacketByteBuf writeLEShortA(int value) {
  87.         buffer.writeByte(value + 128);
  88.         buffer.writeByte(value >> 8);
  89.         return this;
  90.     }
  91.  
  92.     public PacketByteBuf writeByteS(int value) {
  93.         buffer.writeByte(128 - value);
  94.         return this;
  95.     }
  96.  
  97.     public PacketByteBuf writeByteA(int value) {
  98.         buffer.writeByte(value + 128);
  99.         return this;
  100.     }
  101.  
  102.     public PacketByteBuf writeSmart(int value) {
  103.         if (value >= 128) {
  104.             buffer.writeShort(value + 32768);
  105.         } else {
  106.             buffer.writeByte(value);
  107.         }
  108.         return this;
  109.     }
  110.  
  111.     public PacketByteBuf writeBits(int numBits, int value) {
  112.         int bytePos = bitPosition >> 3;
  113.         int bitOffset = 8 - (bitPosition & 7);
  114.         bitPosition += numBits;
  115.         int pos = (bitPosition + 7) / 8;
  116.         buffer.ensureWritable(pos + 1); // pos + 1
  117.         buffer.writerIndex(pos);
  118.         byte b;
  119.         for (; numBits > bitOffset; bitOffset = 8) {
  120.             b = buffer.getByte(bytePos);
  121.             buffer.setByte(bytePos, (byte) (b & ~BIT_MASK_OUT[bitOffset]));
  122.             buffer.setByte(bytePos++, (byte) (b | (value >> (numBits - bitOffset)) & BIT_MASK_OUT[bitOffset]));
  123.             numBits -= bitOffset;
  124.         }
  125.         b = buffer.getByte(bytePos);
  126.         if (numBits == bitOffset) {
  127.             buffer.setByte(bytePos, (byte) (b & ~BIT_MASK_OUT[bitOffset]));
  128.             buffer.setByte(bytePos, (byte) (b | value & BIT_MASK_OUT[bitOffset]));
  129.         } else {
  130.             buffer.setByte(bytePos, (byte) (b & ~(BIT_MASK_OUT[numBits] << (bitOffset - numBits))));
  131.             buffer.setByte(bytePos, (byte) (b | (value & BIT_MASK_OUT[numBits]) << (bitOffset - numBits)));
  132.         }
  133.         return this;
  134.     }
  135.  
  136.     public PacketByteBuf initBitAccess() {
  137.         bitPosition = buffer.writerIndex() * 8;
  138.         return this;
  139.     }
  140.  
  141.     public PacketByteBuf finishBitAccess() {
  142.         buffer.writerIndex((bitPosition + 7) / 8);
  143.         return this;
  144.     }
  145.  
  146.     public PacketByteBuf writeGJString(String string) {
  147.         ByteBufUtils.writeGJString(string, buffer);
  148.         return this;
  149.     }
  150.  
  151.     public PacketByteBuf writeGJString2(String string) {
  152.         ByteBufUtils.writeGJString2(string, buffer);
  153.         return this;
  154.     }
  155.  
  156.     public int readInt() {
  157.         return buffer.readInt();
  158.     }
  159.  
  160.     public int readShort() {
  161.         return buffer.readShort();
  162.     }
  163.  
  164.     public String readRS2String() {
  165.         return ByteBufUtils.readString(buffer);
  166.     }
  167.  
  168.     public ByteBuf getBuffer() {
  169.         return buffer;
  170.     }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement