com.h3c.util.pkt

## Class PacketWriter

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

- Direct Known Subclasses:
    OfPacketWriter

      public class PacketWriter
      extends Object

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

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `PacketWriter(byte[] bytes)` Constructs a packet writer backed by the byte array.
    `PacketWriter(ByteBuffer bb)` Constructs a packet writer wrapping a `ByteBuffer`.
    `PacketWriter(int capacity)` Constructs a packet writer creating a backing `ByteBuffer`.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `byte[]` | `array()` Returns the byte array that backs this reader.
    `void` | `setU16(int idx, int uv)` Set two bytes (unsigned 16-bit) equivalent to the given value at the given index.
    `void` | `setU32(int idx, long uv)` Set two bytes (unsigned 32-bit) equivalent to the given value at the given index.
    `String` | `toString()`
    `int` | `wi()` Returns the writer index.
    `int` | `writableBytes()` Returns the count of writable bytes remaining.
    `void` | `write(BigPortNumber pn)` Writes a (u32) port number to the buffer.
    `void` | `write(EthernetType et)` Writes an Ethernet Type value (u16) to the buffer.
    `void` | `write(ICMPv4Type it)` Writes an ICMPv4 Type value (u8) to the buffer.
    `void` | `write(ICMPv6Type it)` Writes an ICMPv6 Type value (u8) to the buffer.
    `void` | `write(IpAddress ip)` Writes an IP address to the buffer.
    `void` | `write(IpProtocol ipp)` Writes an IP Protocol value (u8) to the buffer.
    `void` | `write(MacAddress mac)` Writes a MAC address to the buffer.
    `void` | `write(PortNumber pn)` Writes a (u16) port number to the buffer.
    `void` | `writeByte(byte b)` Writes a single byte to the buffer.
    `void` | `writeBytes(byte[] bytes)` Writes the specified byte array to the buffer.
    `void` | `writeBytes(byte b, int count)` Writes the given byte value the specified number of times.
    `void` | `writeBytes(PacketWriter w)` Writes the bytes in the passed in writer up to it's write index to the buffer.
    `void` | `writeInt(int i)` Writes an int to the buffer as four bytes.
    `void` | `writeLong(long l)` Writes a long to the buffer as eight bytes.
    `void` | `writeString(String str)` Writes a string to the buffer.
    `void` | `writeString(String str, int fieldLength)` Writes a null-terminated string to the buffer, allocating a field of the specified length.
    `void` | `writeU16(int uv)` Writes two bytes (unsigned 16-bit) equivalent to the given value.
    `void` | `writeU24(int uv)` Writes three bytes (unsigned 24-bit) equivalent to the given value.
    `void` | `writeU32(long uv)` Writes four bytes (unsigned 32-bit) equivalent to the given value.
    `void` | `writeU8(int uv)` Writes a single byte (unsigned 8-bit) equivalent to the given value.
    `void` | `writeU8(short uv)` Writes a single byte (unsigned 8-bit) equivalent to the given value.
    `void` | `writeZeros(int count)` Writes the specified number of NUL bytes (0x00) to the buffer.

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

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

- - ### Constructor Detail

    - #### PacketWriter

          public PacketWriter(ByteBuffer bb)

      Constructs a packet writer wrapping a `ByteBuffer`.
      Parameters:
        `bb` - the byte buffer

    - #### PacketWriter

          public PacketWriter(int capacity)

      Constructs a packet writer creating a backing `ByteBuffer`.
      Parameters:
        `capacity` - the new writer's capacity, in bytes

    - #### PacketWriter

          public PacketWriter(byte[] bytes)

      Constructs a packet writer backed by the byte array.
      Parameters:
        `bytes` - the new writer's backed byte array

  - ### Method Detail

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### wi

          public int wi()

      Returns the writer index.
      Returns:
        the writer index

    - #### writableBytes

          public int writableBytes()

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

    - #### array

          public byte[] array()

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

    - #### write

          public void write(IpAddress ip)

      Writes an IP address to the buffer. If the argument is an IPv4 address, 4 bytes will be written; If the argument is an IPv6 address, 16 bytes will be written.
      Parameters:
        `ip` - the IP address to write

    - #### write

          public void write(MacAddress mac)

      Writes a MAC address to the buffer.
      Parameters:
        `mac` - the MAC address to write

    - #### write

          public void write(EthernetType et)

      Writes an Ethernet Type value (u16) to the buffer.
      Parameters:
        `et` - the Ethernet Type to write

    - #### write

          public void write(IpProtocol ipp)

      Writes an IP Protocol value (u8) to the buffer.
      Parameters:
        `ipp` - the IP Protocol to write

    - #### write

          public void write(ICMPv4Type it)

      Writes an ICMPv4 Type value (u8) to the buffer.
      Parameters:
        `it` - the ICMPv4 Type to write

    - #### write

          public void write(ICMPv6Type it)

      Writes an ICMPv6 Type value (u8) to the buffer.
      Parameters:
        `it` - the ICMPv6 Type to write

    - #### write

          public void write(PortNumber pn)

      Writes a (u16) port number to the buffer.
      Parameters:
        `pn` - the port number to write

    - #### write

          public void write(BigPortNumber pn)

      Writes a (u32) port number to the buffer.
      Parameters:
        `pn` - the port number to write

    - #### writeString

          public void writeString(String str,
                         int fieldLength)

      Writes a null-terminated string to the buffer, allocating a field of the specified length. Note that if the specified string is longer than fieldLength-1, it will be truncated, with the last byte of the field being set to NUL (0x00). If the specified string is shorter than fieldLength-1, the remaining bytes in the field will be set to NUL. If the specified string is null, a zero-filled array will be returned.
      Parameters:
        `str` - the string to write
        `fieldLength` - the length of the field

    - #### writeString

          public void writeString(String str)

      Writes a string to the buffer. The string is converted to an ASCII encoded array of bytes. The number of bytes written is equal to the string length.
      Parameters:
        `str` - the string to write

    - #### writeByte

          public void writeByte(byte b)

      Writes a single byte to the buffer.
      Parameters:
        `b` - the byte to write

    - #### writeBytes

          public void writeBytes(byte[] bytes)

      Writes the specified byte array to the buffer.
      Parameters:
        `bytes` - the bytes to write

    - #### writeBytes

          public void writeBytes(PacketWriter w)

      Writes the bytes in the passed in writer up to it's write index to the buffer.
      Parameters:
        `w` - packet writer containing the bytes to write

    - #### writeBytes

          public void writeBytes(byte b,
                        int count)

      Writes the given byte value the specified number of times.
      Parameters:
        `b` - the byte value to write
        `count` - the number of times to write it

    - #### writeZeros

          public void writeZeros(int count)

      Writes the specified number of NUL bytes (0x00) to the buffer.
      Parameters:
        `count` - the number of zeros to write

    - #### writeInt

          public void writeInt(int i)

      Writes an int to the buffer as four bytes.
      Parameters:
        `i` - the int value

    - #### writeLong

          public void writeLong(long l)

      Writes a long to the buffer as eight bytes.
      Parameters:
        `l` - the long value

    - #### writeU8

          public void writeU8(short uv)

      Writes a single byte (unsigned 8-bit) equivalent to the given value.
      Parameters:
        `uv` - the unsigned value to write
      Throws:
        `IllegalArgumentException` - if u is not 0..2^8-1

    - #### writeU8

          public void writeU8(int uv)

      Writes a single byte (unsigned 8-bit) equivalent to the given value.
      Parameters:
        `uv` - the unsigned value to write
      Throws:
        `IllegalArgumentException` - if u is not 0..2^8-1

    - #### writeU16

          public void writeU16(int uv)

      Writes two bytes (unsigned 16-bit) equivalent to the given value.
      Parameters:
        `uv` - the unsigned value to write
      Throws:
        `IllegalArgumentException` - if u is not 0..2^16-1

    - #### setU16

          public void setU16(int idx,
                    int uv)

      Set two bytes (unsigned 16-bit) equivalent to the given value at the given index.
      Parameters:
        `idx` - the absolute index in the buffer
        `uv` - the unsigned value to write
      Throws:
        `IllegalArgumentException` - if u is not 0..2^16-1

    - #### setU32

          public void setU32(int idx,
                    long uv)

      Set two bytes (unsigned 32-bit) equivalent to the given value at the given index.
      Parameters:
        `idx` - the absolute index in the buffer
        `uv` - the unsigned value to write
      Throws:
        `IllegalArgumentException` - if u is not 0..2^16-1

    - #### writeU24

          public void writeU24(int uv)

      Writes three bytes (unsigned 24-bit) equivalent to the given value.
      Parameters:
        `uv` - the unsigned value to write
      Throws:
        `IllegalArgumentException` - if u is not 0..2^24-1

    - #### writeU32

          public void writeU32(long uv)

      Writes four bytes (unsigned 32-bit) equivalent to the given value.
      Parameters:
        `uv` - the unsigned value to write
      Throws:
        `IllegalArgumentException` - if u is not 0..2^32-1

Copyright © 2014. All Rights Reserved.