Skip to content

Commit 72be8f7

Browse files
committed
Make Cid a subclass of Multihash for nice API upgrade.
1 parent 86bada1 commit 72be8f7

File tree

3 files changed

+57
-32
lines changed

3 files changed

+57
-32
lines changed

lib/multihash.jar

50 Bytes
Binary file not shown.

src/main/java/io/ipfs/cid/Cid.java

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55

66
import java.io.*;
77
import java.util.*;
8+
import java.util.stream.*;
9+
10+
public class Cid extends Multihash {
11+
12+
public static final class CidEncodingException extends RuntimeException {
13+
14+
public CidEncodingException(String message) {
15+
super(message);
16+
}
17+
}
818

9-
public class Cid {
1019
public enum Codec {
1120
Raw(0x55),
1221
DagProtobuf(0x70),
@@ -39,22 +48,31 @@ public static Codec lookup(long c) {
3948

4049
public final long version;
4150
public final Codec codec;
42-
public final Multihash hash;
4351

4452
public Cid(long version, Codec codec, Multihash hash) {
53+
super(hash);
4554
this.version = version;
4655
this.codec = codec;
47-
this.hash = hash;
56+
}
57+
58+
public Cid(long version, Codec codec, Multihash.Type type, byte[] hash) {
59+
super(type, hash);
60+
this.version = version;
61+
this.codec = codec;
62+
}
63+
64+
public Cid(Multihash h) {
65+
this(0, Codec.DagProtobuf, h);
4866
}
4967

5068
private byte[] toBytesV0() {
51-
return hash.toBytes();
69+
return super.toBytes();
5270
}
5371

54-
private static final int MAX_VARINT_LEN64 = 10;
72+
private static final int MAX_VARINT_LEN64 = 10;
5573

5674
private byte[] toBytesV1() {
57-
byte[] hashBytes = hash.toBytes();
75+
byte[] hashBytes = super.toBytes();
5876
byte[] res = new byte[2 * MAX_VARINT_LEN64 + hashBytes.length];
5977
int index = putUvarint(res, 0, version);
6078
index = putUvarint(res, index, codec.type);
@@ -73,7 +91,7 @@ else if (version == 1)
7391
@Override
7492
public String toString() {
7593
if (version == 0) {
76-
return hash.toString();
94+
return super.toString();
7795
} else if (version == 1) {
7896
return Multibase.encode(Multibase.Base.Base58BTC, toBytesV1());
7997
}
@@ -83,33 +101,38 @@ public String toString() {
83101
@Override
84102
public boolean equals(Object o) {
85103
if (this == o) return true;
86-
if (o == null || getClass() != o.getClass()) return false;
87-
88-
Cid cid = (Cid) o;
104+
if (! (o instanceof Multihash)) return false;
105+
if (!super.equals(o)) return false;
89106

90-
if (version != cid.version) return false;
91-
if (codec != cid.codec) return false;
92-
return hash != null ? hash.equals(cid.hash) : cid.hash == null;
107+
if (o instanceof Cid) {
108+
Cid cid = (Cid) o;
93109

110+
if (version != cid.version) return false;
111+
return codec == cid.codec;
112+
}
113+
// o must be a Multihash
114+
return version == 0 && super.equals(o);
94115
}
95116

96117
@Override
97118
public int hashCode() {
98-
int result = (int) (version ^ (version >>> 32));
119+
int result = super.hashCode();
120+
if (version == 0)
121+
return result;
122+
result = 31 * result + (int) (version ^ (version >>> 32));
99123
result = 31 * result + (codec != null ? codec.hashCode() : 0);
100-
result = 31 * result + (hash != null ? hash.hashCode() : 0);
101124
return result;
102125
}
103126

104-
public static Cid buildCidV0(Multihash hash) {
105-
return new Cid(0, Codec.DagProtobuf, hash);
127+
public static Cid buildCidV0(Multihash h) {
128+
return new Cid(h);
106129
}
107130

108-
public static Cid buildCidV1(Codec c, Multihash hash) {
109-
return new Cid(1, c, hash);
131+
public static Cid buildCidV1(Codec c, Multihash.Type type, byte[] hash) {
132+
return new Cid(1, c, type, hash);
110133
}
111134

112-
public static Cid decode(String v) throws IOException {
135+
public static Cid decode(String v) {
113136
if (v.length() < 2)
114137
throw new IllegalStateException("Cid too short!");
115138

@@ -121,22 +144,26 @@ public static Cid decode(String v) throws IOException {
121144
return cast(data);
122145
}
123146

124-
public static Cid cast(byte[] data) throws IOException {
147+
public static Cid cast(byte[] data) {
125148
if (data.length == 34 && data[0] == 0x18 && data[1] == 32)
126149
return buildCidV0(new Multihash(data));
127150

128151
InputStream in = new ByteArrayInputStream(data);
129-
long version = readVarint(in);
130-
if (version != 0 && version != 1)
131-
throw new IllegalStateException("Invalid Cif version number: " + version);
152+
try {
153+
long version = readVarint(in);
154+
if (version != 0 && version != 1)
155+
throw new CidEncodingException("Invalid Cif version number: " + version);
132156

133-
long codec = readVarint(in);
134-
if (version != 0 && version != 1)
135-
throw new IllegalStateException("Invalid Cif version number: " + version);
157+
long codec = readVarint(in);
158+
if (version != 0 && version != 1)
159+
throw new CidEncodingException("Invalid Cif version number: " + version);
136160

137-
Multihash hash = Multihash.deserialize(new DataInputStream(in));
161+
Multihash hash = Multihash.deserialize(new DataInputStream(in));
138162

139-
return new Cid(version, Codec.lookup(codec), hash);
163+
return new Cid(version, Codec.lookup(codec), hash);
164+
} catch (IOException e) {
165+
throw new CidEncodingException("Invalid cid bytes: " + Stream.of(data).map(b -> String.format("%02x", b)).reduce("", (a, b) -> a + b));
166+
}
140167
}
141168

142169
private static long readVarint(InputStream in) throws IOException {

src/test/java/io/ipfs/cid/CidTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ public void version0Handling() throws Exception {
5656

5757
Assert.assertTrue("version 0", cid.version == 0);
5858

59-
Assert.assertTrue("Correct hash", cid.hash.toString().equals(hashString));
60-
61-
Assert.assertTrue("Correct toString", cid.toString().equals(hashString));
59+
Assert.assertTrue("Correct hash", cid.toString().equals(hashString));
6260
}
6361

6462
@Test

0 commit comments

Comments
 (0)