Skip to content

Commit 472d743

Browse files
committed
Add tests and doc to checksum feature
1 parent 10898ee commit 472d743

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* add bitAt() utility
66
* add hash feature
7+
* add checksum transformer
78

89
## v0.4.0
910

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ Bytes hash = Bytes.wrap(array).sha256();
201201
Bytes hash = Bytes.wrap(array).hash("MD5");
202202
```
203203

204+
**Checksum** can be calculated or automatically appended:
205+
206+
```java
207+
import static at.favre.lib.bytes.BytesTransformers.*;
208+
209+
Bytes.wrap(array).transform(checksumAppendCrc32());
210+
Bytes.wrap(array).transform(checksumCrc32());
211+
Bytes.wrap(array).transform(checksum(new Adler32(), ChecksumTransformer.Mode.TRANSFORM, 4));
212+
```
213+
204214
Other transformers:
205215

206216
```java
@@ -289,13 +299,15 @@ The `toString()` methods only shows the length and a preview of maximal 8 bytes:
289299
A simple validation framework which can be used to check the internal byte array:
290300

291301
```java
292-
Bytes.wrap(new byte[]{8, 3, 9}.validate(BytesValidators.startsWith((byte) 8), BytesValidators.atLeast(3)); // true
302+
import static at.favre.lib.bytes.BytesValidators.*;
303+
304+
Bytes.wrap(new byte[]{8, 3, 9}.validate(startsWith((byte) 8), atLeast(3)); // true
293305
```
294306

295307
This is especially convenient when combining validators:
296308

297309
```java
298-
Bytes.wrap(new byte[]{0, 1}.validate(BytesValidators.atMost(2), BytesValidators.notOnlyOf((byte) 0)); // true
310+
Bytes.wrap(new byte[]{0, 1}.validate(atMost(2), notOnlyOf((byte) 0)); // true
299311
```
300312

301313
Validators also support nestable logical expressions AND, OR as well as NOT:

src/main/java/at/favre/lib/bytes/BytesTransformers.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,49 @@
44
import java.util.zip.CRC32;
55
import java.util.zip.Checksum;
66

7+
/**
8+
* Collection of additional {@link BytesTransformer} for more specific use cases
9+
*/
710
public final class BytesTransformers {
811

912
private BytesTransformers() {
1013
}
1114

12-
public static BytesTransformer appendCrc32() {
15+
/**
16+
* Create a {@link BytesTransformer} which appends 4 byte Crc32 checksum to given bytes
17+
*
18+
* @return transformer
19+
*/
20+
public static BytesTransformer checksumAppendCrc32() {
1321
return new ChecksumTransformer(new CRC32(), ChecksumTransformer.Mode.APPEND, 4);
1422
}
1523

24+
/**
25+
* Create a {@link BytesTransformer} which transforms to 4 byte Crc32 checksum of given bytes
26+
*
27+
* @return transformer
28+
*/
29+
public static BytesTransformer checksumCrc32() {
30+
return new ChecksumTransformer(new CRC32(), ChecksumTransformer.Mode.TRANSFORM, 4);
31+
}
32+
33+
/**
34+
* Create a {@link BytesTransformer} which transforms to 4 byte Crc32 checksum of given bytes
35+
* @return transformer
36+
*/
37+
/**
38+
* Create a {@link BytesTransformer} which transforms to 4 byte Crc32 checksum of given bytes
39+
*
40+
* @param checksum used algorithm
41+
* @param mode mode (append or convert)
42+
* @param checksumLengthByte the byte length of the checksum; the {@link Checksum} class always returns 8 byte, but some
43+
* checksum algorithms (e.g. CRC32) only require smaller output. Must be between 1 and 8 byte.
44+
* @return transformer
45+
*/
46+
public static BytesTransformer checksum(Checksum checksum, ChecksumTransformer.Mode mode, int checksumLengthByte) {
47+
return new ChecksumTransformer(checksum, mode, checksumLengthByte);
48+
}
49+
1650
/**
1751
* Adds or converts to arbitrary checksum
1852
*/

src/test/java/at/favre/lib/bytes/BytesTransformTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
import java.nio.ByteBuffer;
2828
import java.security.SecureRandom;
2929
import java.util.Comparator;
30+
import java.util.zip.Adler32;
31+
import java.util.zip.CRC32;
32+
import java.util.zip.Checksum;
3033

34+
import static at.favre.lib.bytes.BytesTransformers.*;
3135
import static org.junit.Assert.*;
3236

3337
public class BytesTransformTest extends ABytesTest {
@@ -272,6 +276,19 @@ public void hash() throws Exception {
272276
assertEquals(Bytes.parseHex("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"), Bytes.from("").hashSha512());
273277
}
274278

279+
@Test
280+
public void checksumTest() throws Exception {
281+
Checksum crc32Checksum = new CRC32();
282+
crc32Checksum.update(example2_bytes_seven, 0, example2_bytes_seven.length);
283+
assertEquals(crc32Checksum.getValue(), Bytes.from(example2_bytes_seven).transform(checksumCrc32()).resize(8).toLong());
284+
assertEquals(Bytes.from(example2_bytes_seven, Bytes.from(crc32Checksum.getValue()).resize(4).array()), Bytes.from(example2_bytes_seven).transform(checksumAppendCrc32()));
285+
286+
Checksum adlerChecksum = new Adler32();
287+
adlerChecksum.update(example2_bytes_seven, 0, example2_bytes_seven.length);
288+
assertEquals(Bytes.from(adlerChecksum.getValue()).resize(4),
289+
Bytes.from(example2_bytes_seven).transform(checksum(new Adler32(), ChecksumTransformer.Mode.TRANSFORM, 4)));
290+
}
291+
275292
@Test
276293
public void transform() throws Exception {
277294
assertArrayEquals(example_bytes_two, Bytes.from(example_bytes_two).transform(new BytesTransformer() {

0 commit comments

Comments
 (0)