diff options
author | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-03-05 05:12:32 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-03-05 05:12:32 +0000 |
commit | 6f35239027aa2efad5fbbfe41ecad0923003112b (patch) | |
tree | f8cbc802a806455c5fdfeab7f696c63641b9f9f5 /jimfs/src/main/java/com/google/common/jimfs/Util.java | |
parent | 68591711a9034281d5fe11fc7a30e535bbce125c (diff) | |
parent | 089373269bfaa2212f2008ac7bda148cdd6945c9 (diff) | |
download | jimfs-6f35239027aa2efad5fbbfe41ecad0923003112b.tar.gz |
Initial merge with upstream am: cef92d673c am: c5f71e95df am: e50b3b7561 am: c47b5481de am: 089373269b
Change-Id: I00caf048ac2f81928690feb6ce0c26274339e84a
Diffstat (limited to 'jimfs/src/main/java/com/google/common/jimfs/Util.java')
-rw-r--r-- | jimfs/src/main/java/com/google/common/jimfs/Util.java | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/jimfs/src/main/java/com/google/common/jimfs/Util.java b/jimfs/src/main/java/com/google/common/jimfs/Util.java new file mode 100644 index 0000000..3d1ec5c --- /dev/null +++ b/jimfs/src/main/java/com/google/common/jimfs/Util.java @@ -0,0 +1,111 @@ +/* + * Copyright 2013 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.common.jimfs; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.collect.ImmutableCollection; + +/** + * Miscellaneous static utility methods. + * + * @author Colin Decker + * @author Austin Appleby + */ +final class Util { + + private Util() {} + + /** Returns the next power of 2 >= n. */ + public static int nextPowerOf2(int n) { + if (n == 0) { + return 1; + } + int b = Integer.highestOneBit(n); + return b == n ? n : b << 1; + } + + /** + * Checks that the given number is not negative, throwing IAE if it is. The given description + * describes the number in the exception message. + */ + static void checkNotNegative(long n, String description) { + checkArgument(n >= 0, "%s must not be negative: %s", description, n); + } + + /** Checks that no element in the given iterable is null, throwing NPE if any is. */ + static void checkNoneNull(Iterable<?> objects) { + if (!(objects instanceof ImmutableCollection)) { + for (Object o : objects) { + checkNotNull(o); + } + } + } + + private static final int C1 = 0xcc9e2d51; + private static final int C2 = 0x1b873593; + + /* + * This method was rewritten in Java from an intermediate step of the Murmur hash function in + * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp, which contained the + * following header: + * + * MurmurHash3 was written by Austin Appleby, and is placed in the public domain. The author + * hereby disclaims copyright to this source code. + */ + static int smearHash(int hashCode) { + return C2 * Integer.rotateLeft(hashCode * C1, 15); + } + + private static final int ARRAY_LEN = 8192; + private static final byte[] ZERO_ARRAY = new byte[ARRAY_LEN]; + private static final byte[][] NULL_ARRAY = new byte[ARRAY_LEN][]; + + /** Zeroes all bytes between off (inclusive) and off + len (exclusive) in the given array. */ + static void zero(byte[] bytes, int off, int len) { + // this is significantly faster than looping or Arrays.fill (which loops), particularly when + // the length of the slice to be zeroed is <= to ARRAY_LEN (in that case, it's faster by a + // factor of 2) + int remaining = len; + while (remaining > ARRAY_LEN) { + System.arraycopy(ZERO_ARRAY, 0, bytes, off, ARRAY_LEN); + off += ARRAY_LEN; + remaining -= ARRAY_LEN; + } + + System.arraycopy(ZERO_ARRAY, 0, bytes, off, remaining); + } + + /** + * Clears (sets to null) all blocks between off (inclusive) and off + len (exclusive) in the given + * array. + */ + static void clear(byte[][] blocks, int off, int len) { + // this is significantly faster than looping or Arrays.fill (which loops), particularly when + // the length of the slice to be cleared is <= to ARRAY_LEN (in that case, it's faster by a + // factor of 2) + int remaining = len; + while (remaining > ARRAY_LEN) { + System.arraycopy(NULL_ARRAY, 0, blocks, off, ARRAY_LEN); + off += ARRAY_LEN; + remaining -= ARRAY_LEN; + } + + System.arraycopy(NULL_ARRAY, 0, blocks, off, remaining); + } +} |