Guest User

Untitled

a guest
Nov 16th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package org.rs2.janus.net.packet;
  5.  
  6. import org.jboss.netty.buffer.ChannelBuffer;
  7.  
  8. /**
  9.  * @author Michael Schmidt <H3llKing> <msrsps@hotmail.com>
  10.  *
  11.  */
  12. public class PacketReader {
  13.  
  14.     /**
  15.      * The buffer the is wrapped by this reader.
  16.      */
  17.     private final ChannelBuffer internalBuffer;
  18.  
  19.     /**
  20.      * If the reader is in bit access.
  21.      */
  22.     private boolean bitAccess = false;
  23.  
  24.     /**
  25.      * The bit position of the reader.
  26.      */
  27.     private int bitPosition;
  28.  
  29.     /**
  30.      * New instance
  31.      *
  32.      * @param packet
  33.      *            The packet who's payload will be read.
  34.      */
  35.     public PacketReader(Packet packet) {
  36.         this.internalBuffer = packet.getPayload();
  37.     }
  38.  
  39.     /**
  40.      *
  41.      */
  42.     public int readBit() {
  43.         int bytePos = bitPosition >> 3;
  44.         int bitOffset = 8 - (bitPosition & 7);
  45.  
  46.         return (internalBuffer.getByte(bytePos) & PacketConstants.BIT_MASK[bitOffset]) << bitOffset;
  47.     }
  48.  
  49.     /**
  50.      * Puts the reader into bit access and sets the bit fields.
  51.      */
  52.     public void setBitAccess() {
  53.         if (bitAccess == true)
  54.             throw new IllegalStateException("Already in bit access.");
  55.  
  56.         bitAccess = true;
  57.         bitPosition = internalBuffer.readerIndex() << 3;
  58.     }
  59.  
  60.     /**
  61.      * Puts the reader into byte access and resets the bit fields.
  62.      */
  63.     public void setByteAccess() {
  64.         if (bitAccess == false)
  65.             throw new IllegalStateException("Already in byte access.");
  66.  
  67.         int bytePos = bitPosition >> 3;
  68.  
  69.         if (bitPosition % 8 != 0) // Round as needed.
  70.             ++bytePos;
  71.  
  72.         internalBuffer.readerIndex(bytePos);
  73.         bitPosition = 0;
  74.     }
  75.  
  76. }
Add Comment
Please, Sign In to add comment