/*
- * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2013, 2015 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
*/
public abstract class Packet {
- protected static final Logger logger = LoggerFactory
- .getLogger(Packet.class);
+ protected static final Logger LOG = LoggerFactory.getLogger(Packet.class);
// Access level granted to this packet
protected boolean writeAccess;
// When deserialized from wire, packet could result corrupted
corrupted = false;
}
- public Packet(boolean writeAccess) {
+ public Packet(final boolean writeAccess) {
this.writeAccess = writeAccess;
corrupted = false;
}
return payload;
}
- public void setParent(Packet parent) {
+ public void setParent(final Packet parent) {
this.parent = parent;
}
- public void setPayload(Packet payload) {
+ public void setPayload(final Packet payload) {
this.payload = payload;
}
- public void setHeaderField(String headerField, byte[] readValue) {
+ public void setHeaderField(final String headerField, final byte[] readValue) {
hdrFieldsMap.put(headerField, readValue);
}
* This method deserializes the data bits obtained from the wire into the
* respective header and payload which are of type Packet
*
- * @param byte[] data - data from wire to deserialize
- * @param int bitOffset bit position where packet header starts in data
+ * @param data - data from wire to deserialize
+ * @param bitOffset bit position where packet header starts in data
* array
- * @param int size of packet in bits
+ * @param size size of packet in bits
* @return Packet
* @throws PacketException
*/
- public Packet deserialize(byte[] data, int bitOffset, int size)
+ public Packet deserialize(final byte[] data, final int bitOffset, final int size)
throws PacketException {
// Deserialize the header fields one by one
try {
hdrFieldBytes = BitBufferHelper.getBits(data, startOffset,
numBits);
- } catch (BufferException e) {
+ } catch (final BufferException e) {
throw new PacketException(e.getMessage());
}
*/
this.setHeaderField(hdrField, hdrFieldBytes);
- if (logger.isTraceEnabled()) {
- logger.trace("{}: {}: {} (offset {} bitsize {})",
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("{}: {}: {} (offset {} bitsize {})",
new Object[] { this.getClass().getSimpleName(), hdrField,
HexEncode.bytesToHexString(hdrFieldBytes),
startOffset, numBits });
if (payloadClass != null) {
try {
payload = payloadClass.newInstance();
- } catch (Exception e) {
+ } catch (final Exception e) {
throw new RuntimeException(
"Error parsing payload for Ethernet packet", e);
}
} else if (rawPayload != null) {
payloadBytes = rawPayload;
}
- int payloadSize = (payloadBytes == null) ? 0 : payloadBytes.length;
+ int payloadSize = payloadBytes == null ? 0 : payloadBytes.length;
// Allocate the buffer to contain the full (header + payload) packet
int headerSize = this.getHeaderSize() / NetUtils.NumBitsInAByte;
try {
BitBufferHelper.setBytes(packetBytes, fieldBytes,
getfieldOffset(field), getfieldnumBits(field));
- } catch (BufferException e) {
+ } catch (final BufferException e) {
throw new PacketException(e.getMessage());
}
}
// Perform post serialize operations (like checksum computation)
postSerializeCustomOperation(packetBytes);
- if (logger.isTraceEnabled()) {
- logger.trace("{}: {}", this.getClass().getSimpleName(),
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("{}: {}", this.getClass().getSimpleName(),
HexEncode.bytesToHexString(packetBytes));
}
* normal Packet.serialize() path. An example is the checksum computation
* for IPv4
*
- * @param byte[] - serialized bytes
+ * @param myBytes serialized bytes
* @throws PacketException
*/
protected void postSerializeCustomOperation(byte[] myBytes)
* Currently only IPv4 and ICMP do checksum computation and validation. TCP
* and UDP need to implement these if required.
*
- * @param byte[] data The byte stream representing the Ethernet frame
- * @param int startBitOffset The bit offset from where the byte array corresponding to this Packet starts in the frame
+ * @param data The byte stream representing the Ethernet frame
+ * @param startBitOffset The bit offset from where the byte array corresponding to this Packet starts in the frame
* @throws PacketException
*/
protected void postDeserializeCustomOperation(byte[] data, int startBitOffset)
* 'fieldname'. The offset is present in the hdrFieldCoordMap of the
* respective packet class
*
- * @param String
- * fieldName
* @return Integer - startOffset of the requested field
*/
- public int getfieldOffset(String fieldName) {
+ public int getfieldOffset(final String fieldName) {
return hdrFieldCoordMap.get(fieldName).getLeft();
}
* 'fieldname'. The numBits are present in the hdrFieldCoordMap of the
* respective packet class
*
- * @param String
- * fieldName
* @return Integer - number of bits of the requested field
*/
- public int getfieldnumBits(String fieldName) {
+ public int getfieldnumBits(final String fieldName) {
return hdrFieldCoordMap.get(fieldName).getRight();
}
*
* @param payload The raw payload as byte array
*/
- public void setRawPayload(byte[] payload) {
+ public void setRawPayload(final byte[] payload) {
this.rawPayload = Arrays.copyOf(payload, payload.length);
}
final int prime = 31;
int result = super.hashCode();
result = prime * result
- + ((this.hdrFieldsMap == null) ? 0 : hdrFieldsMap.hashCode());
+ + (this.hdrFieldsMap == null ? 0 : hdrFieldsMap.hashCode());
return result;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}