- java.lang.Object
-
- org.pcap4j.packet.AbstractPacket
-
- Direct Known Subclasses:
AbstractPppPacket,ArpPacket,BsdLoopbackPacket,DnsPacket,Dot11ManagementPacket,Dot11Selector,Dot1qVlanTagPacket,EthernetPacket,GtpSelector,GtpV1Packet,IcmpIdentifiablePacket,IcmpV4CommonPacket,IcmpV4InvokingPacketPacket,IcmpV6CommonPacket,IcmpV6HomeAgentAddressDiscoveryReplyPacket,IcmpV6HomeAgentAddressDiscoveryRequestPacket,IcmpV6InvokingPacketPacket,IcmpV6MobilePrefixAdvertisementPacket,IcmpV6MobilePrefixSolicitationPacket,IcmpV6NeighborAdvertisementPacket,IcmpV6NeighborSolicitationPacket,IcmpV6RedirectPacket,IcmpV6RouterAdvertisementPacket,IcmpV6RouterSolicitationPacket,IllegalPacket,IpSelector,IpV4Packet,IpV6ExtFragmentPacket,IpV6ExtOptionsPacket,IpV6ExtRoutingPacket,IpV6ExtUnknownPacket,IpV6Packet,LinuxSllPacket,LlcPacket,PppSelector,RadiotapPacket,SctpPacket,SimplePacket,SnapPacket,Ssh2BinaryPacket,Ssh2DebugPacket,Ssh2DisconnectPacket,Ssh2IgnorePacket,Ssh2KexDhInitPacket,Ssh2KexDhReplyPacket,Ssh2KexInitPacket,Ssh2NewKeysPacket,Ssh2ServiceAcceptPacket,Ssh2ServiceRequestPacket,Ssh2UnimplementedPacket,Ssh2VersionExchangePacket,SshPacket,TcpPacket,UdpPacket
public abstract class AbstractPacket extends java.lang.Object implements Packet
Abstract immutable packet class. If you usePropertiesBasedPacketFactory, this subclass must implement the following method:public static Packet newPacket(byte[] rawData, int offset, int length) throws IllegalRawDataException- Since:
- pcap4j 0.9.1
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractPacket.AbstractBuilderAbstract packet builder class.static classAbstractPacket.AbstractHeaderAbstract immutable header class.-
Nested classes/interfaces inherited from interface org.pcap4j.packet.Packet
Packet.Builder, Packet.Header
-
-
Field Summary
Fields Modifier and Type Field Description private LazyValue<java.lang.Integer>hashCodeCacheprivate LazyValue<java.lang.String>hexStringCacheprivate LazyValue<java.lang.Integer>lengthCacheprivate LazyValue<byte[]>rawDataCacheprivate static longserialVersionUIDprivate LazyValue<java.lang.String>stringCache
-
Constructor Summary
Constructors Constructor Description AbstractPacket()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected java.lang.StringbuildHexString()This method builds the valuetoHexString()will return using the return value ofgetRawData().protected byte[]buildRawData()This method builds the valuegetRawData()will return by concatenating the header's raw data and the payload's raw data.protected java.lang.StringbuildString()This method builds the valuetoString()will return by concatenating the header's string representation and the payload's string representation.protected intcalcHashCode()This method calculates the valuehashCode()will return using the header's hash code and the payload's hash code.protected intcalcLength()This method calculates the valuelength()will return by adding up the header length and the payload length.<T extends Packet>
booleancontains(java.lang.Class<T> clazz)Returns true if this packet is or its payload includes an object of specified packet class; false otherwise.booleanequals(java.lang.Object obj)Indicates whether some other object is "equal to" this one.<T extends Packet>
Tget(java.lang.Class<T> clazz)Traverses this packet and its payload to find an object of the specified packet class and returns the object.abstract Packet.BuildergetBuilder()Returns a new Builder object populated with this object's fields' values.Packet.HeadergetHeader()Returns the Header object representing this packet's header.PacketgetOuterOf(java.lang.Class<? extends Packet> clazz)Returns the outer packet object of a packet objectget(clazz)returns.PacketgetPayload()Returns the Packet object representing this packet's payload.byte[]getRawData()Returns this packet's raw data.inthashCode()Returns a hash code value for the object.java.util.Iterator<Packet>iterator()intlength()Returns the packet length in bytes.java.lang.StringtoHexString()Returns the hex string representation of this object.java.lang.StringtoString()Returns a string representation of the object.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
lengthCache
private final LazyValue<java.lang.Integer> lengthCache
-
rawDataCache
private final LazyValue<byte[]> rawDataCache
-
hexStringCache
private final LazyValue<java.lang.String> hexStringCache
-
stringCache
private final LazyValue<java.lang.String> stringCache
-
hashCodeCache
private final LazyValue<java.lang.Integer> hashCodeCache
-
-
Method Detail
-
getHeader
public Packet.Header getHeader()
Returns the Header object representing this packet's header. This subclass have to override this method if the packet represented by the subclass has a header.
-
getPayload
public Packet getPayload()
Returns the Packet object representing this packet's payload. This subclass have to override this method if the packet represented by the subclass has a payload.- Specified by:
getPayloadin interfacePacket- Returns:
- the Packet object representing this packet's payload. May be null if the payload doesn't exist
-
calcLength
protected int calcLength()
This method calculates the valuelength()will return by adding up the header length and the payload length. If you write this subclass which represents a packet with extra parts (e.g. a trailer), you need to override this method.- Returns:
- a calculated length
-
length
public int length()
Returns the packet length in bytes. This method callscalcLength()and caches the return value when it is called for the first time, and then, this method returns the cached value from the second time.
-
buildRawData
protected byte[] buildRawData()
This method builds the valuegetRawData()will return by concatenating the header's raw data and the payload's raw data. If you write this subclass which represents a packet with extra parts (e.g. a trailer), you need to override this method.- Returns:
- a raw data built
-
getRawData
public byte[] getRawData()
Returns this packet's raw data. This method callsbuildRawData()and caches the return value when it is called for the first time, and then, this method returns the cached value from the second time. More correctly, this method returns a copy of the cached value, so that the cache can't be changed.- Specified by:
getRawDatain interfacePacket- Returns:
- this packet's raw data, namely the byte stream which is actually sent through real network
-
iterator
public java.util.Iterator<Packet> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<Packet>
-
get
public <T extends Packet> T get(java.lang.Class<T> clazz)
Description copied from interface:PacketTraverses this packet and its payload to find an object of the specified packet class and returns the object. If there are more than one objects of the specified class in this object, this method returns the most outer one of them.
-
getOuterOf
public Packet getOuterOf(java.lang.Class<? extends Packet> clazz)
Description copied from interface:PacketReturns the outer packet object of a packet objectget(clazz)returns.- Specified by:
getOuterOfin interfacePacket- Parameters:
clazz- the packet class of the object whose outer packet object is what you want to get- Returns:
- a packet object if found; otherwise null
-
contains
public <T extends Packet> boolean contains(java.lang.Class<T> clazz)
Description copied from interface:PacketReturns true if this packet is or its payload includes an object of specified packet class; false otherwise.
-
getBuilder
public abstract Packet.Builder getBuilder()
Description copied from interface:PacketReturns a new Builder object populated with this object's fields' values.- Specified by:
getBuilderin interfacePacket- Returns:
- a new Builder object populated with this object's fields values
-
buildHexString
protected java.lang.String buildHexString()
This method builds the valuetoHexString()will return using the return value ofgetRawData(). Each octet in this return value is separated by a white space. (e.g. 00 01 02 03 aa bb cc)- Returns:
- a hex string representation of this object
-
toHexString
public java.lang.String toHexString()
Returns the hex string representation of this object. This method callsbuildHexString()and caches the return value when it is called for the first time, and then, this method returns the cached value from the second time.- Returns:
- a hex string representation of this object
-
buildString
protected java.lang.String buildString()
This method builds the valuetoString()will return by concatenating the header's string representation and the payload's string representation. If you write this subclass which represents a packet with extra parts (e.g. a trailer), you need to override this method.- Returns:
- a string representation of this object
-
toString
public java.lang.String toString()
Returns a string representation of the object. This method callsbuildString()and caches the return value when it is called for the first time, and then, this method returns the cached value from the second time.- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one. This method firstly compares this packet's header using the header's equals(Object) method, then compares this packet's payload using the payload's equals(Object) method. If you write this subclass with fields which represent somethings other than header or payload, you need to override this method.- Overrides:
equalsin classjava.lang.Object
-
calcHashCode
protected int calcHashCode()
This method calculates the valuehashCode()will return using the header's hash code and the payload's hash code. If you write this subclass which represents a packet with extra parts (e.g. a trailer), you need to override this method.- Returns:
- a calculated hash code value for the object
-
hashCode
public int hashCode()
Returns a hash code value for the object. This method callscalcHashCode()and caches the return value when it is called for the first time, and then, this method returns the cached value from the second time.- Overrides:
hashCodein classjava.lang.Object
-
-