Skip to content

Commit 0230586

Browse files
Syntactic sugar on decode api
1 parent bf0297b commit 0230586

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ try {
4848
}
4949
if (decodedData != null) {
5050
System.out.printf("Codec:%s(%s)%n", decodedData.getCodec().name(), decodedData.getCodec().code);
51-
System.out.println("byte data length:" + decodedData.getByteData().length);
52-
System.out.println("hex data:" + decodedData.getHexData());
51+
System.out.println("byte data length:" + decodedData.getDataAsBytes().length);
52+
System.out.println("hex data:" + decodedData.getDataAsHex());
5353
}
5454
```
5555

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,74 @@
11
package nz.co.identityfoundry.ddi.did.multicodec;
22

3+
import nz.co.identityfoundry.ddi.did.util.HexUtils;
4+
35
/**
4-
* A bean to encapsulate decoded data
6+
* <p>A bean to encapsulate decoded data and its codec.</p>
7+
* <p>The bean provides access to an array of bytes to express the decoded data payload.
8+
* The data can set/get either as a byte[] or as a hex string in such a way as the operations are consistent ways to act
9+
* on the same payload of data.</p>
510
*/
611
public class DecodedData {
712
private Multicodec codec;
8-
private byte[] byteData;
9-
private String hexData;
13+
private byte[] data;
1014

1115
public DecodedData() {
1216
}
1317

18+
/**
19+
* Gets the Multicodec value of the data.
20+
* @return the Multicodec value of the data.
21+
*/
1422
public Multicodec getCodec() {
1523
return codec;
1624
}
1725

26+
/**
27+
* Sets the Multicodec value of the data.
28+
* @param codec the Multicodec value of the data.
29+
*/
1830
public void setCodec(Multicodec codec) {
1931
this.codec = codec;
2032
}
2133

22-
public byte[] getByteData() {
23-
return byteData;
34+
/**
35+
* Gets the data as a byte array.
36+
* @return the data as a byte array.
37+
*/
38+
public byte[] getDataAsBytes() {
39+
return data;
2440
}
2541

26-
public void setByteData(byte[] byteData) {
27-
this.byteData = byteData;
42+
/**
43+
* Sets the data.
44+
* @param data the data as a byte array.
45+
*/
46+
public void setData(byte[] data) {
47+
this.data = data;
2848
}
2949

30-
public String getHexData() {
31-
return hexData;
50+
/**
51+
* Alternative method to get the data as a hex string.
52+
* @return get the data as a hex string.
53+
*/
54+
public String getDataAsHex() {
55+
return HexUtils.bytesToHex(this.data);
3256
}
3357

34-
public void setHexData(String hexData) {
35-
this.hexData = hexData;
58+
/**
59+
* Alternative method to set the data as a hex string.
60+
* @param hexData the data as a hex string.
61+
*/
62+
public void setData(String hexData) {
63+
setData(HexUtils.hexToBytes(hexData));
3664
}
3765

66+
/**
67+
* Determines if this object is populated with non-null information.
68+
* @return true, if this object is populated with non-null information.
69+
*/
3870
public boolean isSet() {
39-
return (getCodec() != null) && (getByteData() != null && getByteData().length > 0);
71+
return (getCodec() != null) && (getDataAsBytes() != null && getDataAsBytes().length > 0);
4072

4173
}
4274
}

src/main/java/nz/co/identityfoundry/ddi/did/multicodec/MulticodecEncoder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ public static DecodedData decode(byte[] multicodecEncodedData) throws AmbiguousC
9999
int codeLength = HexUtils.hexToBytes(c.code).length;
100100
if ((codeLength == 1) && (multicodecEncodedDataHex.startsWith(c.uvarintcode))) {
101101
data.setCodec(c);
102-
String dataHex = StringUtils.removeStart(multicodecEncodedDataHex, c.uvarintcode);
103-
data.setByteData(HexUtils.hexToBytes(dataHex));
104-
data.setHexData(dataHex);
102+
data.setData(StringUtils.removeStart(multicodecEncodedDataHex, c.uvarintcode));
105103
break;
106104
}
107105
}

src/test/java/nz/co/identityfoundry/ddi/did/multicodec/MulticodecEncoderFullTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ public void testDecodeSingleByteCodec() {
574574
}
575575
assertNotNull("DecodedData was not null", decodedData);
576576
assertEquals(String.format("Expected codec %s, but got %s", codec.name(), decodedData.getCodec().name()), codec.name(), decodedData.getCodec().name());
577-
assertArrayEquals(String.format("Expected data %s, but got %s", HexUtils.bytesToHex(raw), decodedData.getHexData()), raw, decodedData.getByteData());
577+
assertArrayEquals(String.format("Expected data %s, but got %s", HexUtils.bytesToHex(raw), decodedData.getDataAsHex()), raw, decodedData.getDataAsBytes());
578578
}
579579
}
580580

0 commit comments

Comments
 (0)