blob: e79cf7bfffbe2fa887d0f5ade504fd24c6e70b97 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20import java.util.Arrays;
21
22/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080023 * @author David Erickson (daviderickson@cs.stanford.edu)
24 */
25public class Data extends BasePacket {
26 protected byte[] data;
27
28 /**
Ray Milkey269ffb92014-04-03 14:43:30 -070029 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030 */
31 public Data() {
32 }
33
34 /**
35 * @param data
36 */
37 public Data(byte[] data) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070038 if (data == null) {
39 this.data = null;
40 } else {
41 this.data = data.clone();
42 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080043 }
44
45 /**
46 * @return the data
47 */
48 public byte[] getData() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070049 if (this.data == null) {
50 return null;
51 }
52 return this.data.clone();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080053 }
54
55 /**
56 * @param data the data to set
57 */
58 public Data setData(byte[] data) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070059 if (data == null) {
60 this.data = null;
61 } else {
62 this.data = data.clone();
63 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080064 return this;
65 }
66
67 public byte[] serialize() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070068 if (this.data == null) {
69 return null;
70 }
71 return this.data.clone();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080072 }
73
74 @Override
75 public IPacket deserialize(byte[] data, int offset, int length) {
76 this.data = Arrays.copyOfRange(data, offset, data.length);
77 return this;
78 }
79
80 /* (non-Javadoc)
81 * @see java.lang.Object#hashCode()
82 */
83 @Override
84 public int hashCode() {
85 final int prime = 1571;
86 int result = super.hashCode();
87 result = prime * result + Arrays.hashCode(data);
88 return result;
89 }
90
91 /* (non-Javadoc)
92 * @see java.lang.Object#equals(java.lang.Object)
93 */
94 @Override
95 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -070096 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080097 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070098 }
99 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800100 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700101 }
102 if (!(obj instanceof Data)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800103 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700104 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 Data other = (Data) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700106 if (!Arrays.equals(data, other.data)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800107 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700108 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800109 return true;
110 }
111}