001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.primitives; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkElementIndex; 019import static com.google.common.base.Preconditions.checkNotNull; 020import static com.google.common.base.Preconditions.checkPositionIndexes; 021 022import com.google.common.annotations.GwtCompatible; 023import java.io.Serializable; 024import java.util.AbstractList; 025import java.util.Arrays; 026import java.util.Collection; 027import java.util.Collections; 028import java.util.List; 029import java.util.RandomAccess; 030import org.jspecify.annotations.Nullable; 031 032/** 033 * Static utility methods pertaining to {@code byte} primitives, that are not already found in 034 * either {@link Byte} or {@link Arrays}, <i>and interpret bytes as neither signed nor unsigned</i>. 035 * The methods which specifically treat bytes as signed or unsigned are found in {@link SignedBytes} 036 * and {@link UnsignedBytes}. 037 * 038 * <p>See the Guava User Guide article on <a 039 * href="https://github.com/google/guava/wiki/PrimitivesExplained">primitive utilities</a>. 040 * 041 * @author Kevin Bourrillion 042 * @since 1.0 043 */ 044// TODO(kevinb): how to prevent warning on UnsignedBytes when building GWT 045// javadoc? 046@GwtCompatible 047public final class Bytes { 048 private Bytes() {} 049 050 /** 051 * Returns a hash code for {@code value}; equal to the result of invoking {@code ((Byte) 052 * value).hashCode()}. 053 * 054 * <p><b>Java 8+ users:</b> use {@link Byte#hashCode(byte)} instead. 055 * 056 * @param value a primitive {@code byte} value 057 * @return a hash code for the value 058 */ 059 public static int hashCode(byte value) { 060 return value; 061 } 062 063 /** 064 * Returns {@code true} if {@code target} is present as an element anywhere in {@code array}. 065 * 066 * @param array an array of {@code byte} values, possibly empty 067 * @param target a primitive {@code byte} value 068 * @return {@code true} if {@code array[i] == target} for some value of {@code i} 069 */ 070 public static boolean contains(byte[] array, byte target) { 071 for (byte value : array) { 072 if (value == target) { 073 return true; 074 } 075 } 076 return false; 077 } 078 079 /** 080 * Returns the index of the first appearance of the value {@code target} in {@code array}. 081 * 082 * @param array an array of {@code byte} values, possibly empty 083 * @param target a primitive {@code byte} value 084 * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no 085 * such index exists. 086 */ 087 public static int indexOf(byte[] array, byte target) { 088 return indexOf(array, target, 0, array.length); 089 } 090 091 // TODO(kevinb): consider making this public 092 private static int indexOf(byte[] array, byte target, int start, int end) { 093 for (int i = start; i < end; i++) { 094 if (array[i] == target) { 095 return i; 096 } 097 } 098 return -1; 099 } 100 101 /** 102 * Returns the start position of the first occurrence of the specified {@code target} within 103 * {@code array}, or {@code -1} if there is no such occurrence. 104 * 105 * <p>More formally, returns the lowest index {@code i} such that {@code Arrays.copyOfRange(array, 106 * i, i + target.length)} contains exactly the same elements as {@code target}. 107 * 108 * @param array the array to search for the sequence {@code target} 109 * @param target the array to search for as a sub-sequence of {@code array} 110 */ 111 public static int indexOf(byte[] array, byte[] target) { 112 checkNotNull(array, "array"); 113 checkNotNull(target, "target"); 114 if (target.length == 0) { 115 return 0; 116 } 117 118 outer: 119 for (int i = 0; i < array.length - target.length + 1; i++) { 120 for (int j = 0; j < target.length; j++) { 121 if (array[i + j] != target[j]) { 122 continue outer; 123 } 124 } 125 return i; 126 } 127 return -1; 128 } 129 130 /** 131 * Returns the index of the last appearance of the value {@code target} in {@code array}. 132 * 133 * @param array an array of {@code byte} values, possibly empty 134 * @param target a primitive {@code byte} value 135 * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no 136 * such index exists. 137 */ 138 public static int lastIndexOf(byte[] array, byte target) { 139 return lastIndexOf(array, target, 0, array.length); 140 } 141 142 // TODO(kevinb): consider making this public 143 private static int lastIndexOf(byte[] array, byte target, int start, int end) { 144 for (int i = end - 1; i >= start; i--) { 145 if (array[i] == target) { 146 return i; 147 } 148 } 149 return -1; 150 } 151 152 /** 153 * Returns the values from each provided array combined into a single array. For example, {@code 154 * concat(new byte[] {a, b}, new byte[] {}, new byte[] {c}} returns the array {@code {a, b, c}}. 155 * 156 * @param arrays zero or more {@code byte} arrays 157 * @return a single array containing all the values from the source arrays, in order 158 * @throws IllegalArgumentException if the total number of elements in {@code arrays} does not fit 159 * in an {@code int} 160 */ 161 public static byte[] concat(byte[]... arrays) { 162 long length = 0; 163 for (byte[] array : arrays) { 164 length += array.length; 165 } 166 byte[] result = new byte[checkNoOverflow(length)]; 167 int pos = 0; 168 for (byte[] array : arrays) { 169 System.arraycopy(array, 0, result, pos, array.length); 170 pos += array.length; 171 } 172 return result; 173 } 174 175 private static int checkNoOverflow(long result) { 176 checkArgument( 177 result == (int) result, 178 "the total number of elements (%s) in the arrays must fit in an int", 179 result); 180 return (int) result; 181 } 182 183 /** 184 * Returns an array containing the same values as {@code array}, but guaranteed to be of a 185 * specified minimum length. If {@code array} already has a length of at least {@code minLength}, 186 * it is returned directly. Otherwise, a new array of size {@code minLength + padding} is 187 * returned, containing the values of {@code array}, and zeroes in the remaining places. 188 * 189 * @param array the source array 190 * @param minLength the minimum length the returned array must guarantee 191 * @param padding an extra amount to "grow" the array by if growth is necessary 192 * @throws IllegalArgumentException if {@code minLength} or {@code padding} is negative 193 * @return an array containing the values of {@code array}, with guaranteed minimum length {@code 194 * minLength} 195 */ 196 public static byte[] ensureCapacity(byte[] array, int minLength, int padding) { 197 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); 198 checkArgument(padding >= 0, "Invalid padding: %s", padding); 199 return (array.length < minLength) ? Arrays.copyOf(array, minLength + padding) : array; 200 } 201 202 /** 203 * Returns an array containing each value of {@code collection}, converted to a {@code byte} value 204 * in the manner of {@link Number#byteValue}. 205 * 206 * <p>Elements are copied from the argument collection as if by {@code collection.toArray()}. 207 * Calling this method is as thread-safe as calling that method. 208 * 209 * @param collection a collection of {@code Number} instances 210 * @return an array containing the same values as {@code collection}, in the same order, converted 211 * to primitives 212 * @throws NullPointerException if {@code collection} or any of its elements is null 213 * @since 1.0 (parameter was {@code Collection<Byte>} before 12.0) 214 */ 215 public static byte[] toArray(Collection<? extends Number> collection) { 216 if (collection instanceof ByteArrayAsList) { 217 return ((ByteArrayAsList) collection).toByteArray(); 218 } 219 220 Object[] boxedArray = collection.toArray(); 221 int len = boxedArray.length; 222 byte[] array = new byte[len]; 223 for (int i = 0; i < len; i++) { 224 // checkNotNull for GWT (do not optimize) 225 array[i] = ((Number) checkNotNull(boxedArray[i])).byteValue(); 226 } 227 return array; 228 } 229 230 /** 231 * Returns a fixed-size list backed by the specified array, similar to {@link 232 * Arrays#asList(Object[])}. The list supports {@link List#set(int, Object)}, but any attempt to 233 * set a value to {@code null} will result in a {@link NullPointerException}. 234 * 235 * <p>The returned list maintains the values, but not the identities, of {@code Byte} objects 236 * written to or read from it. For example, whether {@code list.get(0) == list.get(0)} is true for 237 * the returned list is unspecified. 238 * 239 * <p>The returned list is serializable. 240 * 241 * @param backingArray the array to back the list 242 * @return a list view of the array 243 */ 244 public static List<Byte> asList(byte... backingArray) { 245 if (backingArray.length == 0) { 246 return Collections.emptyList(); 247 } 248 return new ByteArrayAsList(backingArray); 249 } 250 251 @GwtCompatible 252 private static class ByteArrayAsList extends AbstractList<Byte> 253 implements RandomAccess, Serializable { 254 final byte[] array; 255 final int start; 256 final int end; 257 258 ByteArrayAsList(byte[] array) { 259 this(array, 0, array.length); 260 } 261 262 ByteArrayAsList(byte[] array, int start, int end) { 263 this.array = array; 264 this.start = start; 265 this.end = end; 266 } 267 268 @Override 269 public int size() { 270 return end - start; 271 } 272 273 @Override 274 public boolean isEmpty() { 275 return false; 276 } 277 278 @Override 279 public Byte get(int index) { 280 checkElementIndex(index, size()); 281 return array[start + index]; 282 } 283 284 @Override 285 public boolean contains(@Nullable Object target) { 286 // Overridden to prevent a ton of boxing 287 return (target instanceof Byte) && Bytes.indexOf(array, (Byte) target, start, end) != -1; 288 } 289 290 @Override 291 public int indexOf(@Nullable Object target) { 292 // Overridden to prevent a ton of boxing 293 if (target instanceof Byte) { 294 int i = Bytes.indexOf(array, (Byte) target, start, end); 295 if (i >= 0) { 296 return i - start; 297 } 298 } 299 return -1; 300 } 301 302 @Override 303 public int lastIndexOf(@Nullable Object target) { 304 // Overridden to prevent a ton of boxing 305 if (target instanceof Byte) { 306 int i = Bytes.lastIndexOf(array, (Byte) target, start, end); 307 if (i >= 0) { 308 return i - start; 309 } 310 } 311 return -1; 312 } 313 314 @Override 315 public Byte set(int index, Byte element) { 316 checkElementIndex(index, size()); 317 byte oldValue = array[start + index]; 318 // checkNotNull for GWT (do not optimize) 319 array[start + index] = checkNotNull(element); 320 return oldValue; 321 } 322 323 @Override 324 public List<Byte> subList(int fromIndex, int toIndex) { 325 int size = size(); 326 checkPositionIndexes(fromIndex, toIndex, size); 327 if (fromIndex == toIndex) { 328 return Collections.emptyList(); 329 } 330 return new ByteArrayAsList(array, start + fromIndex, start + toIndex); 331 } 332 333 @Override 334 public boolean equals(@Nullable Object object) { 335 if (object == this) { 336 return true; 337 } 338 if (object instanceof ByteArrayAsList) { 339 ByteArrayAsList that = (ByteArrayAsList) object; 340 int size = size(); 341 if (that.size() != size) { 342 return false; 343 } 344 for (int i = 0; i < size; i++) { 345 if (array[start + i] != that.array[that.start + i]) { 346 return false; 347 } 348 } 349 return true; 350 } 351 return super.equals(object); 352 } 353 354 @Override 355 public int hashCode() { 356 int result = 1; 357 for (int i = start; i < end; i++) { 358 result = 31 * result + Bytes.hashCode(array[i]); 359 } 360 return result; 361 } 362 363 @Override 364 public String toString() { 365 StringBuilder builder = new StringBuilder(size() * 5); 366 builder.append('[').append(array[start]); 367 for (int i = start + 1; i < end; i++) { 368 builder.append(", ").append(array[i]); 369 } 370 return builder.append(']').toString(); 371 } 372 373 byte[] toByteArray() { 374 return Arrays.copyOfRange(array, start, end); 375 } 376 377 private static final long serialVersionUID = 0; 378 } 379 380 /** 381 * Reverses the elements of {@code array}. This is equivalent to {@code 382 * Collections.reverse(Bytes.asList(array))}, but is likely to be more efficient. 383 * 384 * @since 23.1 385 */ 386 public static void reverse(byte[] array) { 387 checkNotNull(array); 388 reverse(array, 0, array.length); 389 } 390 391 /** 392 * Reverses the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} 393 * exclusive. This is equivalent to {@code 394 * Collections.reverse(Bytes.asList(array).subList(fromIndex, toIndex))}, but is likely to be more 395 * efficient. 396 * 397 * @throws IndexOutOfBoundsException if {@code fromIndex < 0}, {@code toIndex > array.length}, or 398 * {@code toIndex > fromIndex} 399 * @since 23.1 400 */ 401 public static void reverse(byte[] array, int fromIndex, int toIndex) { 402 checkNotNull(array); 403 checkPositionIndexes(fromIndex, toIndex, array.length); 404 for (int i = fromIndex, j = toIndex - 1; i < j; i++, j--) { 405 byte tmp = array[i]; 406 array[i] = array[j]; 407 array[j] = tmp; 408 } 409 } 410 411 /** 412 * Performs a right rotation of {@code array} of "distance" places, so that the first element is 413 * moved to index "distance", and the element at index {@code i} ends up at index {@code (distance 414 * + i) mod array.length}. This is equivalent to {@code Collections.rotate(Bytes.asList(array), 415 * distance)}, but is somewhat faster. 416 * 417 * <p>The provided "distance" may be negative, which will rotate left. 418 * 419 * @since 32.0.0 420 */ 421 public static void rotate(byte[] array, int distance) { 422 rotate(array, distance, 0, array.length); 423 } 424 425 /** 426 * Performs a right rotation of {@code array} between {@code fromIndex} inclusive and {@code 427 * toIndex} exclusive. This is equivalent to {@code 428 * Collections.rotate(Bytes.asList(array).subList(fromIndex, toIndex), distance)}, but is somewhat 429 * faster. 430 * 431 * <p>The provided "distance" may be negative, which will rotate left. 432 * 433 * @throws IndexOutOfBoundsException if {@code fromIndex < 0}, {@code toIndex > array.length}, or 434 * {@code toIndex > fromIndex} 435 * @since 32.0.0 436 */ 437 public static void rotate(byte[] array, int distance, int fromIndex, int toIndex) { 438 // See Ints.rotate for more details about possible algorithms here. 439 checkNotNull(array); 440 checkPositionIndexes(fromIndex, toIndex, array.length); 441 if (array.length <= 1) { 442 return; 443 } 444 445 int length = toIndex - fromIndex; 446 // Obtain m = (-distance mod length), a non-negative value less than "length". This is how many 447 // places left to rotate. 448 int m = -distance % length; 449 m = (m < 0) ? m + length : m; 450 // The current index of what will become the first element of the rotated section. 451 int newFirstIndex = m + fromIndex; 452 if (newFirstIndex == fromIndex) { 453 return; 454 } 455 456 reverse(array, fromIndex, newFirstIndex); 457 reverse(array, newFirstIndex, toIndex); 458 reverse(array, fromIndex, toIndex); 459 } 460}