/**
   * Initializes the packet's state and data by doing a deep copy of the contents of the buffer.
   *
   * @param buffer buffer containing both state and data in the form
   *     <pre>
   * +--------------+-------------+
   * | packet state | packet data |
   * +--------------+-------------+
   * </pre>
   */
  public JMemoryPacket(JBuffer buffer) {
    super(POINTER);

    header.setWirelen(buffer.size());

    final int len = buffer.size();
    JBuffer b = getMemoryBuffer(len);

    b.transferFrom(buffer); // Make a buffer to buffer copy

    peer(b, 0, len);

    header.setWirelen(len);
  }
  /**
   * Peers the contents of the buffer directly with this packet. No copies are performed but the
   * packet state and data are expected to be contained within the buffer with a certain layout as
   * described below:
   *
   * <p>Supplied buffer layout expected:
   *
   * <pre>
   * +-----+----+
   * |State|Data|
   * +-----+----+
   * </pre>
   *
   * @param buffer buffer containing packet header, state and data
   * @param offset starting offset into the buffer
   * @return number of bytes peered
   */
  public int peerStateAndData(JBuffer buffer, int offset) {

    state.peerTo(buffer, offset, State.sizeof(0));
    int o = state.peerTo(buffer, offset, State.sizeof(state.getHeaderCount()));
    o += super.peer(buffer, offset + o, header.caplen());

    return o;
  }
  /**
   * Preallocates a packet with internal buffer of the supplied size.
   *
   * @param size number of bytes to pre allocate
   */
  public JMemoryPacket(int size) {
    super(size, 0);

    header.setWirelen(size);

    /** Bug #2878768 JMemoryPacket(int) constructor doesn't work */
    super.peer(super.memory);
  }
  /**
   * Initializes the packet's state and data by doing a deep copy of the contents of the buffer.
   *
   * @param buffer buffer containing both state and data in the form
   *     <pre>
   * +--------------+-------------+
   * | packet state | packet data |
   * +--------------+-------------+
   * </pre>
   */
  public JMemoryPacket(byte[] buffer) {
    super(Type.POINTER);

    final JBuffer mem = getMemoryBuffer(buffer);
    super.peer(mem);

    header.setWirelen(buffer.length);
  }
  /**
   * Initializes the packet's state and data by doing a deep copy of the contents of the buffer.
   *
   * @param buffer buffer containing both state and data in the form
   *     <pre>
   * +--------------+-------------+
   * | packet state | packet data |
   * +--------------+-------------+
   * </pre>
   *
   * @throws PeeringException if there is a problem peering with the buffer
   */
  public JMemoryPacket(ByteBuffer buffer) throws PeeringException {
    super(Type.POINTER);

    final int size = buffer.limit() - buffer.position();

    final JBuffer mem = getMemoryBuffer(size);
    super.peer(mem);

    transferFrom(buffer);

    header.setWirelen(size);
  }
 /**
  * Changes the wirelen of this packet.
  *
  * @param wirelen new wirelen for this packet
  */
 public void setWirelen(int wirelen) {
   header.setWirelen(wirelen);
 }