Fix some null warnings
[deliverable/tracecompass.git] / pcap / org.eclipse.tracecompass.pcap.core / src / org / eclipse / tracecompass / internal / pcap / core / protocol / unknown / UnknownPacket.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.pcap.core.protocol.unknown;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.UnsupportedEncodingException;
18 import java.nio.ByteBuffer;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.tracecompass.common.core.NonNullUtils;
24 import org.eclipse.tracecompass.internal.pcap.core.packet.Packet;
25 import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol;
26 import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
27 import org.eclipse.tracecompass.internal.pcap.core.util.ConversionHelper;
28
29 import com.google.common.collect.ImmutableMap;
30 import com.google.common.collect.ImmutableMap.Builder;
31
32 /**
33 * Class that represents an Unknown packet. It is possible to get such a packet
34 * if the protocol has not been implemented in this library or if the parent
35 * packet was invalid (in certain cases only). The header of such a packet is
36 * inexistent.
37 *
38 * @author Vincent Perot
39 */
40 public class UnknownPacket extends Packet {
41
42 private final @Nullable Packet fChildPacket;
43 private final ByteBuffer fPayload;
44
45 private @Nullable UnknownEndpoint fSourceEndpoint;
46 private @Nullable UnknownEndpoint fDestinationEndpoint;
47
48 private @Nullable Map<String, String> fFields;
49
50 /**
51 * Constructor of an Unknown Packet object.
52 *
53 * @param file
54 * The file to which this packet belongs.
55 * @param parent
56 * The parent packet of this packet.
57 * @param packet
58 * The entire packet (header and payload).
59 */
60 public UnknownPacket(PcapFile file, @Nullable Packet parent, ByteBuffer packet) {
61 super(file, parent, PcapProtocol.UNKNOWN);
62
63 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
64 // methods.
65 fSourceEndpoint = null;
66 fDestinationEndpoint = null;
67
68 fFields = null;
69
70 // Header is not used. All data go into payload.
71 fPayload = packet;
72
73 fChildPacket = findChildPacket();
74 }
75
76 @Override
77 public @Nullable Packet getChildPacket() {
78 return fChildPacket;
79 }
80
81 @Override
82 public @Nullable ByteBuffer getPayload() {
83 return fPayload;
84 }
85
86 @Override
87 protected @Nullable Packet findChildPacket() {
88 return null;
89 }
90
91 @Override
92 public String toString() {
93 byte[] array = checkNotNull(fPayload.array());
94 String string = "Payload: " + ConversionHelper.bytesToHex(array, true); //$NON-NLS-1$
95 final Packet child = fChildPacket;
96 if (child != null) {
97 return string + child.toString();
98 }
99 return string;
100 }
101
102 @Override
103 public boolean validate() {
104 // Not yet implemented. ATM, we consider that all packets are valid.
105 // This is the case for all packets.
106 // TODO Implement it.
107 return true;
108 }
109
110 @Override
111 public UnknownEndpoint getSourceEndpoint() {
112 @Nullable
113 UnknownEndpoint endpoint = fSourceEndpoint;
114 if (endpoint == null) {
115 endpoint = new UnknownEndpoint(this, true);
116 }
117 fSourceEndpoint = endpoint;
118 return fSourceEndpoint;
119 }
120
121 @Override
122 public UnknownEndpoint getDestinationEndpoint() {
123 @Nullable
124 UnknownEndpoint endpoint = fDestinationEndpoint;
125 if (endpoint == null) {
126 endpoint = new UnknownEndpoint(this, false);
127 }
128 fDestinationEndpoint = endpoint;
129 return fDestinationEndpoint;
130 }
131
132 @Override
133 public Map<String, String> getFields() {
134 Map<String, String> map = fFields;
135 if (map == null) {
136 byte[] array = checkNotNull(fPayload.array());
137
138 Builder<String, String> builder = ImmutableMap.<@NonNull String, @NonNull String> builder()
139 .put("Binary", ConversionHelper.bytesToHex(array, true)); //$NON-NLS-1$
140 try {
141 String s = new String(array, "UTF-8"); //$NON-NLS-1$
142 builder.put("Character", s); //$NON-NLS-1$
143 } catch (UnsupportedEncodingException e) {
144 // Do nothing. The string won't be added to the map anyway.
145 }
146 fFields = checkNotNull(builder.build());
147 return fFields;
148 }
149 return map;
150 }
151
152 @Override
153 public String getLocalSummaryString() {
154 return "Len: " + fPayload.array().length + " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
155 }
156
157 @Override
158 protected String getSignificationString() {
159 return "Data: " + fPayload.array().length + " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
160 }
161
162 @Override
163 public Packet getMostEcapsulatedPacket() {
164 Packet packet = this.getParentPacket();
165 if (packet == null) {
166 return this;
167 }
168 return packet;
169 }
170
171 @Override
172 public int hashCode() {
173 final int prime = 31;
174 int result = prime;
175 final Packet child = fChildPacket;
176 if (child != null) {
177 result += child.hashCode();
178 }
179 result = prime * result + fPayload.hashCode();
180 return result;
181 }
182
183 @Override
184 public boolean equals(@Nullable Object obj) {
185 if (this == obj) {
186 return true;
187 }
188 if (obj == null) {
189 return false;
190 }
191 if (getClass() != obj.getClass()) {
192 return false;
193 }
194 UnknownPacket other = (UnknownPacket) obj;
195 if (!NonNullUtils.equalsNullable(fChildPacket, other.fChildPacket)) {
196 return false;
197 }
198 if (!fPayload.equals(other.fPayload)) {
199 return false;
200 }
201 return true;
202 }
203
204 }
This page took 0.047866 seconds and 5 git commands to generate.