/**
   * Performs a deep copy of the contents of the buffer into packet's internal memory buffer if that
   * buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are
   * then peered with the internal buffer containing the copy of the supplied buffer
   *
   * @param buffer buffer containing both state and data in the form
   *     <pre>
   *          +--------------+-------------+
   *          | packet state | packet data |
   *          +--------------+-------------+
   * </pre>
   *
   * @return number of bytes copied
   */
  public int transferStateAndDataFrom(ByteBuffer buffer) {
    final int len = buffer.limit() - buffer.position();
    JBuffer b = getMemoryBuffer(len);

    b.transferFrom(buffer, 0);

    return peerStateAndData(b, 0);
  }
  /**
   * Performs a deep copy of the contents of the buffer into packet's internal memory buffer if that
   * buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are
   * then peered with the internal buffer containing the copy of the supplied buffer
   *
   * @param buffer buffer containing both state and data in the form
   *     <pre>
   *          +--------------+-------------+
   *          | packet state | packet data |
   *          +--------------+-------------+
   * </pre>
   *
   * @return number of bytes copied
   */
  public int transferStateAndDataFrom(JBuffer buffer) {
    final int len = buffer.size();
    JBuffer b = getMemoryBuffer(len);

    b.transferFrom(buffer);

    return peerStateAndData(b, 0);
  }
  /**
   * 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);
  }