Skip to content

Commit 6f5d4b8

Browse files
committed
Minor improvements
1 parent f725748 commit 6f5d4b8

2 files changed

Lines changed: 7 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,12 @@ if (MultihashCodec.SHA2_384.isEncoded(encoded)) {
109109
...
110110
}
111111

112-
113-
/* get registry initialized with all multihash codecs */
114-
var registry = MulticodecRegistry.getInstance(Tag.Multihash);
115-
116112
/* determine digest index */
117-
var index = registry.getCodec(encoded)
113+
var index = decoder.getCodec(encoded)
118114
.map(Multihash.class::cast)
119-
.mapToInt(codec -> codec.length() + codec.digestLength(encoded))
115+
.mapToInt(codec -> encoded.length - codec.digestLength(encoded))
120116
.orElseThrow(() -> new IllegalArgumentException("Unsupported multihash."));
121117

122-
/* encode an input as multihash */
123-
byte[] encoded = registry.getCodec(code)
124-
.map(codec -> codec.encode(input))
125-
.orElseThrow(() -> new IllegalArgumentException("Unsupported multihash."));
126118
```
127119

128120
## Installation

src/test/java/com/apicatalog/multihash/MultihashTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.apicatalog.multicodec.Multicodec.Tag;
1515
import com.apicatalog.multicodec.MulticodecDecoder;
1616
import com.apicatalog.multicodec.codec.MultihashCodec;
17+
import com.apicatalog.uvarint.UVarInt;
1718

1819
class MultihashTest {
1920

@@ -66,6 +67,8 @@ void testDecodeFromIndex(byte[] input, Multihash codec, byte[] expected) {
6667

6768
System.arraycopy(input, 0, array, index, input.length);
6869
assertArrayEquals(expected, codec.decode(array, index));
70+
assertEquals(expected.length, codec.digestLength(array, index));
71+
assertEquals(codec.length() + UVarInt.byteLength(expected.length), input.length - codec.digestLength(array, index));
6972
}
7073

7174
@ParameterizedTest(name = "{index}")
@@ -80,6 +83,8 @@ void testDecodeRange(byte[] input, Multihash codec, byte[] expected) {
8083

8184
System.arraycopy(input, 0, array, index, input.length);
8285
assertArrayEquals(expected, codec.decode(array, index, input.length));
86+
assertEquals(expected.length, codec.digestLength(array, index));
87+
assertEquals(codec.length() + UVarInt.byteLength(expected.length), input.length - codec.digestLength(array, index));
8388
}
8489

8590
@ParameterizedTest(name = "{index}")

0 commit comments

Comments
 (0)