blob: c274e6a7e5188c757f2a8e791a8100e47d41c9b6 [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
Carmelo Cascone6b32c992016-04-13 11:53:09 -070019import com.google.common.base.Objects;
20
21import java.nio.ByteBuffer;
22import java.nio.ByteOrder;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070023import java.util.Arrays;
Carmelo Cascone6b32c992016-04-13 11:53:09 -070024
25import static com.google.common.base.Preconditions.checkArgument;
26import static org.apache.commons.lang3.ArrayUtils.reverse;
27
28/**
29 * Immutable sequence of bytes, assumed to represent a value in
30 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN} order.
31 * <p>
32 * Sequences can be created copying from an already existing representation of a
33 * sequence of bytes, such as {@link ByteBuffer} or {@code byte[]}; or by
34 * copying bytes from a primitive data type, such as {@code long}, {@code int}
35 * or {@code short}. In the first case, bytes are assumed to be already given in
36 * big-endian order, while in the second case big-endianness is enforced by this
37 * class.
38 */
39public final class ImmutableByteSequence {
40
41 /*
42 Actual bytes are backed by a byte buffer.
43 The order of a newly-created byte buffer is always BIG_ENDIAN.
44 */
45 private ByteBuffer value;
46
47 /**
48 * Private constructor.
49 * Creates a new byte sequence object backed by the passed ByteBuffer.
50 *
51 * @param value a byte buffer
52 */
53 private ImmutableByteSequence(ByteBuffer value) {
54 this.value = value;
55 // Rewind buffer so it's ready to be read.
56 // No write operation should be performed on it from now on.
57 this.value.rewind();
58 }
59
60 /**
61 * Creates a new immutable byte sequence with the same content and order of
62 * the passed byte array.
63 *
64 * @param original a byte array value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070065 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -070066 */
67 public static ImmutableByteSequence copyFrom(byte[] original) {
68 checkArgument(original != null && original.length > 0,
69 "Cannot copy from an empty or null array");
70 return new ImmutableByteSequence(
71 ByteBuffer.allocate(original.length).put(original));
72 }
73
74 /**
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070075 * Creates a new immutable byte sequence with the same content and order of
76 * the passed byte array, from/to the given indexes (inclusive).
77 *
78 * @param original a byte array value
Ray Milkeybb23e0b2016-08-02 17:00:21 -070079 * @param fromIdx starting index
80 * @param toIdx ending index
Carmelo Cascone17fc9e42016-05-31 11:29:21 -070081 * @return a new immutable byte sequence
82 */
83 public static ImmutableByteSequence copyFrom(byte[] original, int fromIdx, int toIdx) {
84 checkArgument(original != null && original.length > 0,
85 "Cannot copy from an empty or null array");
86 checkArgument(toIdx >= fromIdx && toIdx < original.length, "invalid indexes");
87 ByteBuffer buffer = ByteBuffer.allocate((toIdx - fromIdx) + 1);
88 for (int i = fromIdx; i <= toIdx; i++) {
89 buffer.put(original[i]);
90 }
91 return new ImmutableByteSequence(buffer);
92 }
93
94 /**
Carmelo Cascone6b32c992016-04-13 11:53:09 -070095 * Creates a new immutable byte sequence copying bytes from the given
96 * ByteBuffer {@link ByteBuffer}. If the byte buffer order is not big-endian
97 * bytes will be copied in reverse order.
98 *
99 * @param original a byte buffer
100 * @return a new byte buffer object
101 */
102 public static ImmutableByteSequence copyFrom(ByteBuffer original) {
103 checkArgument(original != null && original.capacity() > 0,
104 "Cannot copy from an empty or null byte buffer");
105
106 byte[] bytes = new byte[original.capacity()];
107
108 // copy bytes from original buffer
109 original.rewind();
110 original.get(bytes);
111
112 if (original.order() == ByteOrder.LITTLE_ENDIAN) {
113 // FIXME: this can be improved, e.g. read bytes in reverse order from original
114 reverse(bytes);
115 }
116
117 return new ImmutableByteSequence(ByteBuffer.wrap(bytes));
118 }
119
120 /**
121 * Creates a new byte sequence of 8 bytes containing the given long value.
122 *
123 * @param original a long value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700124 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700125 */
126 public static ImmutableByteSequence copyFrom(long original) {
127 return new ImmutableByteSequence(
128 ByteBuffer.allocate(Long.BYTES).putLong(original));
129 }
130
131 /**
132 * Creates a new byte sequence of 4 bytes containing the given int value.
133 *
134 * @param original an int value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700135 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700136 */
137 public static ImmutableByteSequence copyFrom(int original) {
138 return new ImmutableByteSequence(
139 ByteBuffer.allocate(Integer.BYTES).putInt(original));
140 }
141
142 /**
143 * Creates a new byte sequence of 2 bytes containing the given short value.
144 *
145 * @param original a short value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700146 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700147 */
148 public static ImmutableByteSequence copyFrom(short original) {
149 return new ImmutableByteSequence(
150 ByteBuffer.allocate(Short.BYTES).putShort(original));
151 }
152
153 /**
154 * Creates a new byte sequence of 1 byte containing the given value.
155 *
156 * @param original a byte value
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700157 * @return a new immutable byte sequence
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700158 */
159 public static ImmutableByteSequence copyFrom(byte original) {
160 return new ImmutableByteSequence(
161 ByteBuffer.allocate(Byte.BYTES).put(original));
162 }
163
164 /**
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700165 * Creates a new byte sequence of the given size where alla bits are 0.
166 *
167 * @param size number of bytes
168 * @return a new immutable byte sequence
169 */
170 public static ImmutableByteSequence ofZeros(int size) {
171 byte[] bytes = new byte[size];
172 Arrays.fill(bytes, (byte) 0);
173 return new ImmutableByteSequence(ByteBuffer.wrap(bytes));
174 }
175
176 /**
177 * Creates a new byte sequence of the given size where alla bits are 1.
178 *
179 * @param size number of bytes
180 * @return a new immutable byte sequence
181 */
182 public static ImmutableByteSequence ofOnes(int size) {
183 byte[] bytes = new byte[size];
184 Arrays.fill(bytes, (byte) 0xFF);
185 return new ImmutableByteSequence(ByteBuffer.wrap(bytes));
186 }
187
188 /**
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700189 * Returns a view of this sequence as a read-only {@link ByteBuffer}.
190 * <p>
191 * The returned buffer will have position 0, while limit and capacity will
192 * be set to this sequence {@link #size()}. The buffer order will be
193 * big-endian.
194 *
195 * @return a read-only byte buffer
196 */
197 public ByteBuffer asReadOnlyBuffer() {
198 // position, limit and capacity set rewind at constructor
199 return value.asReadOnlyBuffer();
200 }
201
202 /**
203 * Gets the number of bytes in this sequence.
204 *
205 * @return an integer value
206 */
207 public int size() {
208 return this.value.capacity();
209 }
210
211 /**
212 * Creates a new byte array view of this sequence.
213 *
214 * @return a new byte array
215 */
216 public byte[] asArray() {
217 ByteBuffer bb = asReadOnlyBuffer();
218 byte[] bytes = new byte[size()];
219 bb.get(bytes);
220 return bytes;
221 }
222
223 @Override
224 public int hashCode() {
225 return value.hashCode();
226 }
227
228 @Override
229 public boolean equals(Object obj) {
230 if (this == obj) {
231 return true;
232 }
233 if (obj == null || getClass() != obj.getClass()) {
234 return false;
235 }
236 final ImmutableByteSequence other = (ImmutableByteSequence) obj;
237 return Objects.equal(this.value, other.value);
238 }
239
240 @Override
241 public String toString() {
Carmelo Cascone804c0012016-06-23 13:05:40 -0700242 return HexString.toHexString(value.array());
Carmelo Cascone6b32c992016-04-13 11:53:09 -0700243 }
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700244}