1010import com .apicatalog .base .Base58 ;
1111
1212/**
13- * Represents multibase encoding.
13+ * Represents a multibase encoding format.
14+ * <p>
15+ * Multibase is a convention for identifying which base encoding has been used
16+ * to encode binary data by prefixing the encoded string with a unique
17+ * character. This allows for consistent and unambiguous decoding across
18+ * different base encodings, such as Base16, Base32, Base58, and Base64
19+ * variants.
20+ * </p>
21+ * <p>
22+ * Each {@code Multibase} instance includes:
23+ * <ul>
24+ * <li>A unique base name (e.g., {@code base64url})</li>
25+ * <li>A single-character prefix used to identify the base in encoded data</li>
26+ * <li>The alphabet size (e.g., 32, 58, 64)</li>
27+ * <li>Functions for encoding and decoding</li>
28+ * </ul>
29+ * </p>
30+ *
31+ * @see <a href="https://github.com/multiformats/multibase">Multibase
32+ * specification</a>
1433 */
1534public class Multibase {
1635
@@ -94,17 +113,26 @@ public class Multibase {
94113 Multibase .BASE_32_HEX_PAD_UPPER ,
95114 Multibase .BASE_16 ,
96115 Multibase .BASE_16_UPPER ,
97- Multibase .BASE_2
116+ Multibase .BASE_2
98117 };
99-
118+
100119 protected final String name ;
101-
120+
102121 protected final char prefix ;
103122 protected final int length ;
104123
105124 protected final Function <String , byte []> decode ;
106125 protected final Function <byte [], String > encode ;
107126
127+ /**
128+ * Constructs a {@code Multibase} instance.
129+ *
130+ * @param name the unique base name (e.g., "base64urlpad")
131+ * @param prefix the unique prefix character indicating the base
132+ * @param length the base alphabet length
133+ * @param decode the decoding function
134+ * @param encode the encoding function
135+ */
108136 public Multibase (
109137 String name ,
110138 char prefix ,
@@ -118,6 +146,7 @@ public Multibase(
118146 this .name = name ;
119147 }
120148
149+ @ Deprecated
121150 public Multibase (
122151 char prefix ,
123152 int length ,
@@ -131,37 +160,39 @@ public Multibase(
131160 }
132161
133162 /**
134- * A unique codec name, all lower case, only <code>ALPHA+ALPHANUM*</code>
135- * @return
163+ * Returns the unique base name of this multibase (e.g., {@code base64url}).
164+ *
165+ * @return the multibase name
136166 */
137167 public String name () {
138168 return name ;
139169 }
140-
170+
141171 /**
142- * A unique prefix identifying base encoding in encoded value.
143- *
144- * @return the base encoding unique prefix
172+ * Returns the unique prefix character used to identify this multibase in
173+ * encoded strings.
174+ *
175+ * @return the multibase prefix character
145176 */
146177 public char prefix () {
147178 return prefix ;
148179 }
149180
150181 /**
151- * An encoding alphabet length. e.g. 32, 58, 64.
152- *
153- * @return the encoding alphabet length
182+ * Returns the length of the base alphabet ( e.g., 32, 58, 64) .
183+ *
184+ * @return the length of the encoding alphabet
154185 */
155186 public int length () {
156187 return length ;
157188 }
158189
159190 /**
160- * Checks if the given value is encoded with the base .
161- *
162- * @param encoded an encoded value to test
163- * @return < code> true</code> is the given value is encoded with the base ,
164- * < code> false</code> otherwise
191+ * Checks whether the given string is encoded with this multibase .
192+ *
193+ * @param encoded the string to test
194+ * @return {@ code true} if the string is encoded using this multibase ,
195+ * {@ code false} otherwise
165196 */
166197 public boolean isEncoded (final String encoded ) {
167198 return encoded != null
@@ -170,10 +201,12 @@ public boolean isEncoded(final String encoded) {
170201 }
171202
172203 /**
173- * Decodes the given data into byte array.
174- *
175- * @param encoded to decodes
176- * @return encoded data as byte array
204+ * Decodes the given multibase-encoded string into a byte array.
205+ *
206+ * @param encoded the multibase-encoded string
207+ * @return the decoded byte array
208+ * @throws IllegalArgumentException if the input is {@code null}, empty, or has
209+ * an incorrect prefix
177210 */
178211 public byte [] decode (final String encoded ) {
179212 if (encoded == null ) {
@@ -196,10 +229,11 @@ public byte[] decode(final String encoded) {
196229 }
197230
198231 /**
199- * Encodes the given data into base encoded string.
200- *
201- * @param data to encode
202- * @return a string representing the encoded data
232+ * Encodes the given byte array into a multibase-encoded string.
233+ *
234+ * @param data the byte array to encode
235+ * @return the encoded string, including the base prefix
236+ * @throws IllegalArgumentException if the input is {@code null} or empty
203237 */
204238 public String encode (byte [] data ) {
205239
@@ -231,11 +265,21 @@ public boolean equals(Object obj) {
231265 return prefix == other .prefix ;
232266 }
233267
268+ /**
269+ * Returns a string representation of this {@code Multibase}.
270+ *
271+ * @return a string containing the prefix and base length
272+ */
234273 @ Override
235274 public String toString () {
236275 return "Multibase [prefix=" + prefix + ", length=" + length + "]" ;
237276 }
238-
277+
278+ /**
279+ * Returns a list of all provided {@code Multibase} instances.
280+ *
281+ * @return an array of supported multibase instances
282+ */
239283 public static Multibase [] provided () {
240284 return ALL ;
241285 }
0 commit comments