Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package org.rs2.janus.net.packet;
- import org.jboss.netty.buffer.ChannelBuffer;
- /**
- * @author Michael Schmidt <H3llKing> <msrsps@hotmail.com>
- *
- */
- public class PacketReader {
- /**
- * The buffer the is wrapped by this reader.
- */
- private final ChannelBuffer internalBuffer;
- /**
- * If the reader is in bit access.
- */
- private boolean bitAccess = false;
- /**
- * The bit position of the reader.
- */
- private int bitPosition;
- /**
- * New instance
- *
- * @param packet
- * The packet who's payload will be read.
- */
- public PacketReader(Packet packet) {
- this.internalBuffer = packet.getPayload();
- }
- /**
- *
- */
- public int readBit() {
- int bytePos = bitPosition >> 3;
- int bitOffset = 8 - (bitPosition & 7);
- return (internalBuffer.getByte(bytePos) & PacketConstants.BIT_MASK[bitOffset]) << bitOffset;
- }
- /**
- * Puts the reader into bit access and sets the bit fields.
- */
- public void setBitAccess() {
- if (bitAccess == true)
- throw new IllegalStateException("Already in bit access.");
- bitAccess = true;
- bitPosition = internalBuffer.readerIndex() << 3;
- }
- /**
- * Puts the reader into byte access and resets the bit fields.
- */
- public void setByteAccess() {
- if (bitAccess == false)
- throw new IllegalStateException("Already in byte access.");
- int bytePos = bitPosition >> 3;
- if (bitPosition % 8 != 0) // Round as needed.
- ++bytePos;
- internalBuffer.readerIndex(bytePos);
- bitPosition = 0;
- }
- }
Add Comment
Please, Sign In to add comment