Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
pulsar
提交
554f0505
pulsar
项目概览
apache
/
pulsar
通知
129
Star
40
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
pulsar
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Pages
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
提交
体验新版 GitCode,发现更多精彩内容 >>
提交
554f0505
编写于
9月 28, 2016
作者:
R
Rajan Dhabalia
提交者:
Matteo Merli
9月 28, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add circe source code to checksum library (#44)
上级
26671f61
变更
60
显示空白变更内容
内联
并排
Showing
60 changed file
with
4713 addition
and
30 deletion
+4713
-30
NOTICE
NOTICE
+9
-1
pom.xml
pom.xml
+6
-0
protobuf/LICENSE.protobuf.txt
protobuf/LICENSE.protobuf.txt
+42
-0
pulsar-checksum/pom.xml
pulsar-checksum/pom.xml
+4
-29
pulsar-checksum/src/main/java/com/scurrilous/circe/CommonHashes.java
...ksum/src/main/java/com/scurrilous/circe/CommonHashes.java
+188
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/Hash.java
pulsar-checksum/src/main/java/com/scurrilous/circe/Hash.java
+53
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/HashParameters.java
...um/src/main/java/com/scurrilous/circe/HashParameters.java
+30
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/HashProvider.java
...ksum/src/main/java/com/scurrilous/circe/HashProvider.java
+101
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/HashProviders.java
...sum/src/main/java/com/scurrilous/circe/HashProviders.java
+125
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/HashSupport.java
...cksum/src/main/java/com/scurrilous/circe/HashSupport.java
+179
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/Hashes.java
...r-checksum/src/main/java/com/scurrilous/circe/Hashes.java
+123
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/IncrementalIntHash.java
...rc/main/java/com/scurrilous/circe/IncrementalIntHash.java
+88
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/IncrementalLongHash.java
...c/main/java/com/scurrilous/circe/IncrementalLongHash.java
+88
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/StatefulHash.java
...ksum/src/main/java/com/scurrilous/circe/StatefulHash.java
+171
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/StatefulIntHash.java
...m/src/main/java/com/scurrilous/circe/StatefulIntHash.java
+31
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/StatefulLongHash.java
.../src/main/java/com/scurrilous/circe/StatefulLongHash.java
+30
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/StatelessHash.java
...sum/src/main/java/com/scurrilous/circe/StatelessHash.java
+31
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/StatelessIntHash.java
.../src/main/java/com/scurrilous/circe/StatelessIntHash.java
+77
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/StatelessLongHash.java
...src/main/java/com/scurrilous/circe/StatelessLongHash.java
+77
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/AbstractIntCrc.java
...rc/main/java/com/scurrilous/circe/crc/AbstractIntCrc.java
+64
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/AbstractLongCrc.java
...c/main/java/com/scurrilous/circe/crc/AbstractLongCrc.java
+64
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/JavaCrc32.java
...sum/src/main/java/com/scurrilous/circe/crc/JavaCrc32.java
+103
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/NormalByteCrc.java
...src/main/java/com/scurrilous/circe/crc/NormalByteCrc.java
+46
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/NormalIntCrc.java
.../src/main/java/com/scurrilous/circe/crc/NormalIntCrc.java
+48
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/NormalLongCrc.java
...src/main/java/com/scurrilous/circe/crc/NormalLongCrc.java
+48
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/ReflectedIntCrc.java
...c/main/java/com/scurrilous/circe/crc/ReflectedIntCrc.java
+45
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/ReflectedLongCrc.java
.../main/java/com/scurrilous/circe/crc/ReflectedLongCrc.java
+45
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/StandardCrcProvider.java
...in/java/com/scurrilous/circe/crc/StandardCrcProvider.java
+82
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/package-info.java
.../src/main/java/com/scurrilous/circe/crc/package-info.java
+9
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractHashProvider.java
.../java/com/scurrilous/circe/impl/AbstractHashProvider.java
+193
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractIncrementalIntHash.java
...com/scurrilous/circe/impl/AbstractIncrementalIntHash.java
+114
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractIncrementalLongHash.java
...om/scurrilous/circe/impl/AbstractIncrementalLongHash.java
+114
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractStatefulHash.java
.../java/com/scurrilous/circe/impl/AbstractStatefulHash.java
+132
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractStatelessIntHash.java
...a/com/scurrilous/circe/impl/AbstractStatelessIntHash.java
+79
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractStatelessLongHash.java
.../com/scurrilous/circe/impl/AbstractStatelessLongHash.java
+79
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/DirectByteBufferAccess.java
...ava/com/scurrilous/circe/impl/DirectByteBufferAccess.java
+18
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/DirectByteBufferAccessLoader.java
...m/scurrilous/circe/impl/DirectByteBufferAccessLoader.java
+49
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/HashCache.java
...um/src/main/java/com/scurrilous/circe/impl/HashCache.java
+44
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/HashCacheLoader.java
.../main/java/com/scurrilous/circe/impl/HashCacheLoader.java
+58
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/IncrementalIntStatefulHash.java
...com/scurrilous/circe/impl/IncrementalIntStatefulHash.java
+92
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/IncrementalLongStatefulHash.java
...om/scurrilous/circe/impl/IncrementalLongStatefulHash.java
+92
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/IntStatefulLongHash.java
...n/java/com/scurrilous/circe/impl/IntStatefulLongHash.java
+109
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/IntStatelessLongHash.java
.../java/com/scurrilous/circe/impl/IntStatelessLongHash.java
+80
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/package-info.java
...src/main/java/com/scurrilous/circe/impl/package-info.java
+6
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/package-info.java
...ksum/src/main/java/com/scurrilous/circe/package-info.java
+6
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/params/CrcParameters.java
.../main/java/com/scurrilous/circe/params/CrcParameters.java
+205
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/params/MurmurHash3Parameters.java
...va/com/scurrilous/circe/params/MurmurHash3Parameters.java
+73
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/params/MurmurHash3Variant.java
.../java/com/scurrilous/circe/params/MurmurHash3Variant.java
+52
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/params/SimpleHashParameters.java
...ava/com/scurrilous/circe/params/SimpleHashParameters.java
+85
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/params/SipHash24Parameters.java
...java/com/scurrilous/circe/params/SipHash24Parameters.java
+69
-0
pulsar-checksum/src/main/java/com/scurrilous/circe/params/package-info.java
...c/main/java/com/scurrilous/circe/params/package-info.java
+5
-0
pulsar-checksum/src/main/resources/META-INF/services/com.scurrilous.circe.HashProvider
...urces/META-INF/services/com.scurrilous.circe.HashProvider
+1
-0
pulsar-checksum/src/test/java/com/scurrilous/circe/CommonHashesTest.java
.../src/test/java/com/scurrilous/circe/CommonHashesTest.java
+45
-0
pulsar-checksum/src/test/java/com/scurrilous/circe/crc/CRCProvidersTest.java
.../test/java/com/scurrilous/circe/crc/CRCProvidersTest.java
+88
-0
pulsar-checksum/src/test/java/com/scurrilous/circe/crc/CRCTest.java
...cksum/src/test/java/com/scurrilous/circe/crc/CRCTest.java
+163
-0
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractIncrementalIntHashTest.java
...scurrilous/circe/impl/AbstractIncrementalIntHashTest.java
+147
-0
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractIncrementalLongHashTest.java
...currilous/circe/impl/AbstractIncrementalLongHashTest.java
+147
-0
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractStatefulHashTest.java
...a/com/scurrilous/circe/impl/AbstractStatefulHashTest.java
+193
-0
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractStatelessIntHashTest.java
...m/scurrilous/circe/impl/AbstractStatelessIntHashTest.java
+84
-0
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractStatelessLongHashTest.java
.../scurrilous/circe/impl/AbstractStatelessLongHashTest.java
+84
-0
未找到文件。
NOTICE
浏览文件 @
554f0505
...
@@ -23,6 +23,14 @@ This product contains a modified portion of and depends on 'Protocol Buffers',
...
@@ -23,6 +23,14 @@ This product contains a modified portion of and depends on 'Protocol Buffers',
Google's data interchange format, which can be obtained at:
Google's data interchange format, which can be obtained at:
* LICENSE:
* LICENSE:
*
license/LICENSE.protobuf.txt (New BSD License)
*
protobuf/LICENSE.protobuf.txt
* HOMEPAGE:
* HOMEPAGE:
* http://code.google.com/p/protobuf/
* http://code.google.com/p/protobuf/
This product contains a modified portion of and depends on 'circe',
Hash Algorithm Framework & Library, which can be obtained at:
* LICENSE : Apache License, Version 2.0
* HOMEPAGE:
* https://github.com/trevorr/circe
\ No newline at end of file
pom.xml
浏览文件 @
554f0505
...
@@ -133,6 +133,12 @@ flexible messaging model and an intuitive client API.</description>
...
@@ -133,6 +133,12 @@ flexible messaging model and an intuitive client API.</description>
<version>
1.6.5
</version>
<version>
1.6.5
</version>
</dependency>
</dependency>
<dependency>
<groupId>
com.googlecode.jmockit
</groupId>
<artifactId>
jmockit
</artifactId>
<version>
1.7
</version>
</dependency>
<dependency>
<dependency>
<groupId>
org.apache.zookeeper
</groupId>
<groupId>
org.apache.zookeeper
</groupId>
<artifactId>
zookeeper
</artifactId>
<artifactId>
zookeeper
</artifactId>
...
...
protobuf/LICENSE.protobuf.txt
0 → 100644
浏览文件 @
554f0505
This license applies to all parts of Protocol Buffers except the following:
- Atomicops support for generic gcc, located in
src/google/protobuf/stubs/atomicops_internals_generic_gcc.h.
This file is copyrighted by Red Hat Inc.
- Atomicops support for AIX/POWER, located in
src/google/protobuf/stubs/atomicops_internals_power.h.
This file is copyrighted by Bloomberg Finance LP.
Copyright 2014, Google Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Code generated by the Protocol Buffer compiler is owned by the owner
of the input file used when generating it. This code is not
standalone and requires a support library to be linked with it. This
support library is itself covered by the above license.
\ No newline at end of file
pulsar-checksum/pom.xml
浏览文件 @
554f0505
...
@@ -55,27 +55,9 @@
...
@@ -55,27 +55,9 @@
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
com.scurrilous
</groupId>
<groupId>
com.googlecode.jmockit
</groupId>
<artifactId>
circe-api
</artifactId>
<artifactId>
jmockit
</artifactId>
<version>
1.1-20140603.044855-1
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
com.scurrilous
</groupId>
<artifactId>
circe-impl
</artifactId>
<version>
1.1-20140603.044907-1
</version>
</dependency>
<dependency>
<groupId>
com.scurrilous
</groupId>
<artifactId>
circe-common
</artifactId>
<version>
1.1-20140603.044903-1
</version>
</dependency>
<dependency>
<groupId>
com.scurrilous
</groupId>
<artifactId>
circe-crc
</artifactId>
<version>
1.1-20140603.044857-1
</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
...
@@ -183,11 +165,4 @@
...
@@ -183,11 +165,4 @@
</profile>
</profile>
</profiles>
</profiles>
<repositories>
<repository>
<id>
circe-crc-repo
</id>
<url>
https://oss.sonatype.org/content/repositories/staging
</url>
</repository>
</repositories>
</project>
</project>
pulsar-checksum/src/main/java/com/scurrilous/circe/CommonHashes.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
com.scurrilous.circe.params.CrcParameters
;
import
com.scurrilous.circe.params.MurmurHash3Parameters
;
import
com.scurrilous.circe.params.MurmurHash3Variant
;
import
com.scurrilous.circe.params.SimpleHashParameters
;
import
com.scurrilous.circe.params.SipHash24Parameters
;
/**
* Static methods to obtain commonly-used hash functions. Note that a suitable
* provider JAR must be made available on the class path. This class does not
* have direct access to specific implementations; it simply constructs the hash
* parameters and uses the {@link Hashes} class to search for a provider.
*/
public
final
class
CommonHashes
{
private
CommonHashes
()
{
}
/**
* Returns an incremental stateless hash function implementing the
* {@linkplain CrcParameters#CRC32 CRC-32} checksum algorithm.
*
* @return a CRC-32 stateless incremental integer hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
IncrementalIntHash
crc32
()
{
return
Hashes
.
getIncrementalInt
(
CrcParameters
.
CRC32
);
}
/**
* Returns an incremental stateless hash function implementing the
* {@linkplain CrcParameters#CRC32C CRC-32C} checksum algorithm.
*
* @return a CRC-32C stateless incremental integer hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
IncrementalIntHash
crc32c
()
{
return
Hashes
.
getIncrementalInt
(
CrcParameters
.
CRC32C
);
}
/**
* Returns an incremental stateless hash function implementing the
* {@linkplain CrcParameters#CRC64 CRC-64} checksum algorithm.
*
* @return a CRC-64 stateless incremental long integer hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
IncrementalLongHash
crc64
()
{
return
Hashes
.
getIncrementalLong
(
CrcParameters
.
CRC64
);
}
/**
* Returns a hash function implementing the MurmurHash3 algorithm, 32-bit
* x86 variant, with a seed of 0.
*
* @return a MurmurHash3 32-bit/x86 stateless integer hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatelessIntHash
murmur3_32
()
{
return
Hashes
.
getStatelessInt
(
new
MurmurHash3Parameters
(
MurmurHash3Variant
.
X86_32
));
}
/**
* Returns a hash function implementing the MurmurHash3 algorithm, 32-bit
* x86 variant, with the given seed value
*
* @param seed the 32-bit seed value
* @return a MurmurHash3 32-bit/x86 stateless integer hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatelessIntHash
murmur3_32
(
int
seed
)
{
return
Hashes
.
getStatelessInt
(
new
MurmurHash3Parameters
(
MurmurHash3Variant
.
X86_32
,
seed
));
}
/**
* Returns a hash function implementing the MurmurHash3 algorithm, 128-bit
* x64 variant, with a seed of 0.
*
* @return a MurmurHash3 128-bit/x64 stateful hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatefulHash
murmur3_128
()
{
return
Hashes
.
createStateful
(
new
MurmurHash3Parameters
(
MurmurHash3Variant
.
X64_128
));
}
/**
* Returns a hash function implementing the MurmurHash3 algorithm, 128-bit
* x64 variant, with the given seed value.
*
* @param seed the 32-bit seed value
* @return a MurmurHash3 128-bit/x64 stateful hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatefulHash
murmur3_128
(
int
seed
)
{
return
Hashes
.
createStateful
(
new
MurmurHash3Parameters
(
MurmurHash3Variant
.
X64_128
,
seed
));
}
/**
* Returns a hash function implementing the SipHash-2-4 algorithm (64 bits)
* with the default seed,
* {@code 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F}.
*
* @return a SipHash-2-4 stateless long integer hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatelessLongHash
sipHash24
()
{
return
Hashes
.
getStatelessLong
(
new
SipHash24Parameters
());
}
/**
* Returns a hash function implementing the SipHash-2-4 algorithm (64 bits)
* with the given seed value.
*
* @param seedLow the low-order 64 bits of the seed
* @param seedHigh the high-order 64 bits of the seed
* @return a SipHash-2-4 stateless long integer hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatelessLongHash
sipHash24
(
long
seedLow
,
long
seedHigh
)
{
return
Hashes
.
getStatelessLong
(
new
SipHash24Parameters
(
seedLow
,
seedHigh
));
}
/**
* Returns a hash function implementing the MD5 algorithm (128 bits).
*
* @return an MD5 stateful hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatefulHash
md5
()
{
return
Hashes
.
createStateful
(
SimpleHashParameters
.
MD5
);
}
/**
* Returns a hash function implementing the SHA-1 algorithm (160 bits).
*
* @return a SHA-1 stateful hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatefulHash
sha1
()
{
return
Hashes
.
createStateful
(
SimpleHashParameters
.
SHA1
);
}
/**
* Returns a hash function implementing the SHA-256 algorithm (256 bits).
*
* @return a SHA-256 stateful hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatefulHash
sha256
()
{
return
Hashes
.
createStateful
(
SimpleHashParameters
.
SHA256
);
}
/**
* Returns a hash function implementing the SHA-384 algorithm (384 bits).
*
* @return a SHA-384 stateful hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatefulHash
sha384
()
{
return
Hashes
.
createStateful
(
SimpleHashParameters
.
SHA384
);
}
/**
* Returns a hash function implementing the SHA-512 algorithm (512 bits).
*
* @return a SHA-512 stateful hash
* @throws UnsupportedOperationException if no provider is available
*/
public
static
StatefulHash
sha512
()
{
return
Hashes
.
createStateful
(
SimpleHashParameters
.
SHA512
);
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/Hash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
/**
* Abstract hash function. Each actual hash function is provided using a
* {@linkplain StatefulHash stateful} derived interface. Hash functions with an
* output length that fits within an {@code int} or a {@code long} are also
* generally provided using a {@linkplain StatelessHash stateless} derived
* interface. Given a stateless hash object, a method is provided for obtaining
* a new corresponding stateful object.
*/
public
interface
Hash
{
/**
* Returns the canonical name of this hash algorithm.
*
* @return the name of this hash algorithm
*/
String
algorithm
();
/**
* Returns the length in bytes of the output of this hash function.
*
* @return the hash length in bytes
*/
int
length
();
/**
* Returns whether this hash function supports unsafe access to arbitrary
* memory addresses using methods such as
* {@link StatefulHash#update(long, long)},
* {@link StatelessIntHash#calculate(long, long)}, or
* {@link IncrementalIntHash#resume(int, long, long)}. Such functions are
* generally implemented in native code.
*
* @return true if unsafe access is supported, false if not
*/
boolean
supportsUnsafe
();
}
pulsar-checksum/src/main/java/com/scurrilous/circe/HashParameters.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
/**
* Base interface implemented by classes describing parameters for a particular
* family of hash algorithms.
*/
public
interface
HashParameters
{
/**
* Returns the canonical name of the hash algorithm.
*
* @return the name of the hash algorithm
*/
String
algorithm
();
}
pulsar-checksum/src/main/java/com/scurrilous/circe/HashProvider.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
java.util.EnumSet
;
/**
* Interface used to obtain instances of various kinds of hash algorithms.
*/
public
interface
HashProvider
{
/**
* Returns information about the available implementations corresponding to
* the given hash algorithm parameters.
*
* @param params the hash algorithm parameters
* @return a set of flags indicating the level of support
*/
EnumSet
<
HashSupport
>
querySupport
(
HashParameters
params
);
/**
* Creates a stateful hash function using the given parameters.
*
* @param params the hash algorithm parameters
* @return a stateful hash function
* @throws UnsupportedOperationException if this provider cannot support the
* given parameters
*/
StatefulHash
createStateful
(
HashParameters
params
);
/**
* Requests a stateless, int-width hash function with the given parameters.
* Because not all stateless hash functions are incremental, this method may
* be able to return implementations not supported by or more optimized than
* {@link #getIncrementalInt}.
*
* @param params the hash algorithm parameters
* @return a stateless int-width hash function
* @throws UnsupportedOperationException if this provider cannot support the
* given parameters
*/
StatelessIntHash
getStatelessInt
(
HashParameters
params
);
/**
* Requests a stateless, long-width hash function with the given parameters.
* Because not all stateless hash functions are incremental, this method may
* be able to return implementations not supported by or more optimized than
* {@link #getIncrementalLong}.
* <p>
* Note that this method may return a less efficient hash function than
* {@link #getStatelessInt} for hashes of 32 bits or less.
*
* @param params the hash algorithm parameters
* @return a stateless long-width hash function
* @throws UnsupportedOperationException if this provider cannot support the
* given parameters
*/
StatelessLongHash
getStatelessLong
(
HashParameters
params
);
/**
* Requests an incremental, stateless, int-width hash function with the
* given parameters. Note that although an algorithm may be available in
* incremental form, some potentially more optimized implementations may not
* support that form, and therefore cannot be provided be this method.
*
* @param params the hash algorithm parameters
* @return a stateful int-width hash function
* @throws UnsupportedOperationException if this provider cannot support the
* given parameters
*/
IncrementalIntHash
getIncrementalInt
(
HashParameters
params
);
/**
* Requests an incremental, stateless, long-width hash function with the
* given parameters. Note that although an algorithm may be available in
* incremental form, some potentially more optimized implementations may not
* support that form, and therefore cannot be provided be this method.
* <p>
* Also note that this method may return a less efficient hash function than
* {@link #getIncrementalInt} for hashes of 32 bits or less.
*
* @param params the hash algorithm parameters
* @return a stateful long-width hash function
* @throws UnsupportedOperationException if this provider cannot support the
* given parameters
*/
IncrementalLongHash
getIncrementalLong
(
HashParameters
params
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/HashProviders.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.EnumSet
;
import
java.util.Iterator
;
import
java.util.LinkedList
;
import
java.util.ServiceLoader
;
import
java.util.SortedMap
;
import
java.util.TreeMap
;
/**
* Static utility methods for discovering {@link HashProvider} instances.
*/
public
final
class
HashProviders
{
static
final
Collection
<
HashProvider
>
ALL_PROVIDERS
=
getAllProviders
();
private
static
Collection
<
HashProvider
>
getAllProviders
()
{
final
ServiceLoader
<
HashProvider
>
loader
=
ServiceLoader
.
load
(
HashProvider
.
class
);
final
LinkedList
<
HashProvider
>
providers
=
new
LinkedList
<>();
for
(
final
HashProvider
provider
:
loader
)
providers
.
add
(
provider
);
return
Collections
.
unmodifiableList
(
new
ArrayList
<>(
providers
));
}
private
HashProviders
()
{
}
/**
* Returns an iterator over all known {@link HashProvider} instances.
*
* @return an iterator over all HashProviders
*/
public
static
Iterator
<
HashProvider
>
iterator
()
{
return
ALL_PROVIDERS
.
iterator
();
}
/**
* Returns the best hash provider supporting at least a stateful
* implementation of a hash function with the given parameters.
*
* @param params the parameters defining the hash function
* @return the best hash provider for the given parameters
* @throws UnsupportedOperationException if no provider supports the
* parameters
*/
public
static
HashProvider
best
(
HashParameters
params
)
{
return
best
(
params
,
EnumSet
.
of
(
HashSupport
.
STATEFUL
));
}
/**
* Returns the best hash provider supporting at least the given flags for a
* hash function with the given parameters.
*
* @param params the parameters defining the hash function
* @param required the required support flags for a provider to be
* considered
* @return the best hash provider for the given parameters
* @throws UnsupportedOperationException if no provider supports the
* parameters
*/
public
static
HashProvider
best
(
HashParameters
params
,
EnumSet
<
HashSupport
>
required
)
{
HashProvider
result
=
null
;
EnumSet
<
HashSupport
>
resultSupport
=
null
;
for
(
final
HashProvider
provider
:
ALL_PROVIDERS
)
{
final
EnumSet
<
HashSupport
>
support
=
provider
.
querySupport
(
params
);
if
(
support
.
containsAll
(
required
)
&&
(
result
==
null
||
HashSupport
.
compare
(
support
,
resultSupport
)
<
0
))
{
result
=
provider
;
resultSupport
=
support
;
}
}
if
(
result
==
null
)
throw
new
UnsupportedOperationException
();
return
result
;
}
/**
* Returns a map of hash providers supporting at least a stateful
* implementation of a hash function with the given parameters.
*
* @param params the parameters defining the hash function
* @return a sorted map of hash support flags to hash providers
*/
public
static
SortedMap
<
EnumSet
<
HashSupport
>,
HashProvider
>
search
(
HashParameters
params
)
{
return
search
(
params
,
EnumSet
.
of
(
HashSupport
.
STATEFUL
));
}
/**
* Returns a map of hash providers supporting at least the given flags for a
* hash function with the given parameters.
*
* @param params the parameters defining the hash function
* @param required the required support flags for a provider to be included
* @return a sorted map of hash support flags to hash providers
*/
public
static
SortedMap
<
EnumSet
<
HashSupport
>,
HashProvider
>
search
(
HashParameters
params
,
EnumSet
<
HashSupport
>
required
)
{
final
SortedMap
<
EnumSet
<
HashSupport
>,
HashProvider
>
result
=
new
TreeMap
<>(
new
HashSupport
.
SetComparator
());
for
(
final
HashProvider
provider
:
ALL_PROVIDERS
)
{
final
EnumSet
<
HashSupport
>
support
=
provider
.
querySupport
(
params
);
if
(
support
.
containsAll
(
required
))
result
.
put
(
support
,
provider
);
}
return
result
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/HashSupport.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
java.util.Comparator
;
import
java.util.EnumSet
;
import
java.util.Iterator
;
/**
* Flags indicating the support available for some set of hash algorithm.
*/
public
enum
HashSupport
{
/**
* Indicates that the hash algorithm is available in hardware-accelerated
* native code as an {@link IncrementalIntHash} or
* {@link IncrementalLongHash}, depending on which of {@link #INT_SIZED} or
* {@link #LONG_SIZED} is set.
*/
HARDWARE_INCREMENTAL
(
10
),
/**
* Indicates that the hash algorithm is available in hardware-accelerated
* native code.
*/
HARDWARE
(
20
),
/**
* Indicates that the hash algorithm is available in native code as a
* {@link IncrementalIntHash} or {@link IncrementalLongHash}, depending on
* which of {@link #INT_SIZED} or {@link #LONG_SIZED} is set.
*/
NATIVE_INCREMENTAL
(
30
),
/**
* Indicates that the hash algorithm is available in native code.
*/
NATIVE
(
40
),
/**
* Indicates that the incremental hash algorithm supports unsafe memory
* access via {@link IncrementalIntHash#resume(int, long, long)} or
* {@link IncrementalLongHash#resume(long, long, long)}, depending on which
* of {@link #INT_SIZED} or {@link #LONG_SIZED} is set.
*/
UNSAFE_INCREMENTAL
(
50
),
/**
* Indicates that the stateful hash algorithm unsafe memory access via
* {@link StatefulHash#update(long, long)}. If {@link #INT_SIZED} is also
* set, the function returned by {@link StatefulIntHash#asStateless()} also
* supports {@link StatelessIntHash#calculate(long, long)}. Similarly, if
* {@link #LONG_SIZED} is also set, the function returned by
* {@link StatefulLongHash#asStateless()} also supports
* {@link StatelessLongHash#calculate(long, long)}.
*/
UNSAFE
(
60
),
/**
* Indicates that the hash algorithm is available as a
* {@link IncrementalIntHash} or {@link IncrementalLongHash}, depending on
* which of {@link #INT_SIZED} or {@link #LONG_SIZED} is set.
*/
STATELESS_INCREMENTAL
(
70
),
/**
* Indicates that the hash algorithm is available as an incremental stateful
* hash function, for which {@link StatefulHash#supportsIncremental()}
* returns {@code true}. This flag is implied by
* {@link #STATELESS_INCREMENTAL}.
*/
INCREMENTAL
(
80
),
/**
* Indicates that the hash algorithm is available as a
* {@link StatefulIntHash} and {@link StatelessIntHash}.
*/
INT_SIZED
(
90
),
/**
* Indicates that the hash algorithm is available as a
* {@link StatefulLongHash} and {@link StatelessLongHash}.
*/
LONG_SIZED
(
90
),
/**
* Indicates that the hash algorithm is available as a {@link StatefulHash}.
* If this flag is not set, the algorithm is not supported at all.
*/
STATEFUL
(
100
);
/**
* The minimum priority value, indicating the highest priority. All flags
* have a priority value greater than this.
*/
public
static
final
int
MIN_PRIORITY
=
0
;
/**
* The maximum priority value, indicating the lowest priority. All flags
* have a priority value less than this.
*/
public
static
final
int
MAX_PRIORITY
=
110
;
private
final
int
priority
;
private
HashSupport
(
int
priority
)
{
this
.
priority
=
priority
;
}
/**
* Returns the relative priority of a hash algorithm support flag, which is
* an indicator of its performance and flexibility. Lower values indicate
* higher priority.
*
* @return the priority of this flag (currently between 10 and 90)
*/
public
int
getPriority
()
{
return
priority
;
}
/**
* Returns the {@linkplain #getPriority() priority} of the highest-priority
* hash algorithm support flag in the given set of flags. If the set is
* empty, {@link #MAX_PRIORITY} is returned.
*
* @param set a set of hash algorithm support flags
* @return the highest priority (lowest value) in the set, or
* {@link #MAX_PRIORITY} if empty
*/
public
static
int
getMaxPriority
(
EnumSet
<
HashSupport
>
set
)
{
if
(
set
.
isEmpty
())
return
MAX_PRIORITY
;
return
set
.
iterator
().
next
().
getPriority
();
}
/**
* Compares the given sets of hash algorithm support flags for priority
* order. The set with the highest priority flag without a flag of matching
* priority in the other set has higher priority.
*
* @param set1 the first set to be compared
* @param set2 the second set to be compared
* @return a negative integer, zero, or a positive integer if the first set
* has priority higher than, equal to, or lower than the second
*/
public
static
int
compare
(
EnumSet
<
HashSupport
>
set1
,
EnumSet
<
HashSupport
>
set2
)
{
// assumes iterators return flags in priority order
final
Iterator
<
HashSupport
>
i1
=
set1
.
iterator
();
final
Iterator
<
HashSupport
>
i2
=
set2
.
iterator
();
int
floor
=
MIN_PRIORITY
;
while
(
i1
.
hasNext
()
||
i2
.
hasNext
())
{
int
p1
,
p2
;
do
{
p1
=
i1
.
hasNext
()
?
i1
.
next
().
getPriority
()
:
MAX_PRIORITY
;
}
while
(
p1
==
floor
);
do
{
p2
=
i2
.
hasNext
()
?
i2
.
next
().
getPriority
()
:
MAX_PRIORITY
;
}
while
(
p2
==
floor
);
if
(
p1
<
p2
)
return
-
1
;
if
(
p1
>
p2
)
return
1
;
floor
=
p1
;
}
return
0
;
}
/**
* {@link Comparator} for {@link EnumSet EnumSets} for hash support flags.
*/
static
final
class
SetComparator
implements
Comparator
<
EnumSet
<
HashSupport
>>
{
@Override
public
int
compare
(
EnumSet
<
HashSupport
>
o1
,
EnumSet
<
HashSupport
>
o2
)
{
return
HashSupport
.
compare
(
o1
,
o2
);
}
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/Hashes.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
java.util.EnumSet
;
import
com.scurrilous.circe.HashParameters
;
import
com.scurrilous.circe.HashProviders
;
import
com.scurrilous.circe.HashSupport
;
import
com.scurrilous.circe.IncrementalIntHash
;
import
com.scurrilous.circe.IncrementalLongHash
;
import
com.scurrilous.circe.StatefulHash
;
import
com.scurrilous.circe.StatelessIntHash
;
import
com.scurrilous.circe.StatelessLongHash
;
/**
* Static methods to obtain various forms of abstract hash functions. Each
* method uses {@link HashProviders#best} to find the best provider for the
* given parameters and hash interface, and then calls the corresponding method
* on that provider.
*/
public
final
class
Hashes
{
private
Hashes
()
{
}
/**
* Creates a stateful hash function using the given parameters.
*
* @param params the hash algorithm parameters
* @return a stateful hash function
* @throws UnsupportedOperationException if no provider supports the
* parameters
*/
public
static
StatefulHash
createStateful
(
HashParameters
params
)
{
return
HashProviders
.
best
(
params
).
createStateful
(
params
);
}
/**
* Requests a stateless, int-width hash function with the given parameters.
* Because not all stateless hash functions are incremental, this method may
* be able to return implementations not supported by or more optimized than
* {@link #getIncrementalInt}.
*
* @param params the hash algorithm parameters
* @return a stateless int-width hash function
* @throws UnsupportedOperationException if no provider supports the
* parameters as a {@link StatelessIntHash}
*/
public
static
StatelessIntHash
getStatelessInt
(
HashParameters
params
)
{
return
HashProviders
.
best
(
params
,
EnumSet
.
of
(
HashSupport
.
INT_SIZED
))
.
getStatelessInt
(
params
);
}
/**
* Requests a stateless, long-width hash function with the given parameters.
* Because not all stateless hash functions are incremental, this method may
* be able to return implementations not supported by or more optimized than
* {@link #getIncrementalLong}.
* <p>
* Note that this method may return a less efficient hash function than
* {@link #getStatelessInt} for hashes of 32 bits or less.
*
* @param params the hash algorithm parameters
* @return a stateless long-width hash function
* @throws UnsupportedOperationException if no provider supports the
* parameters as a {@link StatelessLongHash}
*/
public
static
StatelessLongHash
getStatelessLong
(
HashParameters
params
)
{
return
HashProviders
.
best
(
params
,
EnumSet
.
of
(
HashSupport
.
LONG_SIZED
)).
getStatelessLong
(
params
);
}
/**
* Requests an incremental, stateless, int-width hash function with the
* given parameters. Note that although an algorithm may be available in
* incremental form, some potentially more optimized implementations may not
* support that form, and therefore cannot be provided be this method.
*
* @param params the hash algorithm parameters
* @return a stateful int-width hash function
* @throws UnsupportedOperationException if no provider supports the
* parameters as an {@link IncrementalIntHash}
*/
public
static
IncrementalIntHash
getIncrementalInt
(
HashParameters
params
)
{
return
HashProviders
.
best
(
params
,
EnumSet
.
of
(
HashSupport
.
INT_SIZED
,
HashSupport
.
STATELESS_INCREMENTAL
))
.
getIncrementalInt
(
params
);
}
/**
* Requests an incremental, stateless, long-width hash function with the
* given parameters. Note that although an algorithm may be available in
* incremental form, some potentially more optimized implementations may not
* support that form, and therefore cannot be provided be this method.
* <p>
* Also note that this method may return a less efficient hash function than
* {@link #getIncrementalInt} for hashes of 32 bits or less.
*
* @param params the hash algorithm parameters
* @return a stateful long-width hash function
* @throws UnsupportedOperationException if no provider supports the
* parameters as an {@link IncrementalLongHash}
*/
public
static
IncrementalLongHash
getIncrementalLong
(
HashParameters
params
)
{
return
HashProviders
.
best
(
params
,
EnumSet
.
of
(
HashSupport
.
LONG_SIZED
,
HashSupport
.
STATELESS_INCREMENTAL
))
.
getIncrementalLong
(
params
);
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/IncrementalIntHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
java.nio.ByteBuffer
;
/**
* Incremental stateless integer hash function, which has the property that its
* output is the same as (or easily derivable from) its state. Specifically, for
* any sequence M partitioned arbitrarily into two subsequences M<sub>1</sub>
* M<sub>2</sub>:
*
* <pre>
* h(M) = h'(h(M<sub>1</sub>), M<sub>2</sub>)
* </pre>
*
* where h corresponds to the {@link StatelessIntHash#calculate calculate}
* function and h' corresponds to the {@link #resume} function.
* <p>
* Note that stateful hash functions obtained from incremental stateless hash
* functions are also {@linkplain StatefulHash#supportsIncremental incremental}.
*/
public
interface
IncrementalIntHash
extends
StatelessIntHash
{
/**
* Evaluates this hash function as if the entire given input array were
* appended to the previously hashed input.
*
* @param current the hash output for input hashed so far
* @param input the input array
* @return the output of the hash function for the concatenated input
*/
int
resume
(
int
current
,
byte
[]
input
);
/**
* Evaluates this hash function as if the given range of the given input
* array were appended to the previously hashed input.
*
* @param current the hash output for input hashed so far
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
* @return the output of the hash function for the concatenated input
* @throws IndexOutOfBoundsException if index is negative or
* {@code index + length} is greater than the array length
*/
int
resume
(
int
current
,
byte
[]
input
,
int
index
,
int
length
);
/**
* Evaluates this hash function as if the remaining contents of the given
* input buffer were appended to the previously hashed input. This method
* leaves the buffer position at the limit.
*
* @param current the hash output for input hashed so far
* @param input the input buffer
* @return the output of the hash function for the concatenated input
*/
int
resume
(
int
current
,
ByteBuffer
input
);
/**
* Evaluates this hash function as if the memory with the given address and
* length were appended to the previously hashed input. The arguments are
* generally not checked in any way and will likely lead to a VM crash or
* undefined results if invalid.
*
* @param current the hash output for input hashed so far
* @param address the base address of the input
* @param length the length of the input
* @return the output of the hash function for the concatenated input
* @throws UnsupportedOperationException if this function does not support
* unsafe memory access
* @see #supportsUnsafe()
*/
int
resume
(
int
current
,
long
address
,
long
length
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/IncrementalLongHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
java.nio.ByteBuffer
;
/**
* Incremental stateless long integer hash function, which has the property that
* its output is the same as (or easily derivable from) its state. Specifically,
* for any sequence M partitioned arbitrarily into two subsequences
* M<sub>1</sub> M<sub>2</sub>:
*
* <pre>
* h(M) = h'(h(M<sub>1</sub>), M<sub>2</sub>)
* </pre>
*
* where h corresponds to the {@link StatelessLongHash#calculate calculate}
* function and h' corresponds to the {@link #resume} function.
* <p>
* Note that stateful hash functions obtained from incremental stateless hash
* functions are also {@linkplain StatefulHash#supportsIncremental incremental}.
*/
public
interface
IncrementalLongHash
extends
StatelessLongHash
{
/**
* Evaluates this hash function as if the entire given input array were
* appended to the previously hashed input.
*
* @param current the hash output for input hashed so far
* @param input the input array
* @return the output of the hash function for the concatenated input
*/
long
resume
(
long
current
,
byte
[]
input
);
/**
* Evaluates this hash function as if the given range of the given input
* array were appended to the previously hashed input.
*
* @param current the hash output for input hashed so far
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
* @return the output of the hash function for the concatenated input
* @throws IndexOutOfBoundsException if index is negative or
* {@code index + length} is greater than the array length
*/
long
resume
(
long
current
,
byte
[]
input
,
int
index
,
int
length
);
/**
* Evaluates this hash function as if the remaining contents of the given
* input buffer were appended to the previously hashed input. This method
* leaves the buffer position at the limit.
*
* @param current the hash output for input hashed so far
* @param input the input buffer
* @return the output of the hash function for the concatenated input
*/
long
resume
(
long
current
,
ByteBuffer
input
);
/**
* Evaluates this hash function as if the memory with the given address and
* length were appended to the previously hashed input. The arguments are
* generally not checked in any way and will likely lead to a VM crash or
* undefined results if invalid.
*
* @param current the hash output for input hashed so far
* @param address the base address of the input
* @param length the length of the input
* @return the output of the hash function for the concatenated input
* @throws UnsupportedOperationException if this function does not support
* unsafe memory access
* @see #supportsUnsafe()
*/
long
resume
(
long
current
,
long
address
,
long
length
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/StatefulHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
java.nio.ByteBuffer
;
/**
* Represents a stateful hash function, which can accumulate input from multiple
* method calls and provide output in various forms. Stateful hash functions
* should not be used concurrently from multiple threads.
* <p>
* Stateful hash functions can be incremental or non-incremental. Incremental
* hashing allows calling the {@link #update} methods to continue hashing using
* accumulated state after calling any of the output methods (such as
* {@link #getBytes}). Non-incremental hash functions <i>require</i> calling
* {@link #reset} to reinitialize the state between calling an output method and
* an update method.
*/
public
interface
StatefulHash
extends
Hash
{
/**
* Returns a new instance of this stateful hash function reset to the
* initial state.
*
* @return a new instance of this hash in the initial state
*/
StatefulHash
createNew
();
/**
* Returns whether this hash function supports incremental hashing.
* Incremental hashing allows calling the {@link #update} methods to
* continue hashing using accumulated state after calling any of the output
* methods (such as {@link #getBytes}). Non-incremental hash functions
* require calling {@link #reset} to reinitialize the state between calling
* an output method and an update method.
*
* @return true if incremental hashing is supported, false if not
*/
boolean
supportsIncremental
();
/**
* Resets this hash function to its initial state. Resetting the state is
* required for non-incremental hash functions after any output methods have
* been called.
*/
void
reset
();
/**
* Updates the state of this hash function with the entire given input
* array.
*
* @param input the input array
* @throws IllegalStateException if this hash function is not incremental
* but an output method has been called without an intervening
* call to {@link #reset}
*/
void
update
(
byte
[]
input
);
/**
* Updates the state of this hash function with the given range of the given
* input array.
*
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
* @throws IllegalArgumentException if length is negative
* @throws IndexOutOfBoundsException if index is negative or
* {@code index + length} is greater than the array length
* @throws IllegalStateException if this hash function is not incremental
* but an output method has been called without an intervening
* call to {@link #reset}
*/
void
update
(
byte
[]
input
,
int
index
,
int
length
);
/**
* Updates the state of this hash function with the remaining contents of
* the given input buffer. This method leaves the buffer position at the
* limit.
*
* @param input the input buffer
* @throws IllegalStateException if this hash function is not incremental
* but an output method has been called without an intervening
* call to {@link #reset}
*/
void
update
(
ByteBuffer
input
);
/**
* Updates the state of this hash function with memory with the given
* address and length. The arguments are generally not checked in any way
* and will likely lead to a VM crash or undefined results if invalid.
*
* @param address the base address of the input
* @param length the length of the input
* @throws UnsupportedOperationException if this function does not support
* unsafe memory access
* @throws IllegalStateException if this hash function is not incremental
* but an output method has been called without an intervening
* call to {@link #reset}
* @see #supportsUnsafe()
*/
void
update
(
long
address
,
long
length
);
/**
* Returns a new byte array containing the output of this hash function. The
* caller may freely modify the contents of the array.
*
* @return a new byte array containing the hash output
*/
byte
[]
getBytes
();
/**
* Writes the output of this hash function into the given byte array at the
* given offset.
*
* @param output the destination array for the output
* @param index the starting index of the first output byte
* @param maxLength the maximum number of bytes to write
* @return the number of bytes written
* @throws IllegalArgumentException if {@code maxLength} is negative
* @throws IndexOutOfBoundsException if {@code index} is negative or if
* {@code index + maxLength} is greater than the array length
*/
int
getBytes
(
byte
[]
output
,
int
index
,
int
maxLength
);
/**
* Returns the first byte of the output of this hash function.
*
* @return the first output byte
*/
byte
getByte
();
/**
* Returns the first two bytes of the output of this hash function as a
* little-endian {@code short}. If the output is less than two bytes, the
* remaining bytes are set to 0.
*
* @return the first two output bytes as a short
*/
short
getShort
();
/**
* Returns the first four bytes of the output of this hash function as a
* little-endian {@code int}. If the output is less than four bytes, the
* remaining bytes are set to 0.
*
* @return the first four output bytes as an int
*/
int
getInt
();
/**
* Returns the first eight bytes of the output of this hash function as a
* little-endian {@code long}. If the output is less than eight bytes, the
* remaining bytes are set to 0.
*
* @return the first eight output bytes as a long
*/
long
getLong
();
}
pulsar-checksum/src/main/java/com/scurrilous/circe/StatefulIntHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
/**
* Interface implemented by stateful hash functions with an output length of 4
* bytes or less. Such functions can provide a corresponding stateless
* implementation.
*/
public
interface
StatefulIntHash
extends
StatefulHash
{
/**
* Returns an instance of stateless version of this hash function.
*
* @return the stateless version of this hash function
*/
StatelessIntHash
asStateless
();
}
pulsar-checksum/src/main/java/com/scurrilous/circe/StatefulLongHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
/**
* Interface implemented by stateless hash functions with an output length
* greater than 4 bytes and less than or equal to 8 bytes.
*/
public
interface
StatefulLongHash
extends
StatefulHash
{
/**
* Returns an instance of stateless version of this hash function.
*
* @return the stateless version of this hash function
*/
StatelessLongHash
asStateless
();
}
pulsar-checksum/src/main/java/com/scurrilous/circe/StatelessHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
/**
* Base interface for stateless hash functions that immediately return the hash
* value corresponding to a given input. Stateless hash functions may be used
* concurrently by multiple threads without any synchronization overhead.
*/
public
interface
StatelessHash
extends
Hash
{
/**
* Returns a new instance of stateful version of this hash function.
*
* @return the stateful version of this hash function
*/
StatefulHash
createStateful
();
}
pulsar-checksum/src/main/java/com/scurrilous/circe/StatelessIntHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
java.nio.ByteBuffer
;
/**
* Interface implemented by stateless hash functions with an output length of 4
* bytes or less.
*/
public
interface
StatelessIntHash
extends
StatelessHash
{
/**
* Returns a new instance of stateful version of this hash function.
*
* @return the stateful version of this hash function
*/
@Override
StatefulIntHash
createStateful
();
/**
* Evaluates this hash function for the entire given input array.
*
* @param input the input array
* @return the output of the hash function
*/
int
calculate
(
byte
[]
input
);
/**
* Evaluates this hash function for the given range of the given input
* array.
*
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
* @return the output of the hash function
* @throws IndexOutOfBoundsException if index is negative or
* {@code index + length} is greater than the array length
*/
int
calculate
(
byte
[]
input
,
int
index
,
int
length
);
/**
* Evaluates this hash function with the remaining contents of the given
* input buffer. This method leaves the buffer position at the limit.
*
* @param input the input buffer
* @return the output of the hash function
*/
int
calculate
(
ByteBuffer
input
);
/**
* Evaluates this hash function for the memory with the given address and
* length. The arguments are generally not checked in any way and will
* likely lead to a VM crash or undefined results if invalid.
*
* @param address the base address of the input
* @param length the length of the input
* @return the output of the hash function
* @throws UnsupportedOperationException if this function does not support
* unsafe memory access
* @see #supportsUnsafe()
*/
int
calculate
(
long
address
,
long
length
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/StatelessLongHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
java.nio.ByteBuffer
;
/**
* Interface implemented by stateless hash functions with an output length
* greater than 4 bytes and less than or equal to 8 bytes.
*/
public
interface
StatelessLongHash
extends
StatelessHash
{
/**
* Returns a new instance of stateful version of this hash function.
*
* @return the stateful version of this hash function
*/
@Override
StatefulLongHash
createStateful
();
/**
* Evaluates this hash function for the entire given input array.
*
* @param input the input array
* @return the output of the hash function
*/
long
calculate
(
byte
[]
input
);
/**
* Evaluates this hash function for the given range of the given input
* array.
*
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
* @return the output of the hash function
* @throws IndexOutOfBoundsException if index is negative or
* {@code index + length} is greater than the array length
*/
long
calculate
(
byte
[]
input
,
int
index
,
int
length
);
/**
* Evaluates this hash function with the remaining contents of the given
* input buffer. This method leaves the buffer position at the limit.
*
* @param input the input buffer
* @return the output of the hash function
*/
long
calculate
(
ByteBuffer
input
);
/**
* Evaluates this hash function for the memory with the given address and
* length. The arguments are generally not checked in any way and will
* likely lead to a VM crash or undefined results if invalid.
*
* @param address the base address of the input
* @param length the length of the input
* @return the output of the hash function
* @throws UnsupportedOperationException if this function does not support
* unsafe memory access
* @see #supportsUnsafe()
*/
long
calculate
(
long
address
,
long
length
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/AbstractIntCrc.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
import
com.scurrilous.circe.impl.AbstractIncrementalIntHash
;
/**
* Base implementation of int-width CRC functions.
*/
abstract
class
AbstractIntCrc
extends
AbstractIncrementalIntHash
{
private
final
String
algorithm
;
protected
final
int
bitWidth
;
private
final
int
initial
;
private
final
int
xorOut
;
AbstractIntCrc
(
String
algorithm
,
int
bitWidth
,
int
initial
,
int
xorOut
)
{
if
(
bitWidth
<
1
||
bitWidth
>
32
)
throw
new
IllegalArgumentException
(
"invalid CRC width"
);
this
.
algorithm
=
algorithm
;
this
.
bitWidth
=
bitWidth
;
this
.
initial
=
initial
;
this
.
xorOut
=
xorOut
;
}
@Override
public
String
algorithm
()
{
return
algorithm
;
}
@Override
public
int
length
()
{
return
(
bitWidth
+
7
)
/
8
;
}
@Override
protected
int
initial
()
{
return
initial
^
xorOut
;
}
@Override
protected
int
resumeUnchecked
(
int
current
,
byte
[]
input
,
int
index
,
int
length
)
{
return
resumeRaw
(
current
^
xorOut
,
input
,
index
,
length
)
^
xorOut
;
}
protected
abstract
int
resumeRaw
(
int
crc
,
byte
[]
input
,
int
index
,
int
length
);
protected
final
int
reflect
(
int
value
)
{
return
Integer
.
reverse
(
value
)
>>>
(
32
-
bitWidth
);
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/AbstractLongCrc.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
import
com.scurrilous.circe.impl.AbstractIncrementalLongHash
;
/**
* Base implementation of long-width CRC functions.
*/
abstract
class
AbstractLongCrc
extends
AbstractIncrementalLongHash
{
private
final
String
algorithm
;
protected
final
int
bitWidth
;
private
final
long
initial
;
private
final
long
xorOut
;
AbstractLongCrc
(
String
algorithm
,
int
bitWidth
,
long
initial
,
long
xorOut
)
{
if
(
bitWidth
<
1
||
bitWidth
>
64
)
throw
new
IllegalArgumentException
(
"invalid CRC width"
);
this
.
algorithm
=
algorithm
;
this
.
bitWidth
=
bitWidth
;
this
.
initial
=
initial
;
this
.
xorOut
=
xorOut
;
}
@Override
public
String
algorithm
()
{
return
algorithm
;
}
@Override
public
int
length
()
{
return
(
bitWidth
+
7
)
/
8
;
}
@Override
protected
long
initial
()
{
return
initial
^
xorOut
;
}
@Override
protected
long
resumeUnchecked
(
long
current
,
byte
[]
input
,
int
index
,
int
length
)
{
return
resumeRaw
(
current
^
xorOut
,
input
,
index
,
length
)
^
xorOut
;
}
protected
abstract
long
resumeRaw
(
long
crc
,
byte
[]
input
,
int
index
,
int
length
);
protected
final
long
reflect
(
long
value
)
{
return
Long
.
reverse
(
value
)
>>>
(
64
-
bitWidth
);
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/JavaCrc32.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
import
java.util.zip.CRC32
;
import
com.scurrilous.circe.StatefulHash
;
import
com.scurrilous.circe.StatefulIntHash
;
import
com.scurrilous.circe.StatelessIntHash
;
import
com.scurrilous.circe.impl.AbstractStatefulHash
;
import
com.scurrilous.circe.impl.AbstractStatelessIntHash
;
import
com.scurrilous.circe.params.CrcParameters
;
/**
* Wraps {@link CRC32} in a {@link StatefulIntHash}.
*/
final
class
JavaCrc32
extends
AbstractStatefulHash
implements
StatefulIntHash
{
private
static
final
String
ALGORITHM
=
CrcParameters
.
CRC32
.
algorithm
();
private
static
final
int
LENGTH
=
4
;
private
final
CRC32
impl
=
new
CRC32
();
@Override
public
String
algorithm
()
{
return
ALGORITHM
;
}
@Override
public
int
length
()
{
return
LENGTH
;
}
@Override
public
StatefulHash
createNew
()
{
return
new
JavaCrc32
();
}
@Override
public
boolean
supportsIncremental
()
{
return
true
;
}
@Override
public
void
reset
()
{
impl
.
reset
();
}
@Override
protected
void
updateUnchecked
(
byte
[]
input
,
int
index
,
int
length
)
{
impl
.
update
(
input
,
index
,
length
);
}
@Override
public
int
getInt
()
{
return
(
int
)
impl
.
getValue
();
}
@Override
public
long
getLong
()
{
return
impl
.
getValue
();
}
@Override
public
StatelessIntHash
asStateless
()
{
return
new
AbstractStatelessIntHash
()
{
@Override
public
String
algorithm
()
{
return
ALGORITHM
;
}
@Override
public
int
length
()
{
return
LENGTH
;
}
@Override
public
StatefulIntHash
createStateful
()
{
return
new
JavaCrc32
();
}
@Override
protected
int
calculateUnchecked
(
byte
[]
input
,
int
index
,
int
length
)
{
final
CRC32
crc32
=
new
CRC32
();
crc32
.
update
(
input
,
index
,
length
);
return
(
int
)
crc32
.
getValue
();
}
};
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/NormalByteCrc.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
/**
* Implements a "normal" MSB-first byte-width CRC function using a lookup table.
*/
final
class
NormalByteCrc
extends
AbstractIntCrc
{
private
final
byte
[]
table
=
new
byte
[
256
];
NormalByteCrc
(
String
algorithm
,
int
bitWidth
,
int
poly
,
int
init
,
int
xorOut
)
{
super
(
algorithm
,
bitWidth
,
init
,
xorOut
);
if
(
bitWidth
>
8
)
throw
new
IllegalArgumentException
(
"invalid CRC width"
);
final
int
widthMask
=
(
1
<<
bitWidth
)
-
1
;
final
int
shpoly
=
poly
<<
(
8
-
bitWidth
);
for
(
int
i
=
0
;
i
<
256
;
++
i
)
{
int
crc
=
i
;
for
(
int
j
=
0
;
j
<
8
;
++
j
)
crc
=
(
crc
&
0x80
)
!=
0
?
(
crc
<<
1
)
^
shpoly
:
crc
<<
1
;
table
[
i
]
=
(
byte
)
((
crc
>>
(
8
-
bitWidth
))
&
widthMask
);
}
}
@Override
protected
int
resumeRaw
(
int
crc
,
byte
[]
input
,
int
index
,
int
length
)
{
for
(
int
i
=
0
;
i
<
length
;
++
i
)
crc
=
table
[(
crc
<<
(
8
-
bitWidth
))
^
(
input
[
index
+
i
]
&
0xff
)]
&
0xff
;
return
crc
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/NormalIntCrc.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
/**
* Implements a "normal" MSB-first int-width CRC function using a lookup table.
* Does not support bit-widths less than 8.
*/
final
class
NormalIntCrc
extends
AbstractIntCrc
{
private
final
int
widthMask
;
private
final
int
[]
table
=
new
int
[
256
];
NormalIntCrc
(
String
algorithm
,
int
bitWidth
,
int
poly
,
int
init
,
int
xorOut
)
{
super
(
algorithm
,
bitWidth
,
init
,
xorOut
);
if
(
bitWidth
<
8
)
throw
new
IllegalArgumentException
(
"invalid CRC width"
);
widthMask
=
bitWidth
<
32
?
((
1
<<
bitWidth
)
-
1
)
:
~
0
;
final
int
top
=
1
<<
(
bitWidth
-
1
);
for
(
int
i
=
0
;
i
<
256
;
++
i
)
{
int
crc
=
i
<<
(
bitWidth
-
8
);
for
(
int
j
=
0
;
j
<
8
;
++
j
)
crc
=
(
crc
&
top
)
!=
0
?
(
crc
<<
1
)
^
poly
:
crc
<<
1
;
table
[
i
]
=
crc
&
widthMask
;
}
}
@Override
protected
int
resumeRaw
(
int
crc
,
byte
[]
input
,
int
index
,
int
length
)
{
for
(
int
i
=
0
;
i
<
length
;
++
i
)
crc
=
table
[((
crc
>>>
(
bitWidth
-
8
))
^
input
[
index
+
i
])
&
0xff
]
^
(
crc
<<
8
);
return
crc
&
widthMask
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/NormalLongCrc.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
/**
* Implements a "normal" MSB-first long-width CRC function using a lookup table.
* Does not support bit-widths less than 8.
*/
final
class
NormalLongCrc
extends
AbstractLongCrc
{
private
final
long
widthMask
;
private
final
long
[]
table
=
new
long
[
256
];
NormalLongCrc
(
String
algorithm
,
int
bitWidth
,
long
poly
,
long
init
,
long
xorOut
)
{
super
(
algorithm
,
bitWidth
,
init
,
xorOut
);
if
(
bitWidth
<
8
)
throw
new
IllegalArgumentException
(
"invalid CRC width"
);
widthMask
=
bitWidth
<
64
?
((
1L
<<
bitWidth
)
-
1
)
:
~
0L
;
final
long
top
=
1L
<<
(
bitWidth
-
1
);
for
(
int
i
=
0
;
i
<
256
;
++
i
)
{
long
crc
=
(
long
)
i
<<
(
bitWidth
-
8
);
for
(
int
j
=
0
;
j
<
8
;
++
j
)
crc
=
(
crc
&
top
)
!=
0
?
(
crc
<<
1
)
^
poly
:
crc
<<
1
;
table
[
i
]
=
crc
&
widthMask
;
}
}
@Override
protected
long
resumeRaw
(
long
crc
,
byte
[]
input
,
int
index
,
int
length
)
{
for
(
int
i
=
0
;
i
<
length
;
++
i
)
crc
=
table
[(
int
)
((
crc
>>>
(
bitWidth
-
8
))
^
input
[
index
+
i
])
&
0xff
]
^
(
crc
<<
8
);
return
crc
&
widthMask
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/ReflectedIntCrc.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
/**
* Implements a "reflected" LSB-first int-width CRC function using a lookup
* table.
*/
final
class
ReflectedIntCrc
extends
AbstractIntCrc
{
private
final
int
[]
table
=
new
int
[
256
];
ReflectedIntCrc
(
String
algorithm
,
int
width
,
int
poly
,
int
init
,
int
xorOut
)
{
super
(
algorithm
,
width
,
init
,
xorOut
);
poly
=
reflect
(
poly
);
for
(
int
i
=
0
;
i
<
256
;
++
i
)
{
int
crc
=
i
;
for
(
int
j
=
0
;
j
<
8
;
++
j
)
crc
=
(
crc
&
1
)
!=
0
?
(
crc
>>>
1
)
^
poly
:
crc
>>>
1
;
table
[
i
]
=
crc
;
}
}
@Override
protected
int
resumeRaw
(
int
crc
,
byte
[]
input
,
int
index
,
int
length
)
{
crc
=
reflect
(
crc
);
for
(
int
i
=
0
;
i
<
length
;
++
i
)
crc
=
table
[(
crc
^
input
[
index
+
i
])
&
0xff
]
^
(
crc
>>>
8
);
return
crc
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/ReflectedLongCrc.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
/**
* Implements a "reflected" LSB-first long-width CRC function using a lookup
* table.
*/
final
class
ReflectedLongCrc
extends
AbstractLongCrc
{
private
final
long
[]
table
=
new
long
[
256
];
ReflectedLongCrc
(
String
algorithm
,
int
width
,
long
poly
,
long
init
,
long
xorOut
)
{
super
(
algorithm
,
width
,
init
,
xorOut
);
poly
=
reflect
(
poly
);
for
(
int
i
=
0
;
i
<
256
;
++
i
)
{
long
crc
=
i
;
for
(
int
j
=
0
;
j
<
8
;
++
j
)
crc
=
(
crc
&
1
)
!=
0
?
(
crc
>>>
1
)
^
poly
:
crc
>>>
1
;
table
[
i
]
=
crc
;
}
}
@Override
protected
long
resumeRaw
(
long
crc
,
byte
[]
input
,
int
index
,
int
length
)
{
crc
=
reflect
(
crc
);
for
(
int
i
=
0
;
i
<
length
;
++
i
)
crc
=
table
[(
int
)
(
crc
^
input
[
index
+
i
])
&
0xff
]
^
(
crc
>>>
8
);
return
crc
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/StandardCrcProvider.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
import
java.util.EnumSet
;
import
com.scurrilous.circe.Hash
;
import
com.scurrilous.circe.HashSupport
;
import
com.scurrilous.circe.StatelessHash
;
import
com.scurrilous.circe.impl.AbstractHashProvider
;
import
com.scurrilous.circe.params.CrcParameters
;
/**
* Provides pure Java and JDK-supplied CRC implementations.
*/
public
final
class
StandardCrcProvider
extends
AbstractHashProvider
<
CrcParameters
>
{
/**
* Constructs a new {@link StandardCrcProvider}.
*/
public
StandardCrcProvider
()
{
super
(
CrcParameters
.
class
);
}
@Override
protected
EnumSet
<
HashSupport
>
querySupportTyped
(
CrcParameters
params
)
{
final
EnumSet
<
HashSupport
>
result
=
EnumSet
.
of
(
HashSupport
.
STATEFUL
,
HashSupport
.
INCREMENTAL
,
HashSupport
.
STATELESS_INCREMENTAL
,
HashSupport
.
LONG_SIZED
);
if
(
params
.
bitWidth
()
<=
32
)
result
.
add
(
HashSupport
.
INT_SIZED
);
if
(
params
.
equals
(
CrcParameters
.
CRC32
))
result
.
add
(
HashSupport
.
NATIVE
);
return
result
;
}
@Override
protected
Hash
get
(
CrcParameters
params
,
EnumSet
<
HashSupport
>
required
)
{
if
(!
required
.
contains
(
HashSupport
.
STATELESS_INCREMENTAL
)
&&
params
.
equals
(
CrcParameters
.
CRC32
))
return
new
JavaCrc32
();
if
(
required
.
contains
(
HashSupport
.
NATIVE
))
throw
new
UnsupportedOperationException
();
return
getCacheable
(
params
,
required
);
}
@Override
protected
StatelessHash
createCacheable
(
CrcParameters
params
,
EnumSet
<
HashSupport
>
required
)
{
final
int
bitWidth
=
params
.
bitWidth
();
if
(
bitWidth
>
32
||
(
required
.
contains
(
HashSupport
.
LONG_SIZED
)
&&
bitWidth
>=
8
))
{
if
(
required
.
contains
(
HashSupport
.
INT_SIZED
))
throw
new
UnsupportedOperationException
();
if
(
params
.
reflected
())
return
new
ReflectedLongCrc
(
params
.
algorithm
(),
bitWidth
,
params
.
polynomial
(),
params
.
initial
(),
params
.
xorOut
());
else
return
new
NormalLongCrc
(
params
.
algorithm
(),
bitWidth
,
params
.
polynomial
(),
params
.
initial
(),
params
.
xorOut
());
}
else
{
if
(
params
.
reflected
())
return
new
ReflectedIntCrc
(
params
.
algorithm
(),
bitWidth
,
(
int
)
params
.
polynomial
(),
(
int
)
params
.
initial
(),
(
int
)
params
.
xorOut
());
else
if
(
bitWidth
>
8
)
return
new
NormalIntCrc
(
params
.
algorithm
(),
bitWidth
,
(
int
)
params
.
polynomial
(),
(
int
)
params
.
initial
(),
(
int
)
params
.
xorOut
());
return
new
NormalByteCrc
(
params
.
algorithm
(),
bitWidth
,
(
int
)
params
.
polynomial
(),
(
int
)
params
.
initial
(),
(
int
)
params
.
xorOut
());
}
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/crc/package-info.java
0 → 100644
浏览文件 @
554f0505
/**
* Provides various implementations of <a
* href="http://en.wikipedia.org/wiki/Cyclic_redundancy_check">cyclic redundancy
* check</a> (CRC) error-detecting codes. CRC values are based on the remainder
* of a polynomial division of the input data. They are particularly well-suited
* to detecting burst errors in data sent over telecommunications channels or
* retrieved from storage media.
*/
package
com.scurrilous.circe.crc
;
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractHashProvider.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.util.EnumSet
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutionException
;
import
com.scurrilous.circe.Hash
;
import
com.scurrilous.circe.HashParameters
;
import
com.scurrilous.circe.HashProvider
;
import
com.scurrilous.circe.HashSupport
;
import
com.scurrilous.circe.IncrementalIntHash
;
import
com.scurrilous.circe.IncrementalLongHash
;
import
com.scurrilous.circe.StatefulHash
;
import
com.scurrilous.circe.StatefulIntHash
;
import
com.scurrilous.circe.StatefulLongHash
;
import
com.scurrilous.circe.StatelessHash
;
import
com.scurrilous.circe.StatelessIntHash
;
import
com.scurrilous.circe.StatelessLongHash
;
/**
* Base implementation for hash function providers.
*
* @param <P> base supported hash parameters type
*/
public
abstract
class
AbstractHashProvider
<
P
extends
HashParameters
>
implements
HashProvider
{
private
final
Class
<
P
>
parametersClass
;
/**
* Constructs a new {@link AbstractHashProvider} with the given base
* parameters class.
*
* @param parametersClass the base hash parameters class supported
*/
protected
AbstractHashProvider
(
Class
<
P
>
parametersClass
)
{
this
.
parametersClass
=
parametersClass
;
}
@Override
public
final
EnumSet
<
HashSupport
>
querySupport
(
HashParameters
params
)
{
if
(!
parametersClass
.
isAssignableFrom
(
params
.
getClass
()))
return
EnumSet
.
noneOf
(
HashSupport
.
class
);
return
querySupportTyped
(
parametersClass
.
cast
(
params
));
}
/**
* Implemented by subclasses to provide information about the available
* implementations corresponding to the given hash algorithm parameters.
* Called by {@link #querySupport} if the hash parameters match the base
* type supported by this provider.
*
* @param params the hash algorithm parameters
* @return a set of flags indicating the level of support
*/
protected
abstract
EnumSet
<
HashSupport
>
querySupportTyped
(
P
params
);
/**
* Requests a hash function using the given parameters and support flags.
* This method is only responsible for checking support flags returned by
* {@link #querySupportTyped}.
* <p>
* To support caching of stateless hash functions, call
* {@link #getCacheable} from this method and implement
* {@link #createCacheable}.
*
* @param params the hash algorithm parameters
* @param required the required hash support flags
* @return a hash function
* @throws UnsupportedOperationException if this provider cannot support the
* given parameters
*/
protected
abstract
Hash
get
(
P
params
,
EnumSet
<
HashSupport
>
required
);
/**
* Called by implementations that support caching of stateless hash
* functions when a cached instance is desired. If a cached instance is not
* available, this method calls {@link #createCacheable} to create one,
* which is then cached (if caching is available).
*
* @param params the hash algorithm parameters
* @param required the required hash support flags
* @return a hash function
* @throws UnsupportedOperationException if this provider cannot support the
* given parameters
*/
protected
final
Hash
getCacheable
(
final
P
params
,
final
EnumSet
<
HashSupport
>
required
)
{
if
(
HashCacheLoader
.
hasCache
())
{
final
HashCache
cache
=
HashCacheLoader
.
getCache
();
try
{
return
cache
.
get
(
params
,
required
,
new
Callable
<
Hash
>()
{
@Override
public
Hash
call
()
throws
Exception
{
return
createCacheable
(
params
,
required
);
}
});
}
catch
(
ExecutionException
e
)
{
final
Throwable
cause
=
e
.
getCause
();
if
(
cause
instanceof
RuntimeException
)
throw
(
RuntimeException
)
cause
;
throw
new
UnsupportedOperationException
(
e
);
}
}
return
createCacheable
(
params
,
required
);
}
/**
* Called by {@link #getCacheable} to create new cacheable stateless hash
* functions. The default implementation simply throws
* {@link UnsupportedOperationException}.
*
* @param params the hash algorithm parameters
* @param required the required hash support flags
* @return a stateless hash function
* @throws UnsupportedOperationException if this provider cannot support the
* given parameters
*/
protected
StatelessHash
createCacheable
(
P
params
,
EnumSet
<
HashSupport
>
required
)
{
throw
new
UnsupportedOperationException
();
}
private
Hash
castAndGet
(
HashParameters
params
,
EnumSet
<
HashSupport
>
required
)
{
if
(!
parametersClass
.
isAssignableFrom
(
params
.
getClass
()))
throw
new
UnsupportedOperationException
();
return
get
(
parametersClass
.
cast
(
params
),
required
);
}
@Override
public
StatefulHash
createStateful
(
HashParameters
params
)
{
final
Hash
hash
=
castAndGet
(
params
,
EnumSet
.
of
(
HashSupport
.
STATEFUL
));
if
(
hash
instanceof
StatefulHash
)
return
(
StatefulHash
)
hash
;
if
(
hash
instanceof
StatelessHash
)
return
((
StatelessHash
)
hash
).
createStateful
();
throw
new
UnsupportedOperationException
();
}
@Override
public
StatelessIntHash
getStatelessInt
(
HashParameters
params
)
{
final
Hash
hash
=
castAndGet
(
params
,
EnumSet
.
of
(
HashSupport
.
INT_SIZED
));
if
(
hash
instanceof
StatelessIntHash
)
return
(
StatelessIntHash
)
hash
;
if
(
hash
instanceof
StatefulIntHash
)
return
((
StatefulIntHash
)
hash
).
asStateless
();
throw
new
UnsupportedOperationException
();
}
@Override
public
StatelessLongHash
getStatelessLong
(
HashParameters
params
)
{
final
Hash
hash
=
castAndGet
(
params
,
EnumSet
.
of
(
HashSupport
.
LONG_SIZED
));
if
(
hash
instanceof
StatelessLongHash
)
return
(
StatelessLongHash
)
hash
;
if
(
hash
instanceof
StatefulLongHash
)
return
((
StatefulLongHash
)
hash
).
asStateless
();
if
(
hash
instanceof
StatelessIntHash
)
return
new
IntStatelessLongHash
((
StatelessIntHash
)
hash
);
if
(
hash
instanceof
StatefulIntHash
)
return
new
IntStatelessLongHash
(((
StatefulIntHash
)
hash
).
asStateless
());
throw
new
UnsupportedOperationException
();
}
@Override
public
IncrementalIntHash
getIncrementalInt
(
HashParameters
params
)
{
final
Hash
hash
=
castAndGet
(
params
,
EnumSet
.
of
(
HashSupport
.
INT_SIZED
,
HashSupport
.
STATELESS_INCREMENTAL
));
if
(
hash
instanceof
IncrementalIntHash
)
return
(
IncrementalIntHash
)
hash
;
throw
new
UnsupportedOperationException
();
}
@Override
public
IncrementalLongHash
getIncrementalLong
(
HashParameters
params
)
{
final
Hash
hash
=
castAndGet
(
params
,
EnumSet
.
of
(
HashSupport
.
LONG_SIZED
,
HashSupport
.
STATELESS_INCREMENTAL
));
if
(
hash
instanceof
IncrementalLongHash
)
return
(
IncrementalLongHash
)
hash
;
throw
new
UnsupportedOperationException
();
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractIncrementalIntHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
com.scurrilous.circe.IncrementalIntHash
;
import
com.scurrilous.circe.StatefulIntHash
;
/**
* Base implementation for incremental stateless integer hash functions.
*/
public
abstract
class
AbstractIncrementalIntHash
implements
IncrementalIntHash
{
@Override
public
boolean
supportsUnsafe
()
{
return
false
;
}
@Override
public
StatefulIntHash
createStateful
()
{
return
new
IncrementalIntStatefulHash
(
this
);
}
@Override
public
int
calculate
(
byte
[]
input
)
{
return
resume
(
initial
(),
input
);
}
@Override
public
int
calculate
(
byte
[]
input
,
int
index
,
int
length
)
{
return
resume
(
initial
(),
input
,
index
,
length
);
}
@Override
public
int
calculate
(
ByteBuffer
input
)
{
return
resume
(
initial
(),
input
);
}
@Override
public
int
calculate
(
long
address
,
long
length
)
{
return
resume
(
initial
(),
address
,
length
);
}
@Override
public
int
resume
(
int
current
,
byte
[]
input
)
{
return
resumeUnchecked
(
current
,
input
,
0
,
input
.
length
);
}
@Override
public
int
resume
(
int
current
,
byte
[]
input
,
int
index
,
int
length
)
{
if
(
length
<
0
)
throw
new
IllegalArgumentException
();
if
(
index
<
0
||
index
+
length
>
input
.
length
)
throw
new
IndexOutOfBoundsException
();
return
resumeUnchecked
(
current
,
input
,
index
,
length
);
}
@Override
public
int
resume
(
int
current
,
ByteBuffer
input
)
{
final
byte
[]
array
;
final
int
index
;
final
int
length
=
input
.
remaining
();
if
(
input
.
hasArray
())
{
array
=
input
.
array
();
index
=
input
.
arrayOffset
()
+
input
.
position
();
input
.
position
(
input
.
limit
());
}
else
{
array
=
new
byte
[
length
];
index
=
0
;
input
.
get
(
array
);
}
return
resumeUnchecked
(
current
,
array
,
index
,
length
);
}
@Override
public
int
resume
(
int
current
,
long
address
,
long
length
)
{
throw
new
UnsupportedOperationException
();
}
/**
* The initial state of the hash function, which is the same as the output
* value for an empty input sequence.
*
* @return the initial hash state/output
*/
protected
abstract
int
initial
();
/**
* Evaluates this hash function as if the given range of the given input
* array were appended to the previously hashed input. The index and length
* parameters have already been validated.
*
* @param current the hash output for input hashed so far
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
* @return the output of the hash function for the concatenated input
*/
protected
abstract
int
resumeUnchecked
(
int
current
,
byte
[]
input
,
int
index
,
int
length
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractIncrementalLongHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
com.scurrilous.circe.IncrementalLongHash
;
import
com.scurrilous.circe.StatefulLongHash
;
/**
* Base implementation for incremental stateless long integer hash functions.
*/
public
abstract
class
AbstractIncrementalLongHash
implements
IncrementalLongHash
{
@Override
public
boolean
supportsUnsafe
()
{
return
false
;
}
@Override
public
StatefulLongHash
createStateful
()
{
return
new
IncrementalLongStatefulHash
(
this
);
}
@Override
public
long
calculate
(
byte
[]
input
)
{
return
resume
(
initial
(),
input
);
}
@Override
public
long
calculate
(
byte
[]
input
,
int
index
,
int
length
)
{
return
resume
(
initial
(),
input
,
index
,
length
);
}
@Override
public
long
calculate
(
ByteBuffer
input
)
{
return
resume
(
initial
(),
input
);
}
@Override
public
long
calculate
(
long
address
,
long
length
)
{
return
resume
(
initial
(),
address
,
length
);
}
@Override
public
long
resume
(
long
current
,
byte
[]
input
)
{
return
resumeUnchecked
(
current
,
input
,
0
,
input
.
length
);
}
@Override
public
long
resume
(
long
current
,
byte
[]
input
,
int
index
,
int
length
)
{
if
(
length
<
0
)
throw
new
IllegalArgumentException
();
if
(
index
<
0
||
index
+
length
>
input
.
length
)
throw
new
IndexOutOfBoundsException
();
return
resumeUnchecked
(
current
,
input
,
index
,
length
);
}
@Override
public
long
resume
(
long
current
,
ByteBuffer
input
)
{
final
byte
[]
array
;
final
int
index
;
final
int
length
=
input
.
remaining
();
if
(
input
.
hasArray
())
{
array
=
input
.
array
();
index
=
input
.
arrayOffset
()
+
input
.
position
();
input
.
position
(
input
.
limit
());
}
else
{
array
=
new
byte
[
length
];
index
=
0
;
input
.
get
(
array
);
}
return
resumeUnchecked
(
current
,
array
,
index
,
length
);
}
@Override
public
long
resume
(
long
current
,
long
address
,
long
length
)
{
throw
new
UnsupportedOperationException
();
}
/**
* The initial state of the hash function, which is the same as the output
* value for an empty input sequence.
*
* @return the initial hash state/output
*/
protected
abstract
long
initial
();
/**
* Evaluates this hash function as if the given range of the given input
* array were appended to the previously hashed input. The index and length
* parameters have already been validated.
*
* @param current the hash output for input hashed so far
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
* @return the output of the hash function for the concatenated input
*/
protected
abstract
long
resumeUnchecked
(
long
current
,
byte
[]
input
,
int
index
,
int
length
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractStatefulHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
com.scurrilous.circe.StatefulHash
;
/**
* Base implementation for stateful hash functions.
*/
public
abstract
class
AbstractStatefulHash
implements
StatefulHash
{
@Override
public
boolean
supportsUnsafe
()
{
return
false
;
}
@Override
public
void
update
(
byte
[]
input
)
{
updateUnchecked
(
input
,
0
,
input
.
length
);
}
@Override
public
void
update
(
byte
[]
input
,
int
index
,
int
length
)
{
if
(
length
<
0
)
throw
new
IllegalArgumentException
();
if
(
index
<
0
||
index
+
length
>
input
.
length
)
throw
new
IndexOutOfBoundsException
();
updateUnchecked
(
input
,
index
,
length
);
}
@Override
public
void
update
(
ByteBuffer
input
)
{
final
byte
[]
array
;
final
int
index
;
final
int
length
=
input
.
remaining
();
if
(
input
.
hasArray
())
{
array
=
input
.
array
();
index
=
input
.
arrayOffset
()
+
input
.
position
();
input
.
position
(
input
.
limit
());
}
else
{
// convert to unsafe access if possible
if
(
input
.
isDirect
()
&&
supportsUnsafe
())
{
long
address
=
DirectByteBufferAccessLoader
.
getAddress
(
input
);
if
(
address
!=
0
)
{
address
+=
input
.
position
();
input
.
position
(
input
.
limit
());
update
(
address
,
length
);
return
;
}
}
array
=
new
byte
[
length
];
index
=
0
;
input
.
get
(
array
);
}
updateUnchecked
(
array
,
index
,
length
);
}
@Override
public
void
update
(
long
address
,
long
length
)
{
throw
new
UnsupportedOperationException
();
}
/**
* Updates the state of this hash function with the given range of the given
* input array. The index and length parameters have already been validated.
*
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
*/
protected
abstract
void
updateUnchecked
(
byte
[]
input
,
int
index
,
int
length
);
@Override
public
byte
[]
getBytes
()
{
final
byte
[]
array
=
new
byte
[
length
()];
writeBytes
(
array
,
0
,
array
.
length
);
return
array
;
}
@Override
public
int
getBytes
(
byte
[]
output
,
int
index
,
int
maxLength
)
{
if
(
maxLength
<
0
)
throw
new
IllegalArgumentException
();
if
(
index
<
0
||
index
+
maxLength
>
output
.
length
)
throw
new
IndexOutOfBoundsException
();
final
int
length
=
Math
.
min
(
maxLength
,
length
());
writeBytes
(
output
,
index
,
length
);
return
length
;
}
/**
* Writes the output of this hash function into the given range of the given
* byte array. The inputs have already been validated.
*
* @param output the destination array for the output
* @param index the starting index of the first output byte
* @param length the number of bytes to write
*/
protected
void
writeBytes
(
byte
[]
output
,
int
index
,
int
length
)
{
long
temp
=
getLong
();
for
(
int
i
=
0
;
i
<
length
;
++
i
)
{
output
[
index
+
i
]
=
(
byte
)
temp
;
temp
>>>=
8
;
}
}
@Override
public
byte
getByte
()
{
return
(
byte
)
getInt
();
}
@Override
public
short
getShort
()
{
return
(
short
)
getInt
();
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractStatelessIntHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
com.scurrilous.circe.StatelessIntHash
;
/**
* Base implementation for stateless (but not incremental) integer hash
* functions.
*/
public
abstract
class
AbstractStatelessIntHash
implements
StatelessIntHash
{
@Override
public
boolean
supportsUnsafe
()
{
return
false
;
}
@Override
public
int
calculate
(
byte
[]
input
)
{
return
calculateUnchecked
(
input
,
0
,
input
.
length
);
}
@Override
public
int
calculate
(
byte
[]
input
,
int
index
,
int
length
)
{
if
(
length
<
0
)
throw
new
IllegalArgumentException
();
if
(
index
<
0
||
index
+
length
>
input
.
length
)
throw
new
IndexOutOfBoundsException
();
return
calculateUnchecked
(
input
,
index
,
length
);
}
@Override
public
int
calculate
(
ByteBuffer
input
)
{
final
byte
[]
array
;
final
int
index
;
final
int
length
=
input
.
remaining
();
if
(
input
.
hasArray
())
{
array
=
input
.
array
();
index
=
input
.
arrayOffset
()
+
input
.
position
();
input
.
position
(
input
.
limit
());
}
else
{
array
=
new
byte
[
length
];
index
=
0
;
input
.
get
(
array
);
}
return
calculateUnchecked
(
array
,
index
,
length
);
}
@Override
public
int
calculate
(
long
address
,
long
length
)
{
throw
new
UnsupportedOperationException
();
}
/**
* Evaluates this hash function for the given range of the given input
* array. The index and length parameters have already been validated.
*
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
* @return the output of the hash function
*/
protected
abstract
int
calculateUnchecked
(
byte
[]
input
,
int
index
,
int
length
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/AbstractStatelessLongHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
com.scurrilous.circe.StatelessLongHash
;
/**
* Base implementation for stateless (but not incremental) long integer hash
* functions.
*/
public
abstract
class
AbstractStatelessLongHash
implements
StatelessLongHash
{
@Override
public
boolean
supportsUnsafe
()
{
return
false
;
}
@Override
public
long
calculate
(
byte
[]
input
)
{
return
calculateUnchecked
(
input
,
0
,
input
.
length
);
}
@Override
public
long
calculate
(
byte
[]
input
,
int
index
,
int
length
)
{
if
(
length
<
0
)
throw
new
IllegalArgumentException
();
if
(
index
<
0
||
index
+
length
>
input
.
length
)
throw
new
IndexOutOfBoundsException
();
return
calculateUnchecked
(
input
,
index
,
length
);
}
@Override
public
long
calculate
(
ByteBuffer
input
)
{
final
byte
[]
array
;
final
int
index
;
final
int
length
=
input
.
remaining
();
if
(
input
.
hasArray
())
{
array
=
input
.
array
();
index
=
input
.
arrayOffset
()
+
input
.
position
();
input
.
position
(
input
.
limit
());
}
else
{
array
=
new
byte
[
length
];
index
=
0
;
input
.
get
(
array
);
}
return
calculateUnchecked
(
array
,
index
,
length
);
}
@Override
public
long
calculate
(
long
address
,
long
length
)
{
throw
new
UnsupportedOperationException
();
}
/**
* Evaluates this hash function for the given range of the given input
* array. The index and length parameters have already been validated.
*
* @param input the input array
* @param index the starting index of the first input byte
* @param length the length of the input range
* @return the output of the hash function
*/
protected
abstract
long
calculateUnchecked
(
byte
[]
input
,
int
index
,
int
length
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/DirectByteBufferAccess.java
0 → 100644
浏览文件 @
554f0505
package
com.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
/**
* Service used to provide the native memory address of direct byte buffers.
*/
public
interface
DirectByteBufferAccess
{
/**
* Returns the native memory address of the given direct byte buffer, or 0
* if the buffer is not direct or if obtaining the address is not supported.
*
* @param buffer the direct byte buffer for which to obtain the address
* @return the native memory address or 0
*/
long
getAddress
(
ByteBuffer
buffer
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/DirectByteBufferAccessLoader.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
java.util.Iterator
;
import
java.util.ServiceLoader
;
/**
* Provides access to a singleton {@link DirectByteBufferAccess} implementation,
* if one is available.
*/
public
final
class
DirectByteBufferAccessLoader
{
private
static
final
DirectByteBufferAccess
INSTANCE
;
static
{
final
Iterator
<
DirectByteBufferAccess
>
iterator
=
ServiceLoader
.
load
(
DirectByteBufferAccess
.
class
).
iterator
();
INSTANCE
=
iterator
.
hasNext
()
?
iterator
.
next
()
:
null
;
}
/**
* Returns the native memory address of the given direct byte buffer, or 0
* if the buffer is not direct or if obtaining the address is not supported.
*
* @param buffer the direct byte buffer for which to obtain the address
* @return the native memory address or 0
*/
public
static
long
getAddress
(
ByteBuffer
buffer
)
{
return
INSTANCE
!=
null
?
INSTANCE
.
getAddress
(
buffer
)
:
0
;
}
private
DirectByteBufferAccessLoader
()
{
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/HashCache.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.util.EnumSet
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutionException
;
import
com.scurrilous.circe.Hash
;
import
com.scurrilous.circe.HashParameters
;
import
com.scurrilous.circe.HashSupport
;
/**
* Interface implemented by hash function caches.
*/
public
interface
HashCache
{
/**
* Requests a cached hash function with the given parameters and required
* support flags. If no matching function is cached, the given loader is
* called to obtain one to cache.
*
* @param params the hash algorithm parameters
* @param required the required hash support flags
* @param loader a cache loader that creates the function if not cached
* @return a hash with the given parameters and support flags
* @throws ExecutionException if the loader throws an exception
*/
Hash
get
(
HashParameters
params
,
EnumSet
<
HashSupport
>
required
,
Callable
<
Hash
>
loader
)
throws
ExecutionException
;
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/HashCacheLoader.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.util.Iterator
;
import
java.util.ServiceLoader
;
/**
* Provides access to a singleton hash function cache, if an implementation is
* available.
*/
public
final
class
HashCacheLoader
{
private
static
final
HashCache
HASH_CACHE
;
static
{
final
Iterator
<
HashCache
>
iterator
=
ServiceLoader
.
load
(
HashCache
.
class
).
iterator
();
HASH_CACHE
=
iterator
.
hasNext
()
?
iterator
.
next
()
:
null
;
}
/**
* Returns whether a hash function cache is available.
*
* @return true if a cache is available, false if {@link #getCache} will
* throw an exception
*/
public
static
boolean
hasCache
()
{
return
HASH_CACHE
!=
null
;
}
/**
* Returns the single hash function cache.
*
* @return the single hash cache
* @throws UnsupportedOperationException if no hash cache is available
*/
public
static
HashCache
getCache
()
{
if
(
HASH_CACHE
==
null
)
throw
new
UnsupportedOperationException
();
return
HASH_CACHE
;
}
private
HashCacheLoader
()
{
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/IncrementalIntStatefulHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
com.scurrilous.circe.StatefulHash
;
import
com.scurrilous.circe.StatefulIntHash
;
import
com.scurrilous.circe.StatelessIntHash
;
class
IncrementalIntStatefulHash
extends
AbstractStatefulHash
implements
StatefulIntHash
{
final
AbstractIncrementalIntHash
stateless
;
int
current
;
IncrementalIntStatefulHash
(
AbstractIncrementalIntHash
stateless
)
{
this
.
stateless
=
stateless
;
}
@Override
public
StatelessIntHash
asStateless
()
{
return
stateless
;
}
@Override
public
String
algorithm
()
{
return
stateless
.
algorithm
();
}
@Override
public
int
length
()
{
return
stateless
.
length
();
}
@Override
public
boolean
supportsUnsafe
()
{
return
stateless
.
supportsUnsafe
();
}
@Override
public
StatefulHash
createNew
()
{
return
new
IncrementalIntStatefulHash
(
stateless
);
}
@Override
public
boolean
supportsIncremental
()
{
return
true
;
}
@Override
public
void
reset
()
{
current
=
stateless
.
initial
();
}
@Override
public
void
update
(
ByteBuffer
input
)
{
current
=
stateless
.
resume
(
current
,
input
);
}
@Override
public
void
update
(
long
address
,
long
length
)
{
current
=
stateless
.
resume
(
current
,
address
,
length
);
}
@Override
protected
void
updateUnchecked
(
byte
[]
input
,
int
index
,
int
length
)
{
current
=
stateless
.
resumeUnchecked
(
current
,
input
,
index
,
length
);
}
@Override
public
int
getInt
()
{
return
current
;
}
@Override
public
long
getLong
()
{
return
current
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/IncrementalLongStatefulHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
com.scurrilous.circe.StatefulHash
;
import
com.scurrilous.circe.StatefulLongHash
;
import
com.scurrilous.circe.StatelessLongHash
;
class
IncrementalLongStatefulHash
extends
AbstractStatefulHash
implements
StatefulLongHash
{
final
AbstractIncrementalLongHash
stateless
;
long
current
;
IncrementalLongStatefulHash
(
AbstractIncrementalLongHash
stateless
)
{
this
.
stateless
=
stateless
;
}
@Override
public
StatelessLongHash
asStateless
()
{
return
stateless
;
}
@Override
public
String
algorithm
()
{
return
stateless
.
algorithm
();
}
@Override
public
int
length
()
{
return
stateless
.
length
();
}
@Override
public
boolean
supportsUnsafe
()
{
return
stateless
.
supportsUnsafe
();
}
@Override
public
StatefulHash
createNew
()
{
return
new
IncrementalLongStatefulHash
(
stateless
);
}
@Override
public
boolean
supportsIncremental
()
{
return
true
;
}
@Override
public
void
reset
()
{
current
=
stateless
.
initial
();
}
@Override
public
void
update
(
ByteBuffer
input
)
{
current
=
stateless
.
resume
(
current
,
input
);
}
@Override
public
void
update
(
long
address
,
long
length
)
{
current
=
stateless
.
resume
(
current
,
address
,
length
);
}
@Override
protected
void
updateUnchecked
(
byte
[]
input
,
int
index
,
int
length
)
{
current
=
stateless
.
resumeUnchecked
(
current
,
input
,
index
,
length
);
}
@Override
public
int
getInt
()
{
return
(
int
)
current
;
}
@Override
public
long
getLong
()
{
return
current
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/IntStatefulLongHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
com.scurrilous.circe.StatefulHash
;
import
com.scurrilous.circe.StatefulIntHash
;
import
com.scurrilous.circe.StatefulLongHash
;
import
com.scurrilous.circe.StatelessLongHash
;
/**
* Promotes a {@link StatefulIntHash} to a {@link StatefulLongHash}.
*/
public
final
class
IntStatefulLongHash
implements
StatefulLongHash
{
private
final
StatefulIntHash
intHash
;
/**
* Constructs a new {@link IntStatefulLongHash} that delegates to the given
* {@link StatefulIntHash}.
*
* @param intHash the underlying int-width hash
*/
public
IntStatefulLongHash
(
StatefulIntHash
intHash
)
{
this
.
intHash
=
intHash
;
}
public
StatelessLongHash
asStateless
()
{
return
new
IntStatelessLongHash
(
intHash
.
asStateless
());
}
public
String
algorithm
()
{
return
intHash
.
algorithm
();
}
public
int
length
()
{
return
intHash
.
length
();
}
public
StatefulHash
createNew
()
{
return
intHash
.
createNew
();
}
public
boolean
supportsUnsafe
()
{
return
intHash
.
supportsUnsafe
();
}
public
boolean
supportsIncremental
()
{
return
intHash
.
supportsIncremental
();
}
public
void
reset
()
{
intHash
.
reset
();
}
public
void
update
(
byte
[]
input
)
{
intHash
.
update
(
input
);
}
public
void
update
(
byte
[]
input
,
int
index
,
int
length
)
{
intHash
.
update
(
input
,
index
,
length
);
}
public
void
update
(
ByteBuffer
input
)
{
intHash
.
update
(
input
);
}
public
void
update
(
long
address
,
long
length
)
{
intHash
.
update
(
address
,
length
);
}
public
byte
[]
getBytes
()
{
return
intHash
.
getBytes
();
}
public
int
getBytes
(
byte
[]
output
,
int
index
,
int
maxLength
)
{
return
intHash
.
getBytes
(
output
,
index
,
maxLength
);
}
public
byte
getByte
()
{
return
intHash
.
getByte
();
}
public
short
getShort
()
{
return
intHash
.
getShort
();
}
public
int
getInt
()
{
return
intHash
.
getInt
();
}
public
long
getLong
()
{
return
intHash
.
getLong
();
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/IntStatelessLongHash.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
java.nio.ByteBuffer
;
import
com.scurrilous.circe.StatefulLongHash
;
import
com.scurrilous.circe.StatelessIntHash
;
import
com.scurrilous.circe.StatelessLongHash
;
/**
* Promotes a {@link StatelessIntHash} to a {@link StatelessLongHash}.
*/
public
final
class
IntStatelessLongHash
implements
StatelessLongHash
{
private
final
StatelessIntHash
intHash
;
/**
* Constructs a new {@link IntStatelessLongHash} that delegates to the given
* {@link StatelessIntHash}.
*
* @param intHash the underlying int-width hash
*/
public
IntStatelessLongHash
(
StatelessIntHash
intHash
)
{
this
.
intHash
=
intHash
;
}
@Override
public
String
algorithm
()
{
return
intHash
.
algorithm
();
}
@Override
public
int
length
()
{
return
intHash
.
length
();
}
@Override
public
boolean
supportsUnsafe
()
{
return
intHash
.
supportsUnsafe
();
}
@Override
public
StatefulLongHash
createStateful
()
{
return
new
IntStatefulLongHash
(
intHash
.
createStateful
());
}
@Override
public
long
calculate
(
byte
[]
input
)
{
return
intHash
.
calculate
(
input
);
}
@Override
public
long
calculate
(
byte
[]
input
,
int
index
,
int
length
)
{
return
intHash
.
calculate
(
input
,
index
,
length
);
}
@Override
public
long
calculate
(
ByteBuffer
input
)
{
return
intHash
.
calculate
(
input
);
}
@Override
public
long
calculate
(
long
address
,
long
length
)
{
return
intHash
.
calculate
(
address
,
length
);
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/impl/package-info.java
0 → 100644
浏览文件 @
554f0505
/**
* Provides support for implementing new {@linkplain
* com.scurrilous.circe.HashProvider hash providers} in the form of abstract
* base classes and utility classes.
*/
package
com.scurrilous.circe.impl
;
pulsar-checksum/src/main/java/com/scurrilous/circe/package-info.java
0 → 100644
浏览文件 @
554f0505
/**
* Provides interfaces and minimal support classes for providing and consuming
* various forms of hash functions. The actual hash algorithms are implemented
* by {@linkplain com.scurrilous.circe.HashProvider hash providers}.
*/
package
com.scurrilous.circe
;
pulsar-checksum/src/main/java/com/scurrilous/circe/params/CrcParameters.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.params
;
import
com.scurrilous.circe.HashParameters
;
/**
* Hash parameters used to define a <a
* href="http://en.wikipedia.org/wiki/Cyclic_redundancy_check">cyclic redundancy
* check</a> (CRC). Includes some commonly used sets of parameters.
*/
public
class
CrcParameters
implements
HashParameters
{
private
final
String
name
;
private
final
int
bitWidth
;
private
final
long
polynomial
;
private
final
long
initial
;
private
final
long
xorOut
;
private
final
boolean
reflected
;
/**
* Constructs a {@link CrcParameters} with the given parameters.
*
* @param name the canonical name of the CRC function
* @param bitWidth the width of the CRC function
* @param polynomial the polynomial in binary form (non-reflected)
* @param initial the initial value of the CRC register
* @param xorOut a value XOR'ed with the CRC when it is read
* @param reflected indicates whether the CRC is reflected (LSB-first)
* @throws IllegalArgumentException if the width is less than 1 or greater
* than 64
*/
public
CrcParameters
(
String
name
,
int
bitWidth
,
long
polynomial
,
long
initial
,
long
xorOut
,
boolean
reflected
)
{
if
(
bitWidth
<
1
||
bitWidth
>
64
)
throw
new
IllegalArgumentException
();
this
.
name
=
name
;
this
.
bitWidth
=
bitWidth
;
final
long
mask
=
bitWidth
<
64
?
(
1L
<<
bitWidth
)
-
1
:
~
0L
;
this
.
polynomial
=
polynomial
&
mask
;
this
.
initial
=
initial
&
mask
;
this
.
xorOut
=
xorOut
&
mask
;
this
.
reflected
=
reflected
;
}
@Override
public
String
algorithm
()
{
return
name
;
}
/**
* Returns the width in bits of the CRC function. The width is also the
* position of the implicit set bit at the top of the polynomial.
*
* @return the CRC width in bits
*/
public
int
bitWidth
()
{
return
bitWidth
;
}
/**
* Returns the binary form of polynomial that defines the CRC function (with
* the implicit top bit omitted). For instance, the CRC-16 polynomial
* x<sup>16</sup> + x<sup>15</sup> + x<sup>2</sup> + 1 is represented as
* {@code 1000 0000 0000 0101} ({@code 0x8005}).
*
* @return the CRC polynomial
*/
public
long
polynomial
()
{
return
polynomial
;
}
/**
* Returns the initial value of the CRC register.
*
* @return the CRC initial value
*/
public
long
initial
()
{
return
initial
;
}
/**
* Returns the value XOR'ed with the CRC register when it is read to
* determine the output value.
*
* @return the final XOR value
*/
public
long
xorOut
()
{
return
xorOut
;
}
/**
* Returns whether the CRC function is "reflected". Reflected CRCs process
* data LSB-first, whereas "normal" CRCs are MSB-first.
*
* @return whether the CRC function is reflected
*/
public
boolean
reflected
()
{
return
reflected
;
}
/**
* Returns whether this object matches the given CRC parameters.
*
* @param bitWidth the width of the CRC function
* @param polynomial the polynomial in binary form (non-reflected)
* @param initial the initial value of the CRC register
* @param xorOut a value XOR'ed with the CRC when it is read
* @param reflected indicates whether the CRC is reflected (LSB-first)
* @return true if all parameters match exactly, false otherwise
*/
public
boolean
match
(
int
bitWidth
,
long
polynomial
,
long
initial
,
long
xorOut
,
boolean
reflected
)
{
return
bitWidth
==
this
.
bitWidth
&&
polynomial
==
this
.
polynomial
&&
initial
==
this
.
initial
&&
xorOut
==
this
.
xorOut
&&
reflected
==
this
.
reflected
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
||
getClass
()
!=
obj
.
getClass
())
return
false
;
final
CrcParameters
other
=
(
CrcParameters
)
obj
;
return
bitWidth
==
other
.
bitWidth
&&
polynomial
==
other
.
polynomial
&&
initial
==
other
.
initial
&&
xorOut
==
other
.
xorOut
&&
reflected
==
other
.
reflected
;
}
@Override
public
int
hashCode
()
{
return
(
int
)
(
polynomial
^
(
polynomial
>>>
32
)
^
initial
^
(
initial
>>>
32
)
^
xorOut
^
(
xorOut
>>>
32
))
^
(
reflected
?
~
0
:
0
);
}
/**
* Parameters for CRC-16, used in the ARC and LHA compression utilities.
*/
public
static
final
CrcParameters
CRC16
=
new
CrcParameters
(
"CRC-16"
,
16
,
0x8005
,
0
,
0
,
true
);
/**
* Parameters for CRC-16/CCITT, used in the Kermit protocol.
*/
public
static
final
CrcParameters
CRC16_CCITT
=
new
CrcParameters
(
"CRC-16/CCITT"
,
16
,
0x1021
,
0
,
0
,
true
);
/**
* Parameters for CRC-16/XMODEM, used in the XMODEM protocol.
*/
public
static
final
CrcParameters
CRC16_XMODEM
=
new
CrcParameters
(
"CRC-16/XMODEM"
,
16
,
0x1021
,
0
,
0
,
false
);
/**
* Parameters for CRC-32, used in Ethernet, SATA, PKZIP, ZMODEM, etc.
*/
public
static
final
CrcParameters
CRC32
=
new
CrcParameters
(
"CRC-32"
,
32
,
0x04c11db7
,
~
0
,
~
0
,
true
);
/**
* Parameters for CRC-32/BZIP2, used in BZIP2.
*/
public
static
final
CrcParameters
CRC32_BZIP2
=
new
CrcParameters
(
"CRC-32/BZIP2"
,
32
,
0x04c11db7
,
~
0
,
~
0
,
false
);
/**
* Parameters for CRC-32C, used in iSCSI and SCTP.
*/
public
static
final
CrcParameters
CRC32C
=
new
CrcParameters
(
"CRC-32C"
,
32
,
0x1edc6f41
,
~
0
,
~
0
,
true
);
/**
* Parameters for CRC-32/MPEG-2, used in MPEG-2.
*/
public
static
final
CrcParameters
CRC32_MPEG2
=
new
CrcParameters
(
"CRC-32/MPEG-2"
,
32
,
0x04c11db7
,
~
0
,
0
,
false
);
/**
* Parameters for CRC-32/POSIX, used in the {@code cksum} utility.
*/
public
static
final
CrcParameters
CRC32_POSIX
=
new
CrcParameters
(
"CRC-32/POSIX"
,
32
,
0x04c11db7
,
0
,
~
0
,
false
);
/**
* Parameters for CRC-64, used in the ECMA-182 standard for DLT-1 tapes.
*/
public
static
final
CrcParameters
CRC64
=
new
CrcParameters
(
"CRC-64"
,
64
,
0x42f0e1eba9ea3693
L
,
0L
,
0L
,
false
);
/**
* Parameters for CRC-64/XZ, used in the {@code .xz} file format.
*/
public
static
final
CrcParameters
CRC64_XZ
=
new
CrcParameters
(
"CRC-64/XZ"
,
64
,
0x42f0e1eba9ea3693
L
,
~
0L
,
~
0L
,
true
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/params/MurmurHash3Parameters.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.params
;
import
com.scurrilous.circe.HashParameters
;
/**
* Hash parameters for <a
* href="https://code.google.com/p/smhasher/wiki/MurmurHash3">MurmurHash3</a>.
*/
public
final
class
MurmurHash3Parameters
implements
HashParameters
{
private
final
MurmurHash3Variant
variant
;
private
final
int
seed
;
/**
* Constructs a {@link MurmurHash3Parameters} with the given variant and a
* seed value of zero.
*
* @param variant the variant of the algorithm
*/
public
MurmurHash3Parameters
(
MurmurHash3Variant
variant
)
{
this
(
variant
,
0
);
}
/**
* Constructs a {@link MurmurHash3Parameters} with the given variant and
* seed value.
*
* @param variant the variant of the algorithm
* @param seed the seed value
*/
public
MurmurHash3Parameters
(
MurmurHash3Variant
variant
,
int
seed
)
{
this
.
variant
=
variant
;
this
.
seed
=
seed
;
}
/**
* Returns the variant of the hash algorithm.
*
* @return the algorithm variant
*/
public
MurmurHash3Variant
variant
()
{
return
variant
;
}
/**
* Returns the seed value for the hash function.
*
* @return the seed value
*/
public
int
seed
()
{
return
seed
;
}
@Override
public
String
algorithm
()
{
return
variant
.
algorithm
();
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/params/MurmurHash3Variant.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.params
;
/**
* Enumeration of MurmurHash3 variants.
*/
public
enum
MurmurHash3Variant
{
/**
* 32-bit variant (optimized for x86).
*/
X86_32
(
"MurmurHash3_x86_32"
),
/**
* 128-bit variant optimized for x86.
*/
X86_128
(
"MurmurHash3_x86_128"
),
/**
* 128-bit variant optimized for x64.
*/
X64_128
(
"MurmurHash3_x64_128"
);
private
final
String
algorithm
;
private
MurmurHash3Variant
(
String
algorithm
)
{
this
.
algorithm
=
algorithm
;
}
/**
* Returns the algorithm name corresponding to this variant.
*
* @return this variant's algorithm name
*/
public
String
algorithm
()
{
return
algorithm
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/params/SimpleHashParameters.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.params
;
import
com.scurrilous.circe.HashParameters
;
/**
* Hash parameters consisting only of an algorithm name. Includes instances
* describing some commonly used algorithms.
*/
public
class
SimpleHashParameters
implements
HashParameters
{
private
final
String
algorithm
;
/**
* Constructs a {@link SimpleHashParameters} with the given algorithm name.
*
* @param algorithm the name of the hash algorithm
*/
public
SimpleHashParameters
(
String
algorithm
)
{
this
.
algorithm
=
algorithm
;
}
@Override
public
String
algorithm
()
{
return
algorithm
;
}
@Override
public
int
hashCode
()
{
return
algorithm
.
hashCode
();
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
obj
==
this
)
return
true
;
if
(
obj
==
null
||
obj
.
getClass
()
!=
SimpleHashParameters
.
class
)
return
false
;
return
algorithm
.
equals
(((
SimpleHashParameters
)
obj
).
algorithm
);
}
@Override
public
String
toString
()
{
return
algorithm
;
}
/**
* Represents the MD5 (128-bit) hash algorithm.
*/
public
static
final
SimpleHashParameters
MD5
=
new
SimpleHashParameters
(
"MD5"
);
/**
* Represents the SHA-1 (160-bit) hash algorithm.
*/
public
static
final
SimpleHashParameters
SHA1
=
new
SimpleHashParameters
(
"SHA-1"
);
/**
* Represents the SHA-256 (256-bit) hash algorithm.
*/
public
static
final
SimpleHashParameters
SHA256
=
new
SimpleHashParameters
(
"SHA-256"
);
/**
* Represents the SHA-384 (384-bit) hash algorithm.
*/
public
static
final
SimpleHashParameters
SHA384
=
new
SimpleHashParameters
(
"SHA-384"
);
/**
* Represents the SHA-512 (512-bit) hash algorithm.
*/
public
static
final
SimpleHashParameters
SHA512
=
new
SimpleHashParameters
(
"SHA-512"
);
}
pulsar-checksum/src/main/java/com/scurrilous/circe/params/SipHash24Parameters.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.params
;
import
com.scurrilous.circe.HashParameters
;
/**
* Hash parameters for <a href="https://131002.net/siphash/">SipHash-2-4</a>.
*/
public
final
class
SipHash24Parameters
implements
HashParameters
{
private
final
long
seedLow
;
private
final
long
seedHigh
;
/**
* Constructs a {@link SipHash24Parameters} with the default seed,
* {@code 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F}.
*/
public
SipHash24Parameters
()
{
this
(
0x0706050403020100
L
,
0x0f0e0d0c0b0a0908
L
);
}
/**
* Constructs a {@link SipHash24Parameters} with the given seed.
*
* @param seedLow the low-order 64 bits of the seed
* @param seedHigh the high-order 64 bits of the seed
*/
public
SipHash24Parameters
(
long
seedLow
,
long
seedHigh
)
{
this
.
seedLow
=
seedLow
;
this
.
seedHigh
=
seedHigh
;
}
/**
* Returns the low-order 64 bits of the seed.
*
* @return low-order bits of seed
*/
public
long
seedLow
()
{
return
seedLow
;
}
/**
* Returns the high-order 64 bits of the seed.
*
* @return high-order bits of seed
*/
public
long
seedHigh
()
{
return
seedHigh
;
}
@Override
public
String
algorithm
()
{
return
"SipHash-2-4"
;
}
}
pulsar-checksum/src/main/java/com/scurrilous/circe/params/package-info.java
0 → 100644
浏览文件 @
554f0505
/**
* Defines {@linkplain com.scurrilous.circe.HashParameters hash parameters}
* classes that describe various commonly-used hash functions.
*/
package
com.scurrilous.circe.params
;
pulsar-checksum/src/main/resources/META-INF/services/com.scurrilous.circe.HashProvider
0 → 100644
浏览文件 @
554f0505
com.scurrilous.circe.crc.StandardCrcProvider
\ No newline at end of file
pulsar-checksum/src/test/java/com/scurrilous/circe/CommonHashesTest.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe
;
import
static
org
.
testng
.
Assert
.
assertEquals
;
import
java.nio.charset.Charset
;
import
org.testng.annotations.Test
;
@SuppressWarnings
(
"javadoc"
)
public
class
CommonHashesTest
{
private
static
final
Charset
ASCII
=
Charset
.
forName
(
"ASCII"
);
private
static
final
byte
[]
DIGITS
=
"123456789"
.
getBytes
(
ASCII
);
@Test
public
void
testCrc32
()
{
assertEquals
(
0xcbf43926
,
CommonHashes
.
crc32
().
calculate
(
DIGITS
));
}
@Test
public
void
testCrc32c
()
{
assertEquals
(
0xe3069283
,
CommonHashes
.
crc32c
().
calculate
(
DIGITS
));
}
@Test
public
void
testCrc64
()
{
assertEquals
(
0x6c40df5f0b497347
L
,
CommonHashes
.
crc64
().
calculate
(
DIGITS
));
}
}
pulsar-checksum/src/test/java/com/scurrilous/circe/crc/CRCProvidersTest.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
import
static
com
.
scurrilous
.
circe
.
HashSupport
.
HARDWARE
;
import
static
com
.
scurrilous
.
circe
.
HashSupport
.
INCREMENTAL
;
import
static
com
.
scurrilous
.
circe
.
HashSupport
.
INT_SIZED
;
import
static
com
.
scurrilous
.
circe
.
HashSupport
.
LONG_SIZED
;
import
static
com
.
scurrilous
.
circe
.
HashSupport
.
NATIVE
;
import
static
com
.
scurrilous
.
circe
.
HashSupport
.
STATEFUL
;
import
static
com
.
scurrilous
.
circe
.
HashSupport
.
STATELESS_INCREMENTAL
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC32
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC64
;
import
static
org
.
testng
.
Assert
.
assertEquals
;
import
static
org
.
testng
.
Assert
.
assertFalse
;
import
static
org
.
testng
.
Assert
.
assertTrue
;
import
java.util.EnumSet
;
import
java.util.Iterator
;
import
java.util.Map.Entry
;
import
java.util.SortedMap
;
import
org.testng.annotations.Test
;
import
com.scurrilous.circe.HashProvider
;
import
com.scurrilous.circe.HashProviders
;
import
com.scurrilous.circe.HashSupport
;
import
com.scurrilous.circe.IncrementalLongHash
;
@SuppressWarnings
(
"javadoc"
)
public
class
CRCProvidersTest
{
@Test
public
void
testAll
()
{
final
Iterator
<
HashProvider
>
i
=
HashProviders
.
iterator
();
assertTrue
(
i
.
hasNext
());
assertTrue
(
i
.
next
()
instanceof
StandardCrcProvider
);
assertFalse
(
i
.
hasNext
());
}
@Test
public
void
testNonUnique
()
{
final
HashProvider
provider
=
HashProviders
.
best
(
CRC32
);
final
IncrementalLongHash
i1
=
provider
.
getIncrementalLong
(
CRC32
);
final
IncrementalLongHash
i2
=
provider
.
getIncrementalLong
(
CRC32
);
assertTrue
(
i1
!=
i2
);
}
@Test
public
void
testSearchCRCParametersCRC32
()
{
final
SortedMap
<
EnumSet
<
HashSupport
>,
HashProvider
>
map
=
HashProviders
.
search
(
CRC32
);
assertEquals
(
1
,
map
.
size
());
final
Entry
<
EnumSet
<
HashSupport
>,
HashProvider
>
entry
=
map
.
entrySet
().
iterator
().
next
();
assertEquals
(
EnumSet
.
of
(
NATIVE
,
STATELESS_INCREMENTAL
,
INCREMENTAL
,
INT_SIZED
,
LONG_SIZED
,
STATEFUL
),
entry
.
getKey
());
assertTrue
(
entry
.
getValue
()
instanceof
StandardCrcProvider
);
}
@Test
public
void
testSearchCRCParametersCRC64
()
{
final
SortedMap
<
EnumSet
<
HashSupport
>,
HashProvider
>
map
=
HashProviders
.
search
(
CRC64
);
assertEquals
(
1
,
map
.
size
());
final
Entry
<
EnumSet
<
HashSupport
>,
HashProvider
>
entry
=
map
.
entrySet
().
iterator
().
next
();
assertEquals
(
EnumSet
.
of
(
STATELESS_INCREMENTAL
,
INCREMENTAL
,
LONG_SIZED
,
STATEFUL
),
entry
.
getKey
());
assertTrue
(
entry
.
getValue
()
instanceof
StandardCrcProvider
);
}
@Test
public
void
testSearchCRCParametersEnumSet
()
{
assertEquals
(
1
,
HashProviders
.
search
(
CRC32
,
EnumSet
.
of
(
NATIVE
)).
size
());
assertTrue
(
HashProviders
.
search
(
CRC64
,
EnumSet
.
of
(
NATIVE
)).
isEmpty
());
assertTrue
(
HashProviders
.
search
(
CRC32
,
EnumSet
.
of
(
HARDWARE
)).
isEmpty
());
}
}
pulsar-checksum/src/test/java/com/scurrilous/circe/crc/CRCTest.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.crc
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC16
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC16_CCITT
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC16_XMODEM
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC32
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC32C
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC32_BZIP2
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC32_POSIX
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC64
;
import
static
com
.
scurrilous
.
circe
.
params
.
CrcParameters
.
CRC64_XZ
;
import
static
org
.
testng
.
Assert
.
assertEquals
;
import
java.nio.charset.Charset
;
import
org.testng.annotations.Test
;
import
com.scurrilous.circe.HashProvider
;
import
com.scurrilous.circe.params.CrcParameters
;
/**
* Tests the {@link StandardCrcProvider} with various CRC algorithms. See the <a
* href="http://reveng.sourceforge.net/crc-catalogue/">Catalogue of parametrised
* CRC algorithms</a> for more information on these algorithms and others.
*/
@SuppressWarnings
(
"javadoc"
)
public
class
CRCTest
{
private
static
final
HashProvider
PROVIDER
=
new
StandardCrcProvider
();
private
static
final
Charset
ASCII
=
Charset
.
forName
(
"ASCII"
);
private
static
final
byte
[]
DIGITS
=
"123456789"
.
getBytes
(
ASCII
);
@Test
public
void
testCRC3_ROHC
()
{
final
CrcParameters
CRC3_ROHC
=
new
CrcParameters
(
"CRC-3/ROHC"
,
3
,
0x3
,
0x7
,
0
,
true
);
assertEquals
(
0x6
,
PROVIDER
.
getIncrementalInt
(
CRC3_ROHC
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC5_EPC
()
{
final
CrcParameters
CRC5_EPC
=
new
CrcParameters
(
"CRC-5/EPC"
,
5
,
0x09
,
0x09
,
0
,
false
);
assertEquals
(
0x00
,
PROVIDER
.
getIncrementalInt
(
CRC5_EPC
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC5_USB
()
{
final
CrcParameters
CRC5_USB
=
new
CrcParameters
(
"CRC-5/USB"
,
5
,
0x05
,
0x1f
,
0x1f
,
true
);
assertEquals
(
0x19
,
PROVIDER
.
getIncrementalInt
(
CRC5_USB
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC7
()
{
final
CrcParameters
CRC7
=
new
CrcParameters
(
"CRC-7"
,
7
,
0x09
,
0
,
0
,
false
);
assertEquals
(
0x75
,
PROVIDER
.
getIncrementalInt
(
CRC7
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC7ROHC
()
{
final
CrcParameters
CRC7_ROHC
=
new
CrcParameters
(
"CRC-7/ROHC"
,
7
,
0x4f
,
0x7f
,
0
,
true
);
assertEquals
(
0x53
,
PROVIDER
.
getIncrementalInt
(
CRC7_ROHC
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC8
()
{
final
CrcParameters
CRC8
=
new
CrcParameters
(
"CRC-8"
,
8
,
0x07
,
0
,
0
,
false
);
assertEquals
(
0xf4
,
PROVIDER
.
getIncrementalInt
(
CRC8
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC10
()
{
final
CrcParameters
CRC10
=
new
CrcParameters
(
"CRC-10"
,
10
,
0x233
,
0
,
0
,
false
);
assertEquals
(
0x199
,
PROVIDER
.
getIncrementalInt
(
CRC10
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC15
()
{
final
CrcParameters
CRC15
=
new
CrcParameters
(
"CRC-15"
,
15
,
0x4599
,
0
,
0
,
false
);
assertEquals
(
0x059e
,
PROVIDER
.
getIncrementalInt
(
CRC15
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC16
()
{
assertEquals
(
0xbb3d
,
PROVIDER
.
getIncrementalInt
(
CRC16
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC16_CCITT
()
{
assertEquals
(
0x2189
,
PROVIDER
.
getIncrementalInt
(
CRC16_CCITT
).
calculate
(
DIGITS
));
}
@Test
public
void
testXMODEM
()
{
assertEquals
(
0x31c3
,
PROVIDER
.
getIncrementalInt
(
CRC16_XMODEM
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC24
()
{
final
CrcParameters
CRC24
=
new
CrcParameters
(
"CRC-24"
,
24
,
0x864cfb
,
0xb704ce
,
0
,
false
);
assertEquals
(
0x21cf02
,
PROVIDER
.
getIncrementalInt
(
CRC24
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC32
()
{
assertEquals
(
0xcbf43926
,
PROVIDER
.
getIncrementalInt
(
CRC32
).
calculate
(
DIGITS
));
}
@Test
public
void
testJavaCRC32
()
{
assertEquals
(
0xcbf43926
,
PROVIDER
.
getStatelessInt
(
CRC32
).
calculate
(
DIGITS
));
}
@Test
public
void
testBZIP2
()
{
assertEquals
(
0xfc891918
,
PROVIDER
.
getIncrementalInt
(
CRC32_BZIP2
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC32C
()
{
assertEquals
(
0xe3069283
,
PROVIDER
.
getIncrementalInt
(
CRC32C
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC32D
()
{
final
CrcParameters
CRC32D
=
new
CrcParameters
(
"CRC-32D"
,
32
,
0xa833982b
,
~
0
,
~
0
,
true
);
assertEquals
(
0x87315576
,
PROVIDER
.
getIncrementalInt
(
CRC32D
).
calculate
(
DIGITS
));
}
@Test
public
void
testPOSIX
()
{
assertEquals
(
0x765e7680
,
PROVIDER
.
getIncrementalInt
(
CRC32_POSIX
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC32Q
()
{
final
CrcParameters
CRC32Q
=
new
CrcParameters
(
"CRC-32Q"
,
32
,
0x814141ab
,
0
,
0
,
false
);
assertEquals
(
0x3010bf7f
,
PROVIDER
.
getIncrementalInt
(
CRC32Q
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC64
()
{
assertEquals
(
0x6c40df5f0b497347
L
,
PROVIDER
.
getIncrementalLong
(
CRC64
).
calculate
(
DIGITS
));
}
@Test
public
void
testCRC64_XZ
()
{
assertEquals
(
0x995dc9bbdf1939fa
L
,
PROVIDER
.
getIncrementalLong
(
CRC64_XZ
).
calculate
(
DIGITS
));
}
}
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractIncrementalIntHashTest.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
static
org
.
junit
.
Assert
.*;
import
java.nio.ByteBuffer
;
import
mockit.Expectations
;
import
mockit.Mocked
;
import
org.testng.annotations.Test
;
import
static
org
.
testng
.
Assert
.*;
import
com.scurrilous.circe.StatefulHash
;
import
com.scurrilous.circe.impl.AbstractIncrementalIntHash
;
@SuppressWarnings
(
"javadoc"
)
public
class
AbstractIncrementalIntHashTest
{
@Mocked
private
AbstractIncrementalIntHash
hash
;
@Test
public
void
testAsStateful
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
algorithm
();
hash
.
length
();
hash
.
initial
();
result
=
42
;
hash
.
resumeUnchecked
(
42
,
input
,
2
,
4
);
result
=
99
;
}
};
StatefulHash
stateful
=
hash
.
createStateful
();
stateful
.
algorithm
();
stateful
.
length
();
assertNotEquals
(
stateful
,
stateful
.
createNew
());
stateful
.
reset
();
stateful
.
update
(
input
,
2
,
4
);
assertEquals
(
99
,
stateful
.
getInt
());
}
@Test
public
void
testCalculateByteArray
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
initial
();
result
=
42
;
hash
.
resume
(
42
,
input
);
}
};
hash
.
calculate
(
input
);
}
@Test
public
void
testCalculateByteArrayIntInt
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
initial
();
result
=
42
;
hash
.
resume
(
42
,
input
,
2
,
4
);
}
};
hash
.
calculate
(
input
,
2
,
4
);
}
@Test
public
void
testCalculateByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
10
);
new
Expectations
(
hash
)
{
{
hash
.
initial
();
result
=
42
;
hash
.
resume
(
42
,
input
);
}
};
hash
.
calculate
(
input
);
}
@Test
public
void
testResumeIntByteArray
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
resumeUnchecked
(
42
,
input
,
0
,
input
.
length
);
}
};
hash
.
resume
(
42
,
input
);
}
@Test
public
void
testResumeIntByteArrayIntInt
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
resumeUnchecked
(
42
,
input
,
2
,
4
);
}
};
hash
.
resume
(
42
,
input
,
2
,
4
);
}
@Test
public
void
testResumeIntByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
);
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
resumeUnchecked
(
42
,
input
.
array
(),
input
.
arrayOffset
()
+
5
,
10
);
}
};
hash
.
resume
(
42
,
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
@Test
public
void
testResumeIntReadOnlyByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
).
asReadOnlyBuffer
();
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
resumeUnchecked
(
42
,
withInstanceOf
(
byte
[].
class
),
0
,
10
);
}
};
hash
.
resume
(
42
,
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
}
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractIncrementalLongHashTest.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
static
org
.
testng
.
Assert
.*;
import
java.nio.ByteBuffer
;
import
mockit.Expectations
;
import
mockit.Mocked
;
import
org.testng.annotations.Test
;
import
com.scurrilous.circe.StatefulHash
;
import
com.scurrilous.circe.impl.AbstractIncrementalLongHash
;
@SuppressWarnings
(
"javadoc"
)
public
class
AbstractIncrementalLongHashTest
{
@Mocked
private
AbstractIncrementalLongHash
hash
;
@Test
public
void
testAsStateful
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
algorithm
();
hash
.
length
();
hash
.
initial
();
result
=
0x4200000000
L
;
hash
.
resumeUnchecked
(
0x4200000000
L
,
input
,
2
,
4
);
result
=
0x990000000000
L
;
}
};
StatefulHash
stateful
=
hash
.
createStateful
();
stateful
.
algorithm
();
stateful
.
length
();
assertNotSame
(
stateful
,
stateful
.
createNew
());
stateful
.
reset
();
stateful
.
update
(
input
,
2
,
4
);
assertEquals
(
0
,
stateful
.
getInt
());
assertEquals
(
0x990000000000
L
,
stateful
.
getLong
());
}
@Test
public
void
testCalculateByteArray
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
initial
();
result
=
0x4200000000
L
;
hash
.
resume
(
0x4200000000
L
,
input
);
}
};
hash
.
calculate
(
input
);
}
@Test
public
void
testCalculateByteArrayIntInt
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
initial
();
result
=
0x4200000000
L
;
hash
.
resume
(
0x4200000000
L
,
input
,
2
,
4
);
}
};
hash
.
calculate
(
input
,
2
,
4
);
}
@Test
public
void
testCalculateByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
10
);
new
Expectations
(
hash
)
{
{
hash
.
initial
();
result
=
0x4200000000
L
;
hash
.
resume
(
0x4200000000
L
,
input
);
}
};
hash
.
calculate
(
input
);
}
@Test
public
void
testResumeLongByteArray
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
resumeUnchecked
(
0x4200000000
L
,
input
,
0
,
input
.
length
);
}
};
hash
.
resume
(
0x4200000000
L
,
input
);
}
@Test
public
void
testResumeLongByteArrayIntInt
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
resumeUnchecked
(
0x4200000000
L
,
input
,
2
,
4
);
}
};
hash
.
resume
(
0x4200000000
L
,
input
,
2
,
4
);
}
@Test
public
void
testResumeLongByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
);
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
resumeUnchecked
(
0x4200000000
L
,
input
.
array
(),
input
.
arrayOffset
()
+
5
,
10
);
}
};
hash
.
resume
(
0x4200000000
L
,
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
@Test
public
void
testResumeLongReadOnlyByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
).
asReadOnlyBuffer
();
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
resumeUnchecked
(
0x4200000000
L
,
withInstanceOf
(
byte
[].
class
),
0
,
10
);
}
};
hash
.
resume
(
0x4200000000
L
,
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
}
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractStatefulHashTest.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
static
org
.
testng
.
Assert
.
assertEquals
;
import
java.nio.ByteBuffer
;
import
java.util.ArrayList
;
import
java.util.List
;
import
mockit.Expectations
;
import
mockit.Mocked
;
import
mockit.NonStrictExpectations
;
import
org.testng.annotations.Test
;
@SuppressWarnings
(
"javadoc"
)
public
class
AbstractStatefulHashTest
{
@Mocked
private
AbstractStatefulHash
hash
;
@Test
public
void
testUpdateByteArray
()
{
final
byte
[]
input
=
new
byte
[
42
];
new
Expectations
(
hash
)
{
{
hash
.
updateUnchecked
(
input
,
0
,
input
.
length
);
}
};
hash
.
update
(
input
);
}
@Test
public
void
testUpdateByteArrayIntInt
()
{
final
byte
[]
input
=
new
byte
[
42
];
new
Expectations
(
hash
)
{
{
hash
.
updateUnchecked
(
input
,
5
,
10
);
}
};
hash
.
update
(
input
,
5
,
10
);
}
@Test
(
expectedExceptions
=
IllegalArgumentException
.
class
)
public
void
testUpdateByteArrayIntNegInt
()
{
final
byte
[]
input
=
new
byte
[
42
];
new
Expectations
(
hash
)
{
};
hash
.
update
(
input
,
1
,
-
1
);
}
@Test
(
expectedExceptions
=
IndexOutOfBoundsException
.
class
)
public
void
testUpdateByteArrayNegIntInt
()
{
final
byte
[]
input
=
new
byte
[
42
];
new
Expectations
(
hash
)
{
};
hash
.
update
(
input
,
-
1
,
10
);
}
@Test
(
expectedExceptions
=
IndexOutOfBoundsException
.
class
)
public
void
testUpdateByteArrayIntIntOverflow
()
{
final
byte
[]
input
=
new
byte
[
42
];
new
Expectations
(
hash
)
{
};
hash
.
update
(
input
,
40
,
3
);
}
@Test
public
void
testUpdateByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
);
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
updateUnchecked
(
input
.
array
(),
input
.
arrayOffset
()
+
5
,
10
);
}
};
hash
.
update
(
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
@Test
public
void
testUpdateReadOnlyByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
).
asReadOnlyBuffer
();
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
updateUnchecked
(
withInstanceOf
(
byte
[].
class
),
0
,
10
);
}
};
hash
.
update
(
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
@Test
public
void
testGetBytes
()
{
final
List
<
byte
[]>
captures
=
new
ArrayList
<>();
new
Expectations
(
hash
)
{
{
hash
.
length
();
result
=
5
;
hash
.
writeBytes
(
withCapture
(
captures
),
0
,
5
);
}
};
hash
.
getBytes
();
assertEquals
(
5
,
captures
.
get
(
0
).
length
);
}
@Test
public
void
testGetBytesByteArrayInt
()
{
final
byte
[]
output
=
new
byte
[
5
];
new
Expectations
(
hash
)
{
{
hash
.
length
();
result
=
output
.
length
;
hash
.
getLong
();
result
=
0x1234567890
L
;
}
};
hash
.
getBytes
(
output
,
0
,
output
.
length
);
assertEquals
(
new
byte
[]
{
(
byte
)
0x90
,
0x78
,
0x56
,
0x34
,
0x12
},
output
);
}
@Test
(
expectedExceptions
=
IndexOutOfBoundsException
.
class
)
public
void
testGetBytesByteArrayNegInt
()
{
final
byte
[]
output
=
new
byte
[
5
];
new
NonStrictExpectations
(
hash
)
{
{
hash
.
length
();
result
=
output
.
length
;
}
};
hash
.
getBytes
(
output
,
-
1
,
output
.
length
);
}
@Test
(
expectedExceptions
=
IndexOutOfBoundsException
.
class
)
public
void
testGetBytesByteArrayIntOverflow
()
{
final
byte
[]
output
=
new
byte
[
5
];
new
Expectations
(
hash
)
{
};
hash
.
getBytes
(
output
,
0
,
output
.
length
+
1
);
}
@Test
public
void
testGetBytesByteArrayIntPartial
()
{
final
byte
[]
output
=
new
byte
[
5
];
new
Expectations
(
hash
)
{
{
hash
.
length
();
result
=
output
.
length
+
1
;
hash
.
writeBytes
(
output
,
0
,
output
.
length
);
}
};
hash
.
getBytes
(
output
,
0
,
output
.
length
);
}
@Test
public
void
testGetByte
()
{
new
Expectations
(
hash
)
{
{
hash
.
getInt
();
result
=
0x12345678
;
}
};
assertEquals
(
0x78
,
hash
.
getByte
());
}
@Test
public
void
testGetShort
()
{
new
Expectations
(
hash
)
{
{
hash
.
getInt
();
result
=
0x12345678
;
}
};
assertEquals
(
0x5678
,
hash
.
getShort
());
}
}
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractStatelessIntHashTest.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
static
org
.
testng
.
Assert
.*;
import
java.nio.ByteBuffer
;
import
mockit.Expectations
;
import
mockit.Mocked
;
import
org.testng.annotations.Test
;
import
com.scurrilous.circe.impl.AbstractStatelessIntHash
;
@SuppressWarnings
(
"javadoc"
)
public
class
AbstractStatelessIntHashTest
{
@Mocked
private
AbstractStatelessIntHash
hash
;
@Test
public
void
testCalculateByteArray
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
calculateUnchecked
(
input
,
0
,
input
.
length
);
}
};
hash
.
calculate
(
input
);
}
@Test
public
void
testCalculateByteArrayIntInt
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
calculateUnchecked
(
input
,
2
,
4
);
}
};
hash
.
calculate
(
input
,
2
,
4
);
}
@Test
public
void
testCalculateByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
);
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
calculateUnchecked
(
input
.
array
(),
input
.
arrayOffset
()
+
5
,
10
);
}
};
hash
.
calculate
(
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
@Test
public
void
testCalculateReadOnlyByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
).
asReadOnlyBuffer
();
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
calculateUnchecked
(
withInstanceOf
(
byte
[].
class
),
0
,
10
);
}
};
hash
.
calculate
(
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
}
pulsar-checksum/src/test/java/com/scurrilous/circe/impl/AbstractStatelessLongHashTest.java
0 → 100644
浏览文件 @
554f0505
/*******************************************************************************
* Copyright 2014 Trevor Robinson
*
* 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.scurrilous.circe.impl
;
import
static
org
.
testng
.
Assert
.*;
import
java.nio.ByteBuffer
;
import
mockit.Expectations
;
import
mockit.Mocked
;
import
org.testng.annotations.Test
;
import
com.scurrilous.circe.impl.AbstractStatelessLongHash
;
@SuppressWarnings
(
"javadoc"
)
public
class
AbstractStatelessLongHashTest
{
@Mocked
private
AbstractStatelessLongHash
hash
;
@Test
public
void
testCalculateByteArray
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
calculateUnchecked
(
input
,
0
,
input
.
length
);
}
};
hash
.
calculate
(
input
);
}
@Test
public
void
testCalculateByteArrayIntInt
()
{
final
byte
[]
input
=
new
byte
[
10
];
new
Expectations
(
hash
)
{
{
hash
.
calculateUnchecked
(
input
,
2
,
4
);
}
};
hash
.
calculate
(
input
,
2
,
4
);
}
@Test
public
void
testCalculateByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
);
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
calculateUnchecked
(
input
.
array
(),
input
.
arrayOffset
()
+
5
,
10
);
}
};
hash
.
calculate
(
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
@Test
public
void
testCalculateReadOnlyByteBuffer
()
{
final
ByteBuffer
input
=
ByteBuffer
.
allocate
(
20
).
asReadOnlyBuffer
();
input
.
position
(
5
);
input
.
limit
(
15
);
new
Expectations
(
hash
)
{
{
hash
.
calculateUnchecked
(
withInstanceOf
(
byte
[].
class
),
0
,
10
);
}
};
hash
.
calculate
(
input
);
assertEquals
(
input
.
limit
(),
input
.
position
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录