You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -166,11 +166,14 @@ public byte[] decode(final byte[] encoded, final int index) {
166
166
Objects.requireNonNull(encoded);
167
167
168
168
if ((encoded.length - index) < (codeVarint.length + 1)) {
169
-
thrownewIllegalArgumentException("The value to decode must be non empty byte array, min length = " + (codeVarint.length + 1) + ", actual = " + encoded.length + ".");
169
+
thrownewIllegalArgumentException(
170
+
"The value to decode must be a non-empty byte array with a minimum length of "
171
+
+ (codeVarint.length + 1) + " bytes, but the actual length is " + encoded.length + " bytes.");
170
172
}
171
173
172
174
if (!IntStream.range(0, codeVarint.length).allMatch(i -> codeVarint[i] == encoded[i + index])) {
173
-
thrownewIllegalArgumentException("The value to decode is not encoded with " + toString() + ".");
175
+
thrownewIllegalArgumentException(
176
+
"The provided value is not encoded with this codec: " + toString() + ".");
174
177
}
175
178
176
179
returnArrays.copyOfRange(encoded, index + codeVarint.length, encoded.length - index);
@@ -53,29 +95,51 @@ public byte[] encode(final byte[] value) {
53
95
returnencoded;
54
96
}
55
97
98
+
/**
99
+
* Decodes a multihash value starting from the given index.
100
+
*
101
+
* <p>
102
+
* Validates that the encoded data matches this multihash's code, then reads the
103
+
* declared digest length and verifies it matches the actual remaining data
104
+
* length before returning the digest bytes.
105
+
* </p>
106
+
*
107
+
* @param encoded the multihash-encoded byte array
108
+
* @param index the starting index (inclusive)
109
+
* @return the decoded hash digest bytes
110
+
* @throws NullPointerException if {@code encoded} is {@code null}
111
+
* @throws IllegalArgumentException if the encoded data is too short, not
112
+
* encoded with this multihash's code, or the
113
+
* declared digest length does not match the
114
+
* actual data length
115
+
*/
56
116
@Override
57
117
publicbyte[] decode(byte[] encoded, intindex) {
58
118
59
119
Objects.requireNonNull(encoded);
60
120
61
121
if ((encoded.length - index) < (codeVarint.length + 2)) {
62
-
thrownewIllegalArgumentException("The value to decode must be non empty byte array, min length = " + (codeVarint.length + 2) + ", actual = " + encoded.length + ".");
122
+
thrownewIllegalArgumentException(
123
+
"The value to decode must be a non-empty byte array with a minimum length of "
124
+
+ (codeVarint.length + 2) + " bytes, but the actual length is " + encoded.length + " bytes.");
63
125
}
64
126
65
127
if (!IntStream.range(0, codeVarint.length).allMatch(i -> codeVarint[i] == encoded[i + index])) {
66
-
thrownewIllegalArgumentException("The value to decode is not encoded with " + toString() + ".");
128
+
thrownewIllegalArgumentException(
129
+
"The provided value is not encoded with this multihash: " + toString() + ".");
67
130
}
68
131
69
-
// digest size
132
+
// Get digest size
70
133
longsize = UVarInt.decode(encoded, index + codeVarint.length);
71
134
intsizeVarintLength = UVarInt.byteLength(size);
72
135
73
136
if (size != (encoded.length - index - codeVarint.length - sizeVarintLength)) {
74
137
thrownewIllegalArgumentException(
75
-
"The declared digest size = " + size + " and the actual hash digest size = " + (encoded.length - index - codeVarint.length - sizeVarintLength) + " do not match.");
138
+
"Digest size mismatch: declared size is " + size + " bytes, but the actual digest size is " +
139
+
(encoded.length - index - codeVarint.length - sizeVarintLength) + " bytes.");
76
140
}
77
141
78
-
// digest
142
+
// Extract digest
79
143
returnArrays.copyOfRange(encoded, index + codeVarint.length + sizeVarintLength, encoded.length - index);
0 commit comments