1 /* 2 * Copyright 2013 Google Inc. 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 17 package com.google.common.jimfs; 18 19 import static com.google.common.base.Preconditions.checkArgument; 20 import static com.google.common.base.Preconditions.checkNotNull; 21 22 import com.google.common.collect.ImmutableCollection; 23 24 /** 25 * Miscellaneous static utility methods. 26 * 27 * @author Colin Decker 28 * @author Austin Appleby 29 */ 30 final class Util { 31 Util()32 private Util() {} 33 34 /** Returns the next power of 2 >= n. */ nextPowerOf2(int n)35 public static int nextPowerOf2(int n) { 36 if (n == 0) { 37 return 1; 38 } 39 int b = Integer.highestOneBit(n); 40 return b == n ? n : b << 1; 41 } 42 43 /** 44 * Checks that the given number is not negative, throwing IAE if it is. The given description 45 * describes the number in the exception message. 46 */ checkNotNegative(long n, String description)47 static void checkNotNegative(long n, String description) { 48 checkArgument(n >= 0, "%s must not be negative: %s", description, n); 49 } 50 51 /** Checks that no element in the given iterable is null, throwing NPE if any is. */ checkNoneNull(Iterable<?> objects)52 static void checkNoneNull(Iterable<?> objects) { 53 if (!(objects instanceof ImmutableCollection)) { 54 for (Object o : objects) { 55 checkNotNull(o); 56 } 57 } 58 } 59 60 private static final int C1 = 0xcc9e2d51; 61 private static final int C2 = 0x1b873593; 62 63 /* 64 * This method was rewritten in Java from an intermediate step of the Murmur hash function in 65 * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp, which contained the 66 * following header: 67 * 68 * MurmurHash3 was written by Austin Appleby, and is placed in the public domain. The author 69 * hereby disclaims copyright to this source code. 70 */ smearHash(int hashCode)71 static int smearHash(int hashCode) { 72 return C2 * Integer.rotateLeft(hashCode * C1, 15); 73 } 74 75 private static final int ARRAY_LEN = 8192; 76 private static final byte[] ZERO_ARRAY = new byte[ARRAY_LEN]; 77 private static final byte[][] NULL_ARRAY = new byte[ARRAY_LEN][]; 78 79 /** Zeroes all bytes between off (inclusive) and off + len (exclusive) in the given array. */ zero(byte[] bytes, int off, int len)80 static void zero(byte[] bytes, int off, int len) { 81 // this is significantly faster than looping or Arrays.fill (which loops), particularly when 82 // the length of the slice to be zeroed is <= to ARRAY_LEN (in that case, it's faster by a 83 // factor of 2) 84 int remaining = len; 85 while (remaining > ARRAY_LEN) { 86 System.arraycopy(ZERO_ARRAY, 0, bytes, off, ARRAY_LEN); 87 off += ARRAY_LEN; 88 remaining -= ARRAY_LEN; 89 } 90 91 System.arraycopy(ZERO_ARRAY, 0, bytes, off, remaining); 92 } 93 94 /** 95 * Clears (sets to null) all blocks between off (inclusive) and off + len (exclusive) in the given 96 * array. 97 */ clear(byte[][] blocks, int off, int len)98 static void clear(byte[][] blocks, int off, int len) { 99 // this is significantly faster than looping or Arrays.fill (which loops), particularly when 100 // the length of the slice to be cleared is <= to ARRAY_LEN (in that case, it's faster by a 101 // factor of 2) 102 int remaining = len; 103 while (remaining > ARRAY_LEN) { 104 System.arraycopy(NULL_ARRAY, 0, blocks, off, ARRAY_LEN); 105 off += ARRAY_LEN; 106 remaining -= ARRAY_LEN; 107 } 108 109 System.arraycopy(NULL_ARRAY, 0, blocks, off, remaining); 110 } 111 } 112