08fa003172312af1569daf4eb83700fca177df2e
[controller.git] / third-party / openflowj_netty / src / main / java / org / openflow / protocol / vendor / OFByteArrayVendorData.java
1 /**
2 *    Copyright 2011, Big Switch Networks, Inc. 
3 *    Originally created by David Erickson & Rob Sherwood, 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 **/
17
18 package org.openflow.protocol.vendor;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21
22 /**
23  * Basic implementation of OFVendorData that just treats the data as a
24  * byte array. This is used if there's an OFVendor message where there's
25  * no registered OFVendorId or no specific OFVendorDataType that can be
26  * determined from the data.
27  * 
28  * @author Rob Vaterlaus (rob.vaterlaus@bigswitch.com)
29  */
30 public class OFByteArrayVendorData implements OFVendorData {
31
32     protected byte[] bytes;
33     
34     /**
35      * Construct vendor data with an empty byte array.
36      */
37     public OFByteArrayVendorData() {
38     }
39     
40     /**
41      * Construct vendor data with the specified byte array.
42      * @param bytes
43      */
44     public OFByteArrayVendorData(byte[] bytes) {
45         this.bytes = bytes;
46     }
47     
48     /**
49      * Get the associated byte array for this vendor data.
50      * @return the byte array containing the raw vendor data.
51      */
52     public byte[] getBytes() {
53         return bytes;
54     }
55     
56     /**
57      * Set the byte array for the vendor data.
58      * @param bytes the raw byte array containing the vendor data.
59      */
60     public void setBytes(byte[] bytes) {
61         this.bytes = bytes;
62     }
63     
64     /**
65      * Get the length of the vendor data. In this case it's just then length
66      * of the underlying byte array.
67      * @return the length of the vendor data
68      */
69     @Override
70     public int getLength() {
71         return (bytes != null) ? bytes.length : 0;
72     }
73
74     /**
75      * Read the vendor data from the ChannelBuffer into the byte array.
76      * @param data the channel buffer from which we're deserializing
77      * @param length the length to the end of the enclosing message
78      */
79     @Override
80     public void readFrom(ChannelBuffer data, int length) {
81         bytes = new byte[length];
82         data.readBytes(bytes);
83     }
84
85     /**
86      * Write the vendor data bytes to the ChannelBuffer
87      * @param data the channel buffer to which we're serializing
88      */
89     @Override
90     public void writeTo(ChannelBuffer data) {
91         if (bytes != null)
92             data.writeBytes(bytes);
93     }
94 }