2 * Copyright 2011 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.google.ipc.invalidation.util;
18 import com.google.ipc.invalidation.util.LazyString.LazyStringReceiver;
20 import java.nio.ByteBuffer;
21 import java.nio.charset.Charset;
22 import java.util.Arrays;
23 import java.util.Locale;
27 * A class that encapsulates a (fixed size) sequence of bytes and provides a
28 * equality (along with hashcode) method that considers two sequences to be
29 * equal if they have the same contents. Borrowed from protobuf's ByteString
32 public class Bytes extends InternalBase implements Comparable<Bytes> {
34 public static final Bytes EMPTY_BYTES = new Bytes(new byte[0]);
35 private static final Charset UTF_8 = Charset.forName("UTF-8");
38 * Interface accessing byte elements from {@code T}, which may be (for instance)
39 * {@link com.google.protobuf.ByteString ByteString} or {@code byte[]}.
41 interface BytesAccessor<T> {
43 byte get(T bytes, int index);
46 private static final BytesAccessor<byte[]> BYTE_ARRAY_ACCESSOR = new BytesAccessor<byte[]>() {
47 @Override public int size(byte[] bytes) {
48 return bytes == null ? 0 : bytes.length;
51 @Override public byte get(byte[] bytes, int index) {
56 private static final LazyStringReceiver<byte[]> BYTE_ARRAY_RECEIVER =
57 new LazyStringReceiver<byte[]>() {
58 @Override public void appendToBuilder(TextBuilder builder, byte[] element) {
59 toCompactString(builder, element);
64 * Three arrays that store the representation of each character from 0 to 255.
65 * The ith number's octal representation is: CHAR_OCTAL_STRINGS1[i],
66 * CHAR_OCTAL_STRINGS2[i], CHAR_OCTAL_STRINGS3[i]
68 * E.g., if the number 128, these arrays contain 2, 0, 0 at index 128. We use
69 * 3 char arrays instead of an array of strings since the code path for a
70 * character append operation is quite a bit shorter than the append operation
73 private static final char[] CHAR_OCTAL_STRINGS1 = new char[256];
74 private static final char[] CHAR_OCTAL_STRINGS2 = new char[256];
75 private static final char[] CHAR_OCTAL_STRINGS3 = new char[256];
77 /** The actual sequence. */
78 private final byte[] bytes;
81 private volatile int hash = 0;
84 // Initialize the array with the Octal string values so that we do not have
85 // to do String.format for every byte during runtime.
86 for (int i = 0; i < CHAR_OCTAL_STRINGS1.length; i++) {
87 String value = String.format(Locale.ROOT, "\\%03o", i);
88 CHAR_OCTAL_STRINGS1[i] = value.charAt(1);
89 CHAR_OCTAL_STRINGS2[i] = value.charAt(2);
90 CHAR_OCTAL_STRINGS3[i] = value.charAt(3);
94 public Bytes(byte[] bytes) {
99 * Creates a Bytes object with the contents of {@code array1} followed by the
100 * contents of {@code array2}.
102 public Bytes(byte[] array1, byte[] array2) {
103 Preconditions.checkNotNull(array1);
104 Preconditions.checkNotNull(array2);
105 ByteBuffer buffer = ByteBuffer.allocate(array1.length + array2.length);
108 this.bytes = buffer.array();
112 * Creates a Bytes object with the contents of {@code b1} followed by the
113 * contents of {@code b2}.
115 public Bytes(Bytes b1, Bytes b2) {
116 this(b1.bytes, b2.bytes);
119 public Bytes(byte b) {
120 this.bytes = new byte[1];
124 /** Creates a Bytes object from the given string encoded as a UTF-8 byte array. */
125 public static Bytes fromUtf8Encoding(String s) {
126 return new Bytes(s.getBytes(UTF_8));
130 * Gets the byte at the given index.
132 * @throws ArrayIndexOutOfBoundsException {@code index} is < 0 or >= size
134 public byte byteAt(final int index) {
139 * Gets the number of bytes.
146 * Returns the internal byte array.
148 public byte[] getByteArray() {
153 * Returns a new {@code Bytes} containing the given subrange of bytes [{@code from}, {@code to}).
155 public Bytes subsequence(int from, int to) {
156 // Identical semantics to Arrays.copyOfRange() but implemented manually so runs on
158 int newLength = to - from;
160 throw new IllegalArgumentException(from + " > " + to);
162 byte[] copy = new byte[newLength];
163 System.arraycopy(bytes, from, copy, 0, Math.min(bytes.length - from, newLength));
164 return new Bytes(copy);
167 @Override public boolean equals(final Object o) {
172 if (!(o instanceof Bytes)) {
176 final Bytes other = (Bytes) o;
177 return Arrays.equals(bytes, other.bytes);
180 @Override public int hashCode() {
183 // If the hash has been not computed, go through each byte and compute it.
185 final byte[] thisBytes = bytes;
186 final int size = bytes.length;
189 for (int i = 0; i < size; i++) {
190 h = h * 31 + thisBytes[i];
203 * Returns whether these bytes are a prefix (either proper or improper) of
206 public boolean isPrefixOf(Bytes other) {
207 Preconditions.checkNotNull(other);
208 if (size() > other.size()) {
211 for (int i = 0; i < size(); ++i) {
212 if (bytes[i] != other.bytes[i]) {
220 * Returns whether these bytes are a suffix (either proper or improper) of
223 public boolean isSuffixOf(Bytes other) {
224 Preconditions.checkNotNull(other);
225 int diff = other.size() - size();
229 for (int i = 0; i < size(); ++i) {
230 if (bytes[i] != other.bytes[i + diff]) {
237 @Override public int compareTo(Bytes other) {
238 return compare(bytes, other.bytes);
241 public static Bytes fromByteArray(byte[] bytes) {
242 return (bytes == null) ? null : new Bytes(bytes);
246 * Same specs as {@link #compareTo} except for the byte[] type. Null arrays are ordered before
249 public static int compare(byte[] first, byte[] second) {
250 return compare(BYTE_ARRAY_ACCESSOR, first, second);
254 * Performs lexicographic comparison of two byte sequences. Null sequences are ordered before
255 * non-null sequences.
257 static <T> int compare(BytesAccessor<T> accessor, T first, T second) {
258 // Order null arrays before non-null arrays.
260 return (second == null) ? 0 : -1;
262 if (second == null) {
266 int minLength = Math.min(accessor.size(first), accessor.size(second));
267 for (int i = 0; i < minLength; i++) {
269 if (accessor.get(first, i) != accessor.get(second, i)) {
270 int firstByte = accessor.get(first, i) & 0xff;
271 int secondByte = accessor.get(second, i) & 0xff;
272 return firstByte - secondByte;
275 // At this point, either both arrays are equal length or one of the arrays has ended.
276 // * If the arrays are of equal length, they must be identical (else we would have
277 // returned the correct value above
278 // * If they are not of equal length, the one with the longer length is greater.
279 return accessor.size(first) - accessor.size(second);
283 * Renders the bytes as a string in standard bigtable ascii / octal mix compatible with bt and
286 public static String toString(byte[] bytes) {
287 return toCompactString(new TextBuilder(), bytes).toString();
291 * Renders the bytes as a string in standard bigtable ascii / octal mix compatible with bt and
292 * adds it to builder.
294 @Override public void toCompactString(TextBuilder builder) {
295 toCompactString(builder, bytes);
299 * Renders the bytes as a string in standard bigtable ascii / octal mix compatible with bt and
300 * adds it to builder. Returns {@code builder}.
302 public static TextBuilder toCompactString(TextBuilder builder, byte[] bytes) {
303 return toCompactString(BYTE_ARRAY_ACCESSOR, builder, bytes);
307 * Returns an object that lazily formats {@code bytes} when {@link Object#toString()} is called.
309 public static Object toLazyCompactString(byte[] bytes) {
310 if (bytes == null || bytes.length == 0) {
313 return LazyString.toLazyCompactString(bytes, BYTE_ARRAY_RECEIVER);
317 * Renders the bytes as a string in standard bigtable ascii / octal mix compatible with bt and
318 * adds it to builder. Borrowed from Bigtable's {@code Util$keyToString()}.
319 * Returns {@code builder}.
321 static <T> TextBuilder toCompactString(BytesAccessor<T> accessor, TextBuilder builder,
323 for (int i = 0; i < accessor.size(bytes); i++) {
324 byte c = accessor.get(bytes, i);
326 case '\n': builder.append('\\'); builder.append('n'); break;
327 case '\r': builder.append('\\'); builder.append('r'); break;
328 case '\t': builder.append('\\'); builder.append('t'); break;
329 case '\"': builder.append('\\'); builder.append('"'); break;
330 case '\\': builder.append('\\'); builder.append('\\'); break;
332 if ((c >= 32) && (c < 127) && c != '\'') {
333 builder.append((char) c);
339 builder.append('\\');
340 builder.append(CHAR_OCTAL_STRINGS1[byteValue]);
341 builder.append(CHAR_OCTAL_STRINGS2[byteValue]);
342 builder.append(CHAR_OCTAL_STRINGS3[byteValue]);