Skip to content

Commit 8c51487

Browse files
committed
Improve JavaDoc
1 parent ecf515f commit 8c51487

2 files changed

Lines changed: 121 additions & 64 deletions

File tree

src/main/java/com/apicatalog/multibase/Multibase.java

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,26 @@
1010
import 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
*/
1534
public 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
}

src/main/java/com/apicatalog/multibase/MultibaseDecoder.java

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,44 @@
66
import java.util.function.Function;
77
import java.util.stream.Collectors;
88

9+
/**
10+
* Decodes multibase-encoded strings using the {@link Multibase} encodings
11+
* explicitly registered via prefix characters.
12+
* <p>
13+
* The decoder maps prefix characters (e.g., {@code 'z'}, {@code 'm'},
14+
* {@code 'f'}) to {@link Multibase} instances. When decoding, it uses the first
15+
* character of the input string to select the matching encoding and decode the
16+
* remaining data.
17+
* </p>
18+
*/
919
public class MultibaseDecoder {
1020

1121
protected final Map<Character, Multibase> bases;
1222

23+
/**
24+
* Constructs a {@code MultibaseDecoder} with the provided base prefix mappings.
25+
*
26+
* @param bases a map of prefix characters to {@link Multibase} instances
27+
*/
1328
protected MultibaseDecoder(final Map<Character, Multibase> bases) {
1429
this.bases = bases;
1530
}
1631

1732
/**
18-
* A new instance initialized with all supported bases.
19-
*
20-
* @return a new instance
33+
* Creates a {@code MultibaseDecoder} instance configured with the encodings
34+
* returned by {@link Multibase#provided()}.
35+
*
36+
* @return a new decoder instance
2137
*/
2238
public static MultibaseDecoder getInstance() {
23-
return getInstance(
24-
Multibase.BASE_58_BTC,
25-
Multibase.BASE_64,
26-
Multibase.BASE_64_PAD,
27-
Multibase.BASE_64_URL,
28-
Multibase.BASE_64_URL_PAD,
29-
Multibase.BASE_32,
30-
Multibase.BASE_32_UPPER,
31-
Multibase.BASE_32_PAD,
32-
Multibase.BASE_32_PAD_UPPER,
33-
Multibase.BASE_32_HEX,
34-
Multibase.BASE_32_HEX_UPPER,
35-
Multibase.BASE_32_HEX_PAD,
36-
Multibase.BASE_32_HEX_PAD_UPPER,
37-
Multibase.BASE_16,
38-
Multibase.BASE_16_UPPER,
39-
Multibase.BASE_2);
39+
return getInstance(Multibase.provided());
4040
}
4141

4242
/**
43-
* A new instance initialized with the given bases.
44-
*
45-
* @param bases to initialize the decoder
46-
* @return a new instance
43+
* Creates a {@code MultibaseDecoder} using only the provided encodings.
44+
*
45+
* @param bases the encodings to register for decoding
46+
* @return a new decoder instance
4747
*/
4848
public static MultibaseDecoder getInstance(final Multibase... bases) {
4949
return new MultibaseDecoder(
@@ -52,10 +52,25 @@ public static MultibaseDecoder getInstance(final Multibase... bases) {
5252
}
5353

5454
/**
55-
* Tries to detect the base used for encoding.
56-
*
57-
* @param encoded an encoded value
58-
* @return detected base encoding or {@link Optional#empty()}
55+
* Returns the {@link Multibase} encoding associated with the given prefix
56+
* character, if it was registered when this decoder was created.
57+
*
58+
* @param prefix the multibase prefix character
59+
* @return an {@code Optional} containing the corresponding {@link Multibase},
60+
* or empty if not registered
61+
*/
62+
public Optional<Multibase> getBase(final char prefix) {
63+
return Optional.ofNullable(bases.get(prefix));
64+
}
65+
66+
/**
67+
* Detects the encoding used in the given string based on the prefix character,
68+
* and returns the corresponding {@link Multibase} if it was registered.
69+
*
70+
* @param encoded a multibase-encoded string
71+
* @return an {@code Optional} containing the corresponding {@link Multibase},
72+
* or empty if not registered
73+
* @throws IllegalArgumentException if {@code encoded} is null or empty
5974
*/
6075
public Optional<Multibase> getBase(final String encoded) {
6176

@@ -69,17 +84,15 @@ public Optional<Multibase> getBase(final String encoded) {
6984

7085
return getBase(encoded.charAt(0));
7186
}
72-
87+
7388
/**
74-
* Returns a base associated with the given prefix.
75-
*
76-
* @param prefix associated with the base encoding
77-
* @return base encoding or {@link Optional#empty()}
89+
* Decodes a multibase-encoded string into a byte array.
90+
*
91+
* @param encoded the encoded string to decode
92+
* @return the decoded byte array
93+
* @throws IllegalArgumentException if the encoding is not registered or input
94+
* is invalid
7895
*/
79-
public Optional<Multibase> getBase(final char prefix) {
80-
return Optional.ofNullable(bases.get(prefix));
81-
}
82-
8396
public byte[] decode(final String encoded) {
8497
return getBase(encoded)
8598
.map(base -> base.decode(encoded))

0 commit comments

Comments
 (0)