blob: 57171de042016a2bc0b307aef84020bad50ef1eb [file] [log] [blame]
Carmelo Cascone6b32c992016-04-13 11:53:09 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onlab.util;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.base.Objects;
21
22import java.nio.ByteBuffer;
23import java.nio.ByteOrder;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070024import java.util.Arrays;
Carmelo Cascone6b32c992016-04-13 11:53:09 -070025
26import static com.google.common.base.Preconditions.checkArgument;
27import static org.apache.commons.lang3.ArrayUtils.reverse;
28
29/**
30 * Immutable sequence of bytes, assumed to represent a value in
31 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN} order.
32 * <p>
33 * Sequences can be created copying from an already existing representation of a
34 * sequence of bytes, such as {@link ByteBuffer} or {@code byte[]}; or by
35 * copying bytes from a primitive data type, such as {@code long}, {@code int}
36 * or {@code short}. In the first case, bytes are assumed to be already given in
37 * big-endian order, while in the second case big-endianness is enforced by this
38 * class.
39 */
40public final class ImmutableByteSequence {
41
42 /*
43 Actual bytes are backed by a byte buffer.
44 The order of a newly-created byte buffer is always BIG_ENDIAN.
45 */
46 private ByteBuffer value;
47
48 /**
49 * Private constructor.
50 * Creates a new byte sequence object backed by the passed ByteBuffer.
51 *
52 * @param value a byte buffer
53 */
54 private ImmutableByteSequence(ByteBuffer value) {
55 this.value = value;
56 // Rewind buffer so it's ready to be read.
57 // No write operation should be performed on it from now on.
58 this.value.rewind();
59 }
60
61 /**
62 * Creates a new immutable byte sequence with the same content and order of
63 * the passed byte array.
64 *
65 * @param original a byte array value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070066 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -070067 */
68 public static ImmutableByteSequence copyFrom(byte[] original) {
69 checkArgument(original != null && original.length > 0,
70 "Cannot copy from an empty or null array");
71 return new ImmutableByteSequence(
72 ByteBuffer.allocate(original.length).put(original));
73 }
74
75 /**
76 * Creates a new immutable byte sequence copying bytes from the given
77 * ByteBuffer {@link ByteBuffer}. If the byte buffer order is not big-endian
78 * bytes will be copied in reverse order.
79 *
80 * @param original a byte buffer
81 * @return a new byte buffer object
82 */
83 public static ImmutableByteSequence copyFrom(ByteBuffer original) {
84 checkArgument(original != null && original.capacity() > 0,
85 "Cannot copy from an empty or null byte buffer");
86
87 byte[] bytes = new byte[original.capacity()];
88
89 // copy bytes from original buffer
90 original.rewind();
91 original.get(bytes);
92
93 if (original.order() == ByteOrder.LITTLE_ENDIAN) {
94 // FIXME: this can be improved, e.g. read bytes in reverse order from original
95 reverse(bytes);
96 }
97
98 return new ImmutableByteSequence(ByteBuffer.wrap(bytes));
99 }
100
101 /**
102 * Creates a new byte sequence of 8 bytes containing the given long value.
103 *
104 * @param original a long value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700105 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700106 */
107 public static ImmutableByteSequence copyFrom(long original) {
108 return new ImmutableByteSequence(
109 ByteBuffer.allocate(Long.BYTES).putLong(original));
110 }
111
112 /**
113 * Creates a new byte sequence of 4 bytes containing the given int value.
114 *
115 * @param original an int value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700116 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700117 */
118 public static ImmutableByteSequence copyFrom(int original) {
119 return new ImmutableByteSequence(
120 ByteBuffer.allocate(Integer.BYTES).putInt(original));
121 }
122
123 /**
124 * Creates a new byte sequence of 2 bytes containing the given short value.
125 *
126 * @param original a short value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700127 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700128 */
129 public static ImmutableByteSequence copyFrom(short original) {
130 return new ImmutableByteSequence(
131 ByteBuffer.allocate(Short.BYTES).putShort(original));
132 }
133
134 /**
135 * Creates a new byte sequence of 1 byte containing the given value.
136 *
137 * @param original a byte value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700138 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700139 */
140 public static ImmutableByteSequence copyFrom(byte original) {
141 return new ImmutableByteSequence(
142 ByteBuffer.allocate(Byte.BYTES).put(original));
143 }
144
145 /**
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700146 * Creates a new byte sequence of the given size where alla bits are 0.
147 *
148 * @param size number of bytes
149 * @return a new immutable byte sequence
150 */
151 public static ImmutableByteSequence ofZeros(int size) {
152 byte[] bytes = new byte[size];
153 Arrays.fill(bytes, (byte) 0);
154 return new ImmutableByteSequence(ByteBuffer.wrap(bytes));
155 }
156
157 /**
158 * Creates a new byte sequence of the given size where alla bits are 1.
159 *
160 * @param size number of bytes
161 * @return a new immutable byte sequence
162 */
163 public static ImmutableByteSequence ofOnes(int size) {
164 byte[] bytes = new byte[size];
165 Arrays.fill(bytes, (byte) 0xFF);
166 return new ImmutableByteSequence(ByteBuffer.wrap(bytes));
167 }
168
169 /**
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700170 * Returns a view of this sequence as a read-only {@link ByteBuffer}.
171 * <p>
172 * The returned buffer will have position 0, while limit and capacity will
173 * be set to this sequence {@link #size()}. The buffer order will be
174 * big-endian.
175 *
176 * @return a read-only byte buffer
177 */
178 public ByteBuffer asReadOnlyBuffer() {
179 // position, limit and capacity set rewind at constructor
180 return value.asReadOnlyBuffer();
181 }
182
183 /**
184 * Gets the number of bytes in this sequence.
185 *
186 * @return an integer value
187 */
188 public int size() {
189 return this.value.capacity();
190 }
191
192 /**
193 * Creates a new byte array view of this sequence.
194 *
195 * @return a new byte array
196 */
197 public byte[] asArray() {
198 ByteBuffer bb = asReadOnlyBuffer();
199 byte[] bytes = new byte[size()];
200 bb.get(bytes);
201 return bytes;
202 }
203
204 @Override
205 public int hashCode() {
206 return value.hashCode();
207 }
208
209 @Override
210 public boolean equals(Object obj) {
211 if (this == obj) {
212 return true;
213 }
214 if (obj == null || getClass() != obj.getClass()) {
215 return false;
216 }
217 final ImmutableByteSequence other = (ImmutableByteSequence) obj;
218 return Objects.equal(this.value, other.value);
219 }
220
221 @Override
222 public String toString() {
223 return MoreObjects.toStringHelper(this)
224 .addValue(HexString.toHexString(asArray()))
225 .toString();
226 }
227}