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.checkerframework.checker.nullness.compatqual.NullableDecl; 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 */ 159 public static byte[] concat(byte[]... arrays) { 160 int length = 0; 161 for (byte[] array : arrays) { 162 length += array.length; 163 } 164 byte[] result = new byte[length]; 165 int pos = 0; 166 for (byte[] array : arrays) { 167 System.arraycopy(array, 0, result, pos, array.length); 168 pos += array.length; 169 } 170 return result; 171 } 172 173 /** 174 * Returns an array containing the same values as {@code array}, but guaranteed to be of a 175 * specified minimum length. If {@code array} already has a length of at least {@code minLength}, 176 * it is returned directly. Otherwise, a new array of size {@code minLength + padding} is 177 * returned, containing the values of {@code array}, and zeroes in the remaining places. 178 * 179 * @param array the source array 180 * @param minLength the minimum length the returned array must guarantee 181 * @param padding an extra amount to "grow" the array by if growth is necessary 182 * @throws IllegalArgumentException if {@code minLength} or {@code padding} is negative 183 * @return an array containing the values of {@code array}, with guaranteed minimum length {@code 184 * minLength} 185 */ 186 public static byte[] ensureCapacity(byte[] array, int minLength, int padding) { 187 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); 188 checkArgument(padding >= 0, "Invalid padding: %s", padding); 189 return (array.length < minLength) ? Arrays.copyOf(array, minLength + padding) : array; 190 } 191 192 /** 193 * Returns an array containing each value of {@code collection}, converted to a {@code byte} value 194 * in the manner of {@link Number#byteValue}. 195 * 196 * <p>Elements are copied from the argument collection as if by {@code collection.toArray()}. 197 * Calling this method is as thread-safe as calling that method. 198 * 199 * @param collection a collection of {@code Number} instances 200 * @return an array containing the same values as {@code collection}, in the same order, converted 201 * to primitives 202 * @throws NullPointerException if {@code collection} or any of its elements is null 203 * @since 1.0 (parameter was {@code Collection<Byte>} before 12.0) 204 */ 205 public static byte[] toArray(Collection<? extends Number> collection) { 206 if (collection instanceof ByteArrayAsList) { 207 return ((ByteArrayAsList) collection).toByteArray(); 208 } 209 210 Object[] boxedArray = collection.toArray(); 211 int len = boxedArray.length; 212 byte[] array = new byte[len]; 213 for (int i = 0; i < len; i++) { 214 // checkNotNull for GWT (do not optimize) 215 array[i] = ((Number) checkNotNull(boxedArray[i])).byteValue(); 216 } 217 return array; 218 } 219 220 /** 221 * Returns a fixed-size list backed by the specified array, similar to {@link 222 * Arrays#asList(Object[])}. The list supports {@link List#set(int, Object)}, but any attempt to 223 * set a value to {@code null} will result in a {@link NullPointerException}. 224 * 225 * <p>The returned list maintains the values, but not the identities, of {@code Byte} objects 226 * written to or read from it. For example, whether {@code list.get(0) == list.get(0)} is true for 227 * the returned list is unspecified. 228 * 229 * @param backingArray the array to back the list 230 * @return a list view of the array 231 */ 232 public static List<Byte> asList(byte... backingArray) { 233 if (backingArray.length == 0) { 234 return Collections.emptyList(); 235 } 236 return new ByteArrayAsList(backingArray); 237 } 238 239 @GwtCompatible 240 private static class ByteArrayAsList extends AbstractList<Byte> 241 implements RandomAccess, Serializable { 242 final byte[] array; 243 final int start; 244 final int end; 245 246 ByteArrayAsList(byte[] array) { 247 this(array, 0, array.length); 248 } 249 250 ByteArrayAsList(byte[] array, int start, int end) { 251 this.array = array; 252 this.start = start; 253 this.end = end; 254 } 255 256 @Override 257 public int size() { 258 return end - start; 259 } 260 261 @Override 262 public boolean isEmpty() { 263 return false; 264 } 265 266 @Override 267 public Byte get(int index) { 268 checkElementIndex(index, size()); 269 return array[start + index]; 270 } 271 272 @Override 273 public boolean contains(Object target) { 274 // Overridden to prevent a ton of boxing 275 return (target instanceof Byte) && Bytes.indexOf(array, (Byte) target, start, end) != -1; 276 } 277 278 @Override 279 public int indexOf(Object target) { 280 // Overridden to prevent a ton of boxing 281 if (target instanceof Byte) { 282 int i = Bytes.indexOf(array, (Byte) target, start, end); 283 if (i >= 0) { 284 return i - start; 285 } 286 } 287 return -1; 288 } 289 290 @Override 291 public int lastIndexOf(Object target) { 292 // Overridden to prevent a ton of boxing 293 if (target instanceof Byte) { 294 int i = Bytes.lastIndexOf(array, (Byte) target, start, end); 295 if (i >= 0) { 296 return i - start; 297 } 298 } 299 return -1; 300 } 301 302 @Override 303 public Byte set(int index, Byte element) { 304 checkElementIndex(index, size()); 305 byte oldValue = array[start + index]; 306 // checkNotNull for GWT (do not optimize) 307 array[start + index] = checkNotNull(element); 308 return oldValue; 309 } 310 311 @Override 312 public List<Byte> subList(int fromIndex, int toIndex) { 313 int size = size(); 314 checkPositionIndexes(fromIndex, toIndex, size); 315 if (fromIndex == toIndex) { 316 return Collections.emptyList(); 317 } 318 return new ByteArrayAsList(array, start + fromIndex, start + toIndex); 319 } 320 321 @Override 322 public boolean equals(@NullableDecl Object object) { 323 if (object == this) { 324 return true; 325 } 326 if (object instanceof ByteArrayAsList) { 327 ByteArrayAsList that = (ByteArrayAsList) object; 328 int size = size(); 329 if (that.size() != size) { 330 return false; 331 } 332 for (int i = 0; i < size; i++) { 333 if (array[start + i] != that.array[that.start + i]) { 334 return false; 335 } 336 } 337 return true; 338 } 339 return super.equals(object); 340 } 341 342 @Override 343 public int hashCode() { 344 int result = 1; 345 for (int i = start; i < end; i++) { 346 result = 31 * result + Bytes.hashCode(array[i]); 347 } 348 return result; 349 } 350 351 @Override 352 public String toString() { 353 StringBuilder builder = new StringBuilder(size() * 5); 354 builder.append('[').append(array[start]); 355 for (int i = start + 1; i < end; i++) { 356 builder.append(", ").append(array[i]); 357 } 358 return builder.append(']').toString(); 359 } 360 361 byte[] toByteArray() { 362 return Arrays.copyOfRange(array, start, end); 363 } 364 365 private static final long serialVersionUID = 0; 366 } 367 368 /** 369 * Reverses the elements of {@code array}. This is equivalent to {@code 370 * Collections.reverse(Bytes.asList(array))}, but is likely to be more efficient. 371 * 372 * @since 23.1 373 */ 374 public static void reverse(byte[] array) { 375 checkNotNull(array); 376 reverse(array, 0, array.length); 377 } 378 379 /** 380 * Reverses the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} 381 * exclusive. This is equivalent to {@code 382 * Collections.reverse(Bytes.asList(array).subList(fromIndex, toIndex))}, but is likely to be more 383 * efficient. 384 * 385 * @throws IndexOutOfBoundsException if {@code fromIndex < 0}, {@code toIndex > array.length}, or 386 * {@code toIndex > fromIndex} 387 * @since 23.1 388 */ 389 public static void reverse(byte[] array, int fromIndex, int toIndex) { 390 checkNotNull(array); 391 checkPositionIndexes(fromIndex, toIndex, array.length); 392 for (int i = fromIndex, j = toIndex - 1; i < j; i++, j--) { 393 byte tmp = array[i]; 394 array[i] = array[j]; 395 array[j] = tmp; 396 } 397 } 398}