/** * Copies both state and data from supplied packet to this packet by performing 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 packet source packet * @return number of bytes copied */ public int transferStateAndDataFrom(JPacket packet) { int len = packet.state.size() + packet.size(); JBuffer mem = getMemoryBuffer(len); int o = packet.state.transferTo(mem, 0, packet.state.size(), 0); o += packet.transferTo(mem, 0, packet.size(), o); 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); }