Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
| 20 | import java.util.Arrays; |
| 21 | |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 22 | import org.apache.commons.lang.ArrayUtils; |
| 23 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 25 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 26 | */ |
| 27 | public class Data extends BasePacket { |
| 28 | protected byte[] data; |
| 29 | |
| 30 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 32 | */ |
| 33 | public Data() { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param data |
| 38 | */ |
| 39 | public Data(byte[] data) { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 40 | this.data = ArrayUtils.clone(data); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return the data |
| 45 | */ |
| 46 | public byte[] getData() { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 47 | return ArrayUtils.clone(this.data); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param data the data to set |
| 52 | */ |
| 53 | public Data setData(byte[] data) { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 54 | this.data = ArrayUtils.clone(data); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 55 | return this; |
| 56 | } |
| 57 | |
| 58 | public byte[] serialize() { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 59 | return ArrayUtils.clone(this.data); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 64 | this.data = Arrays.copyOfRange(data, offset, data.length); |
| 65 | return this; |
| 66 | } |
| 67 | |
| 68 | /* (non-Javadoc) |
| 69 | * @see java.lang.Object#hashCode() |
| 70 | */ |
| 71 | @Override |
| 72 | public int hashCode() { |
| 73 | final int prime = 1571; |
| 74 | int result = super.hashCode(); |
| 75 | result = prime * result + Arrays.hashCode(data); |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | /* (non-Javadoc) |
| 80 | * @see java.lang.Object#equals(java.lang.Object) |
| 81 | */ |
| 82 | @Override |
| 83 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 84 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 85 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 86 | } |
| 87 | if (!super.equals(obj)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 88 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 89 | } |
| 90 | if (!(obj instanceof Data)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 91 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 92 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 93 | Data other = (Data) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 94 | if (!Arrays.equals(data, other.data)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 95 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 96 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | } |