com.h3c.util.pkt

## Class PacketReader

- java.lang.Object
- - com.h3c.util.pkt.PacketReader

- Direct Known Subclasses:
    OfPacketReader

      public class PacketReader
      extends Object

  Wraps a `ByteBuffer` and provides methods to read rich data types from it. Supported data types are from the `com.h3c.util.ip` package.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `PacketReader(ByteBuffer b)` Constructs a packet reader wrapping a `ByteBuffer`.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `byte[]` | `array()` Returns the byte array that backs this reader.
    `int` | `limit()` Returns the limit of usable bytes.
    `int` | `odometer()` Number of bytes read since creation of this reader or since the last call to reset the odometer.
    `byte` | `peek(int offset)` Peek at the byte at the specified offset from current reader index.
    `int` | `peekU16()` Peek at the next two bytes from the current position, interpreting them as an unsigned 16-bit value.
    `int` | `peekU16(int offset)` Peek at the next two bytes from the given offset from the current position, interpreting them as an unsigned 16-bit value.
    `int` | `peekU24()` Peek at the next three bytes from the current position, interpreting them as an unsigned 24-bit value.
    `int` | `peekU24(int offset)` Peek at the next three bytes from the given offset from the current position, interpreting them as an unsigned 24-bit value.
    `short` | `peekU8()` Peek at the next byte from the current position, interpreting it as an unsigned 8-bit value.
    `short` | `peekU8(int offset)` Peek at the byte at the given offset from the current position, interpreting it as an unsigned 8-bit value.
    `int` | `readableBytes()` Returns the count of readable bytes remaining.
    `BigPortNumber` | `readBigPortNumber()` Reads a (u32) port number from the buffer.
    `byte` | `readByte()` Reads and returns a single byte from the buffer.
    `byte[]` | `readBytes(int n)` Reads the specified number of bytes from the buffer, returning them in a newly allocated array.
    `EthernetType` | `readEthernetType()` Reads an Ethernet type from the buffer.
    `ICMPv4Type` | `readIcmpv4Type()` Reads an ICMPv4 Type from the buffer.
    `ICMPv6Type` | `readIcmpv6Type()` Reads an ICMPv6 Type from the buffer.
    `int` | `readInt()` Reads four bytes as an int.
    `IpProtocol` | `readIpProtocol()` Reads an IP Protocol from the buffer.
    `IpAddress` | `readIPv4Address()` Reads an IPv4 address from the buffer.
    `IpAddress` | `readIPv6Address()` Reads an IPv6 address from the buffer.
    `long` | `readLong()` Reads eight bytes as a long.
    `MacAddress` | `readMacAddress()` Reads a MAC address from the buffer.
    `PortNumber` | `readPortNumber()` Reads a (u16) port number from the buffer.
    `String` | `readString(int n)` Reads a string from the buffer.
    `int` | `readU16()` Reads two bytes, interpreting them as an unsigned 16-bit value.
    `int` | `readU24()` Reads three bytes, interpreting them as an unsigned 24-bit value.
    `long` | `readU32()` Reads four bytes, interpreting them as an unsigned 32-bit value.
    `short` | `readU8()` Reads a byte, interpreting it as an unsigned 8-bit value.
    `void` | `resetIndex()` Resets the buffer position to the reader's original starting position.
    `void` | `resetOdometer()` Resets the odometer to the current buffer position.
    `int` | `ri()` Returns the reader index.
    `void` | `ri(int newPosition)` Sets the reader index to the given position.
    `void` | `skip(int count)` Skips buffer data by increasing the current reader index by the specified number of bytes.
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### PacketReader

          public PacketReader(ByteBuffer b)

      Constructs a packet reader wrapping a `ByteBuffer`.
      Parameters:
        `b` - the byte buffer

  - ### Method Detail

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### odometer

          public int odometer()

      Number of bytes read since creation of this reader or since the last call to reset the odometer.
      Returns:
        the number of bytes read since creation or reset

    - #### resetOdometer

          public void resetOdometer()

      Resets the odometer to the current buffer position.

    - #### resetIndex

          public void resetIndex()

      Resets the buffer position to the reader's original starting position. The odometer is reset to 0.

    - #### ri

          public int ri()

      Returns the reader index.
      Returns:
        the reader index

    - #### ri

          public void ri(int newPosition)

      Sets the reader index to the given position. The new position must be non-negative and no larger than the current limit.
      Parameters:
        `newPosition` - the new position
      Throws:
        `IllegalArgumentException` - if preconditions for newPosition are not met

    - #### readableBytes

          public int readableBytes()

      Returns the count of readable bytes remaining.
      Returns:
        the number of readable bytes

    - #### limit

          public int limit()

      Returns the limit of usable bytes.
      Returns:
        the limit

    - #### array

          public byte[] array()

      Returns the byte array that backs this reader.
      Returns:
        the backing byte array

    - #### readIPv4Address

          public IpAddress readIPv4Address()

      Reads an IPv4 address from the buffer.
      Returns:
        an IP address (IPv4)

    - #### readIPv6Address

          public IpAddress readIPv6Address()

      Reads an IPv6 address from the buffer.
      Returns:
        an IP address (IPv6)

    - #### readMacAddress

          public MacAddress readMacAddress()

      Reads a MAC address from the buffer.
      Returns:
        a MAC address

    - #### readEthernetType

          public EthernetType readEthernetType()

      Reads an Ethernet type from the buffer.
      Returns:
        an Ethernet type

    - #### readIpProtocol

          public IpProtocol readIpProtocol()

      Reads an IP Protocol from the buffer.
      Returns:
        an IP Protocol

    - #### readIcmpv4Type

          public ICMPv4Type readIcmpv4Type()

      Reads an ICMPv4 Type from the buffer.
      Returns:
        an ICMPv4 type

    - #### readIcmpv6Type

          public ICMPv6Type readIcmpv6Type()

      Reads an ICMPv6 Type from the buffer.
      Returns:
        an ICMPv6 type

    - #### readPortNumber

          public PortNumber readPortNumber()

      Reads a (u16) port number from the buffer.
      Returns:
        a port number

    - #### readBigPortNumber

          public BigPortNumber readBigPortNumber()

      Reads a (u32) port number from the buffer.
      Returns:
        a big port number

    - #### readString

          public String readString(int n)

      Reads a string from the buffer. The data in the buffer is assumed to be in a field of length N, null terminated and encoded as ASCII. At the end of this operation the reader index of the underlying buffer will have advanced by N bytes.
      Parameters:
        `n` - the size of the string field
      Returns:
        a string

    - #### readBytes

          public byte[] readBytes(int n)

      Reads the specified number of bytes from the buffer, returning them in a newly allocated array.
      Parameters:
        `n` - the number of bytes to read
      Returns:
        a byte array containing the read bytes

    - #### readByte

          public byte readByte()

      Reads and returns a single byte from the buffer.
      Returns:
        the next byte in the buffer

    - #### skip

          public void skip(int count)

      Skips buffer data by increasing the current reader index by the specified number of bytes.
      Parameters:
        `count` - the number of bytes to skip

    - #### readInt

          public int readInt()

      Reads four bytes as an int.
      Returns:
        the int value

    - #### readLong

          public long readLong()

      Reads eight bytes as a long.
      Returns:
        the long value

    - #### readU8

          public short readU8()

      Reads a byte, interpreting it as an unsigned 8-bit value.
      Returns:
        the U8 equivalent of the byte read

    - #### peek

          public byte peek(int offset)

      Peek at the byte at the specified offset from current reader index.
      Parameters:
        `offset` - from the current reader index
      Returns:
        the byte at offset

    - #### peekU8

          public short peekU8(int offset)

      Peek at the byte at the given offset from the current position, interpreting it as an unsigned 8-bit value.
      Parameters:
        `offset` - the offset from the current position
      Returns:
        the U8 equivalent of the byte

    - #### peekU8

          public short peekU8()

      Peek at the next byte from the current position, interpreting it as an unsigned 8-bit value.
      Returns:
        the U8 equivalent of the byte

    - #### peekU16

          public int peekU16(int offset)

      Peek at the next two bytes from the given offset from the current position, interpreting them as an unsigned 16-bit value.
      Parameters:
        `offset` - the offset from the current position
      Returns:
        the U16 equivalent of the bytes read

    - #### peekU16

          public int peekU16()

      Peek at the next two bytes from the current position, interpreting them as an unsigned 16-bit value.
      Returns:
        the U16 equivalent of the bytes read

    - #### peekU24

          public int peekU24(int offset)

      Peek at the next three bytes from the given offset from the current position, interpreting them as an unsigned 24-bit value.
      Parameters:
        `offset` - the offset from the current position
      Returns:
        the U24 equivalent of the bytes read

    - #### peekU24

          public int peekU24()

      Peek at the next three bytes from the current position, interpreting them as an unsigned 24-bit value.
      Returns:
        the U24 equivalent of the bytes read

    - #### readU16

          public int readU16()

      Reads two bytes, interpreting them as an unsigned 16-bit value.
      Returns:
        the U16 equivalent of the bytes read

    - #### readU24

          public int readU24()

      Reads three bytes, interpreting them as an unsigned 24-bit value.
      Returns:
        the U24 equivalent of the bytes read

    - #### readU32

          public long readU32()

      Reads four bytes, interpreting them as an unsigned 32-bit value.
      Returns:
        the U32 equivalent of the bytes read

Copyright © 2014. All Rights Reserved.