本文整理了Java中com.google.common.hash.HashCode.fromBytesNoCopy()
方法的一些代码示例,展示了HashCode.fromBytesNoCopy()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HashCode.fromBytesNoCopy()
方法的具体详情如下:
包路径:com.google.common.hash.HashCode
类名称:HashCode
方法名:fromBytesNoCopy
[英]Creates a HashCode from a byte array. The array is not copied defensively, so it must be handed-off so as to preserve the immutability contract of HashCode.
[中]从字节数组创建哈希代码。数组不是防御性复制的,因此必须将其传递,以保持哈希代码的不变性契约。
代码示例来源:origin: google/guava
/**
* Creates a {@code HashCode} from a byte array. The array is defensively copied to preserve the
* immutability contract of {@code HashCode}. The array cannot be empty.
*
* @since 15.0 (since 12.0 in HashCodes)
*/
public static HashCode fromBytes(byte[] bytes) {
checkArgument(bytes.length >= 1, "A HashCode must contain at least 1 byte.");
return fromBytesNoCopy(bytes.clone());
}
代码示例来源:origin: google/guava
@Override
public HashCode hash() {
checkNotDone();
done = true;
return (bytes == digest.getDigestLength())
? HashCode.fromBytesNoCopy(digest.digest())
: HashCode.fromBytesNoCopy(Arrays.copyOf(digest.digest(), bytes));
}
}
代码示例来源:origin: google/guava
@Override
public HashCode hash() {
checkNotDone();
done = true;
return HashCode.fromBytesNoCopy(mac.doFinal());
}
}
代码示例来源:origin: google/guava
@Override
public HashCode makeHash() {
h1 ^= length;
h2 ^= length;
h1 += h2;
h2 += h1;
h1 = fmix64(h1);
h2 = fmix64(h2);
h1 += h2;
h2 += h1;
return HashCode.fromBytesNoCopy(
ByteBuffer.wrap(new byte[CHUNK_SIZE])
.order(ByteOrder.LITTLE_ENDIAN)
.putLong(h1)
.putLong(h2)
.array());
}
代码示例来源:origin: google/guava
/**
* Returns a hash code, having the same bit length as each of the input hash codes, that combines
* the information of these hash codes in an unordered fashion. That is, whenever two equal hash
* codes are produced by two calls to this method, it is <i>as likely as possible</i> that each
* was computed from the <i>same</i> input hash codes in <i>some</i> order.
*
* @throws IllegalArgumentException if {@code hashCodes} is empty, or the hash codes do not all
* have the same bit length
*/
public static HashCode combineUnordered(Iterable<HashCode> hashCodes) {
Iterator<HashCode> iterator = hashCodes.iterator();
checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
byte[] resultBytes = new byte[iterator.next().bits() / 8];
for (HashCode hashCode : hashCodes) {
byte[] nextBytes = hashCode.asBytes();
checkArgument(
nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
for (int i = 0; i < nextBytes.length; i++) {
resultBytes[i] += nextBytes[i];
}
}
return HashCode.fromBytesNoCopy(resultBytes);
}
代码示例来源:origin: google/guava
/**
* Returns a hash code, having the same bit length as each of the input hash codes, that combines
* the information of these hash codes in an ordered fashion. That is, whenever two equal hash
* codes are produced by two calls to this method, it is <i>as likely as possible</i> that each
* was computed from the <i>same</i> input hash codes in the <i>same</i> order.
*
* @throws IllegalArgumentException if {@code hashCodes} is empty, or the hash codes do not all
* have the same bit length
*/
public static HashCode combineOrdered(Iterable<HashCode> hashCodes) {
Iterator<HashCode> iterator = hashCodes.iterator();
checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
int bits = iterator.next().bits();
byte[] resultBytes = new byte[bits / 8];
for (HashCode hashCode : hashCodes) {
byte[] nextBytes = hashCode.asBytes();
checkArgument(
nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
for (int i = 0; i < nextBytes.length; i++) {
resultBytes[i] = (byte) (resultBytes[i] * 37 ^ nextBytes[i]);
}
}
return HashCode.fromBytesNoCopy(resultBytes);
}
代码示例来源:origin: google/j2objc
@Override
public HashCode hash() {
checkNotDone();
done = true;
return HashCode.fromBytesNoCopy(mac.doFinal());
}
}
代码示例来源:origin: google/j2objc
/**
* Creates a {@code HashCode} from a byte array. The array is defensively copied to preserve the
* immutability contract of {@code HashCode}. The array cannot be empty.
*
* @since 15.0 (since 12.0 in HashCodes)
*/
public static HashCode fromBytes(byte[] bytes) {
checkArgument(bytes.length >= 1, "A HashCode must contain at least 1 byte.");
return fromBytesNoCopy(bytes.clone());
}
代码示例来源:origin: google/j2objc
@Override
public HashCode hash() {
checkNotDone();
done = true;
return (bytes == digest.getDigestLength())
? HashCode.fromBytesNoCopy(digest.digest())
: HashCode.fromBytesNoCopy(Arrays.copyOf(digest.digest(), bytes));
}
}
代码示例来源:origin: google/guava
/**
* Creates a {@code HashCode} from a hexadecimal ({@code base 16}) encoded string. The string must
* be at least 2 characters long, and contain only valid, lower-cased hexadecimal characters.
*
* <p>This method accepts the exact format generated by {@link #toString}. If you require more
* lenient {@code base 16} decoding, please use {@link com.google.common.io.BaseEncoding#decode}
* (and pass the result to {@link #fromBytes}).
*
* @since 15.0
*/
public static HashCode fromString(String string) {
checkArgument(
string.length() >= 2, "input string (%s) must have at least 2 characters", string);
checkArgument(
string.length() % 2 == 0,
"input string (%s) must have an even number of characters",
string);
byte[] bytes = new byte[string.length() / 2];
for (int i = 0; i < string.length(); i += 2) {
int ch1 = decode(string.charAt(i)) << 4;
int ch2 = decode(string.charAt(i + 1));
bytes[i / 2] = (byte) (ch1 + ch2);
}
return fromBytesNoCopy(bytes);
}
代码示例来源:origin: google/guava
@Override
HashCode makeHash(Hasher[] hashers) {
byte[] bytes = new byte[bits() / 8];
int i = 0;
for (Hasher hasher : hashers) {
HashCode newHash = hasher.hash();
i += newHash.writeBytesTo(bytes, i, newHash.bits() / 8);
}
return HashCode.fromBytesNoCopy(bytes);
}
代码示例来源:origin: wildfly/wildfly
/**
* Creates a {@code HashCode} from a byte array. The array is defensively copied to preserve the
* immutability contract of {@code HashCode}. The array cannot be empty.
*
* @since 15.0 (since 12.0 in HashCodes)
*/
public static HashCode fromBytes(byte[] bytes) {
checkArgument(bytes.length >= 1, "A HashCode must contain at least 1 byte.");
return fromBytesNoCopy(bytes.clone());
}
代码示例来源:origin: wildfly/wildfly
@Override
public HashCode hash() {
checkNotDone();
done = true;
return HashCode.fromBytesNoCopy(mac.doFinal());
}
}
代码示例来源:origin: google/guava
@Override
public HashCode hash() {
return HashCode.fromBytesNoCopy(bytes());
}
}
代码示例来源:origin: google/j2objc
@Override
HashCode makeHash(Hasher[] hashers) {
byte[] bytes = new byte[bits() / 8];
int i = 0;
for (Hasher hasher : hashers) {
HashCode newHash = hasher.hash();
i += newHash.writeBytesTo(bytes, i, newHash.bits() / 8);
}
return HashCode.fromBytesNoCopy(bytes);
}
代码示例来源:origin: wildfly/wildfly
@Override
HashCode makeHash(Hasher[] hashers) {
byte[] bytes = new byte[bits() / 8];
int i = 0;
for (Hasher hasher : hashers) {
HashCode newHash = hasher.hash();
i += newHash.writeBytesTo(bytes, i, newHash.bits() / 8);
}
return HashCode.fromBytesNoCopy(bytes);
}
代码示例来源:origin: google/guava
public void testPadToLongWith6Bytes() {
assertEquals(0x0000999999999999L, HashCode.fromBytesNoCopy(byteArrayWith9s(6)).padToLong());
}
代码示例来源:origin: google/guava
public void testPadToLongWith8Bytes() {
assertEquals(0x9999999999999999L, HashCode.fromBytesNoCopy(byteArrayWith9s(8)).padToLong());
}
代码示例来源:origin: google/guava
public void testPadToLongWith4Bytes() {
assertEquals(0x0000000099999999L, HashCode.fromBytesNoCopy(byteArrayWith9s(4)).padToLong());
}
代码示例来源:origin: google/guava
public void testFromBytesNoCopy_noCopyOccurs() {
byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00};
HashCode hashCode = HashCode.fromBytesNoCopy(bytes);
assertEquals(0x0000abcd, hashCode.asInt());
assertEquals("cdab0000", hashCode.toString());
bytes[0] = (byte) 0x00;
assertEquals(0x0000ab00, hashCode.asInt());
assertEquals("00ab0000", hashCode.toString());
}
内容来源于网络,如有侵权,请联系作者删除!