Skip to content

Commit cc6189b

Browse files
authored
Merge pull request #106 from filip26/feat/codecs
v2.1.0
2 parents a8b96cb + 1ef33f3 commit cc6189b

17 files changed

Lines changed: 415 additions & 172 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ To include Copper Multicodec in your project, add the following dependency to yo
110110
<dependency>
111111
<groupId>com.apicatalog</groupId>
112112
<artifactId>copper-multicodec</artifactId>
113-
<version>2.0.0</version>
113+
<version>2.1.0</version>
114114
</dependency>
115115
```
116116

pom.xml

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.apicatalog</groupId>
77
<artifactId>copper-multicodec</artifactId>
8-
<version>2.0.0</version>
8+
<version>2.1.0</version>
99
<packaging>jar</packaging>
1010
<url>https://github.com/filip26/copper-multicodec</url>
1111
<scm>
@@ -159,42 +159,19 @@
159159
</execution>
160160
</executions>
161161
</plugin>
162-
<!--
163-
<plugin>
164-
<groupId>org.sonatype.plugins</groupId>
165-
<artifactId>nexus-staging-maven-plugin</artifactId>
166-
<version>1.7.0</version>
167-
<extensions>true</extensions>
168-
<configuration>
169-
<serverId>ossrh</serverId>
170-
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
171-
<autoReleaseAfterClose>true</autoReleaseAfterClose>
172-
</configuration>
173-
</plugin>
174-
-->
175-
<plugin>
176-
<groupId>org.sonatype.central</groupId>
177-
<artifactId>central-publishing-maven-plugin</artifactId>
178-
<version>0.8.0</version>
179-
<extensions>true</extensions>
180-
<configuration>
181-
<publishingServerId>central</publishingServerId>
182-
<tokenAuth>true</tokenAuth>
183-
<autoPublish>true</autoPublish>
184-
</configuration>
185-
</plugin>
162+
<plugin>
163+
<groupId>org.sonatype.central</groupId>
164+
<artifactId>central-publishing-maven-plugin</artifactId>
165+
<version>0.8.0</version>
166+
<extensions>true</extensions>
167+
<configuration>
168+
<publishingServerId>central</publishingServerId>
169+
<tokenAuth>true</tokenAuth>
170+
<autoPublish>true</autoPublish>
171+
</configuration>
172+
</plugin>
186173
</plugins>
187174
</build>
188-
<distributionManagement>
189-
<repository>
190-
<id>ossrh</id>
191-
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
192-
</repository>
193-
<snapshotRepository>
194-
<id>ossrh</id>
195-
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
196-
</snapshotRepository>
197-
</distributionManagement>
198175
</profile>
199176
</profiles>
200177
</project>

src/gen/java/com/apicatalog/multicodec/CodecTag.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public static void generate(final String tag, final String className, final Clas
5454
writer.println();
5555
});
5656

57-
writer.print(" protected static final Map<Long,");
58-
writer.print(clazz.getSimpleName());
57+
writer.print(" protected static final Map<Long,Multicodec");
58+
// writer.print(clazz.getSimpleName());
5959
writer.println("> ALL = new TreeMap<>();");
6060
writer.println();
6161
writer.println(" static {");

src/main/java/com/apicatalog/multicodec/Multicodec.java

Lines changed: 129 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,28 @@
77
import com.apicatalog.uvarint.UVarInt;
88

99
/**
10-
* Represents a multicodec definition and encoder/decoder instance.
10+
* Represents a
11+
* <a href="https://github.com/multiformats/multicodec">multicodec</a>
12+
* definition and provides encoding/decoding operations using its varint code.
13+
*
14+
* <p>
15+
* A multicodec is a self-describing format where the encoded data is prefixed
16+
* with a varint code identifying the format. This class stores metadata about
17+
* the codec, including its name, tag, status, and binary varint code, and can
18+
* be used to:
19+
* <ul>
20+
* <li>Encode raw byte arrays with the codec prefix</li>
21+
* <li>Check if a value is encoded with this codec</li>
22+
* <li>Decode a previously encoded value</li>
23+
* </ul>
24+
*
25+
* <p>
26+
* Instances of this class are immutable and thread-safe.
1127
*/
1228
public class Multicodec {
1329

1430
/**
15-
* Recognized multicodec tags
31+
* Categories of recognized multicodec tags.
1632
*/
1733
public enum Tag {
1834
Key,
@@ -28,7 +44,7 @@ public enum Tag {
2844
}
2945

3046
/**
31-
* Common registration status
47+
* Registration status of the codec.
3248
*/
3349
public enum Status {
3450
Deprecated,
@@ -42,6 +58,15 @@ public enum Status {
4258
protected final Tag tag;
4359
protected final Status status;
4460

61+
/**
62+
* Constructs a new {@code Multicodec}.
63+
*
64+
* @param name the codec name
65+
* @param tag the codec category tag
66+
* @param code the codec numeric code
67+
* @param uvarint the varint-encoded form of the code
68+
* @param status the registration status
69+
*/
4570
protected Multicodec(String name, Tag tag, long code, byte[] uvarint, Status status) {
4671
this.tag = tag;
4772
this.name = name;
@@ -50,52 +75,45 @@ protected Multicodec(String name, Tag tag, long code, byte[] uvarint, Status sta
5075
this.status = status;
5176
}
5277

78+
/**
79+
* Creates a new {@code Multicodec} with no explicit status.
80+
*
81+
* @param name the codec name
82+
* @param tag the codec category tag
83+
* @param code the codec numeric code
84+
* @return a new {@code Multicodec} instance
85+
*/
5386
public static Multicodec of(String name, Tag tag, long code) {
5487
return of(name, tag, code, null);
5588
}
5689

90+
/**
91+
* Creates a new {@code Multicodec}.
92+
*
93+
* @param name the codec name
94+
* @param tag the codec category tag
95+
* @param code the codec numeric code
96+
* @param status the codec registration status
97+
* @return a new {@code Multicodec} instance
98+
*/
5799
public static Multicodec of(String name, Tag tag, long code, Status status) {
58100
return new Multicodec(name, tag, code, UVarInt.encode(code), status);
59101
}
60102

61-
public int length() {
62-
return codeVarint.length;
63-
}
64-
65-
public byte[] varint() {
66-
return codeVarint;
67-
}
68-
69-
public Tag tag() {
70-
return tag;
71-
}
72-
73-
public String name() {
74-
return name;
75-
}
76-
77-
public long code() {
78-
return code;
79-
}
80-
81-
public Status status() {
82-
return status;
83-
}
84-
85103
/**
86-
* Encode a value with a codec.
87-
*
88-
* @param value a value to encode
89-
* @return an encoded value
104+
* Encodes the given value by prefixing it with this codec's varint code.
90105
*
91-
* @throws IllegalArgumentException if the value cannot be encoded
106+
* @param value the non-empty byte array to encode
107+
* @return the encoded byte array (varint prefix + original value)
108+
* @throws NullPointerException if {@code value} is {@code null}
109+
* @throws IllegalArgumentException if {@code value} is empty
92110
*/
93111
public byte[] encode(final byte[] value) {
94112

95113
Objects.requireNonNull(value);
96114

97115
if (value.length == 0) {
98-
throw new IllegalArgumentException("The value to encode must be non empty byte array.");
116+
throw new IllegalArgumentException("The value to encode must be a non-empty byte array.");
99117
}
100118

101119
final byte[] encoded = new byte[codeVarint.length + value.length];
@@ -107,11 +125,11 @@ public byte[] encode(final byte[] value) {
107125
}
108126

109127
/**
110-
* Checks if the given value is encoded with the codec.
128+
* Checks if the given encoded byte array starts with this codec's varint code.
111129
*
112-
* @param encoded an encoded value to test
113-
* @return <code>true</code> is the given value is encoded with the codec,
114-
* <code>false</code> otherwise
130+
* @param encoded the byte array to test
131+
* @return {@code true} if {@code encoded} starts with this codec's varint
132+
* prefix, {@code false} otherwise
115133
*/
116134
public boolean isEncoded(final byte[] encoded) {
117135
return encoded != null
@@ -120,36 +138,42 @@ public boolean isEncoded(final byte[] encoded) {
120138
}
121139

122140
/**
123-
* Decode an encoded value
124-
*
125-
* @param encoded value to decode
126-
* @return a decoded value
141+
* Decodes an encoded value by removing this codec's varint prefix.
127142
*
128-
* @throws IllegalArgumentException if the encoded value cannot be decoded
143+
* @param encoded the encoded value
144+
* @return the decoded (original) byte array
145+
* @throws NullPointerException if {@code encoded} is {@code null}
146+
* @throws IllegalArgumentException if {@code encoded} is too short or does not
147+
* begin with this codec's varint prefix
129148
*/
130149
public byte[] decode(final byte[] encoded) {
131150
return decode(encoded, 0);
132151
}
133-
152+
134153
/**
135-
* Decode an encoded value
136-
*
137-
* @param encoded value to decode
138-
* @param index an index from which to start (included)
139-
* @return a decoded value
154+
* Decodes an encoded value starting from a given index.
140155
*
141-
* @throws IllegalArgumentException if the encoded value cannot be decoded
156+
* @param encoded the encoded value
157+
* @param index the starting index (inclusive)
158+
* @return the decoded (original) byte array
159+
* @throws NullPointerException if {@code encoded} is {@code null}
160+
* @throws IllegalArgumentException if the value is too short from {@code index}
161+
* onward or does not begin with this codec's
162+
* varint prefix
142163
*/
143164
public byte[] decode(final byte[] encoded, final int index) {
144165

145166
Objects.requireNonNull(encoded);
146167

147168
if ((encoded.length - index) < (codeVarint.length + 1)) {
148-
throw new IllegalArgumentException("The value to decode must be non empty byte array, min length = " + (codeVarint.length + 1) + ", actual = " + encoded.length + ".");
169+
throw new IllegalArgumentException(
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.");
149172
}
150173

151174
if (!IntStream.range(0, codeVarint.length).allMatch(i -> codeVarint[i] == encoded[i + index])) {
152-
throw new IllegalArgumentException("The value to decode is not encoded with " + toString() + ".");
175+
throw new IllegalArgumentException(
176+
"The provided value is not encoded with this codec: " + toString() + ".");
153177
}
154178

155179
return Arrays.copyOfRange(encoded, index + codeVarint.length, encoded.length - index);
@@ -176,4 +200,58 @@ public boolean equals(Object obj) {
176200
public String toString() {
177201
return "Multicodec [name=" + name + ", tag=" + tag + ", code=" + code + "]";
178202
}
203+
204+
/**
205+
* Returns the length of this codec's varint code in bytes.
206+
*
207+
* @return varint length
208+
*/
209+
public int length() {
210+
return codeVarint.length;
211+
}
212+
213+
/**
214+
* Returns the varint-encoded form of this codec's code.
215+
*
216+
* @return varint byte array
217+
*/
218+
public byte[] varint() {
219+
return codeVarint;
220+
}
221+
222+
/**
223+
* Returns this codec's category tag.
224+
*
225+
* @return the tag
226+
*/
227+
public Tag tag() {
228+
return tag;
229+
}
230+
231+
/**
232+
* Returns this codec's name.
233+
*
234+
* @return the name
235+
*/
236+
public String name() {
237+
return name;
238+
}
239+
240+
/**
241+
* Returns this codec's numeric code.
242+
*
243+
* @return the numeric code
244+
*/
245+
public long code() {
246+
return code;
247+
}
248+
249+
/**
250+
* Returns this codec's registration status.
251+
*
252+
* @return the status, or {@code null} if unspecified
253+
*/
254+
public Status status() {
255+
return status;
256+
}
179257
}

0 commit comments

Comments
 (0)