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 | |
| 22 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 23 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 24 | */ |
| 25 | public class Data extends BasePacket { |
| 26 | protected byte[] data; |
| 27 | |
| 28 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 29 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | */ |
| 31 | public Data() { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param data |
| 36 | */ |
| 37 | public Data(byte[] data) { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 38 | if (data == null) { |
| 39 | this.data = null; |
| 40 | } else { |
| 41 | this.data = data.clone(); |
| 42 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return the data |
| 47 | */ |
| 48 | public byte[] getData() { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 49 | if (this.data == null) { |
| 50 | return null; |
| 51 | } |
| 52 | return this.data.clone(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param data the data to set |
| 57 | */ |
| 58 | public Data setData(byte[] data) { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 59 | if (data == null) { |
| 60 | this.data = null; |
| 61 | } else { |
| 62 | this.data = data.clone(); |
| 63 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 64 | return this; |
| 65 | } |
| 66 | |
| 67 | public byte[] serialize() { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 68 | if (this.data == null) { |
| 69 | return null; |
| 70 | } |
| 71 | return this.data.clone(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 72 | } |
| 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 96 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 97 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 98 | } |
| 99 | if (!super.equals(obj)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 100 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 101 | } |
| 102 | if (!(obj instanceof Data)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 103 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 104 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 105 | Data other = (Data) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 106 | if (!Arrays.equals(data, other.data)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 107 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 108 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | } |