/** * Sets the packet type by updating the packet type bits in the {@code flags} bitfield. Other bits * are unaffected. * * @param type the type to set * @return {@code this} (for fluent interface) */ public Packet setPacketType(Packet.Type type) { int nonTypeFlags = flags & (~FLAG_TYPE0 & ~FLAG_TYPE1 & ~FLAG_TYPE2); resetFlagsTo(type.headerEncoding | nonTypeFlags); return this; }
/** * Sets the packet type by updating the packet type bits in the {@code flags} bitfield. Other bits * are unaffected. * * @param type the type to set * @return {@code this} (for fluent interface) */ public Packet setPacketType(Packet.Type type) { int nonTypeFlags = flags & (~FLAG_TYPE0 & ~FLAG_TYPE1 & ~FLAG_TYPE2); resetFlagsTo(type.headerEncoding | nonTypeFlags); return this; }
/** * Reads the packet data from the supplied {@code ByteBuffer}. The buffer may not contain the complete packet. * If this method returns {@code false}, it should be called again to read more packet data. * * @param src the source byte buffer * @return the read Packet if all the packet's data is now read; {@code null} otherwise. */ public Packet readFrom(ByteBuffer src) { if (!headerComplete) { if (src.remaining() < HEADER_SIZE) { return null; } byte version = src.get(); if (VERSION != version) { throw new IllegalArgumentException("Packet versions are not matching! Expected -> " + VERSION + ", Incoming -> " + version); } flags = src.getChar(); partitionId = src.getInt(); size = src.getInt(); headerComplete = true; } if (readValue(src)) { Packet packet = new Packet(payload, partitionId).resetFlagsTo(flags); reset(); return packet; } else { return null; } }
/** * Reads the packet data from the supplied {@code ByteBuffer}. The buffer may not contain the complete packet. * If this method returns {@code false}, it should be called again to read more packet data. * * @param src the source byte buffer * @return the read Packet if all the packet's data is now read; {@code null} otherwise. */ public Packet readFrom(ByteBuffer src) { if (!headerComplete) { if (src.remaining() < HEADER_SIZE) { return null; } byte version = src.get(); if (VERSION != version) { throw new IllegalArgumentException("Packet versions are not matching! Expected -> " + VERSION + ", Incoming -> " + version); } flags = src.getChar(); partitionId = src.getInt(); size = src.getInt(); headerComplete = true; } if (readValue(src)) { Packet packet = new Packet(payload, partitionId).resetFlagsTo(flags); reset(); return packet; } else { return null; } }