11package io .ipfs .multihash ;
22
3- import io .ipfs .multibase .*;
3+ import io .ipfs .multibase .Base16 ;
4+ import io .ipfs .multibase .Base58 ;
45
5- import java .io .*;
6- import java .util .*;
6+ import java .io .ByteArrayOutputStream ;
7+ import java .io .DataInput ;
8+ import java .io .DataOutput ;
9+ import java .io .IOException ;
10+ import java .util .Arrays ;
11+ import java .util .Map ;
12+ import java .util .TreeMap ;
713
814public class Multihash {
915 public enum Type {
@@ -21,9 +27,9 @@ public enum Type {
2127 blake2b (0x40 , 64 ),
2228 blake2s (0x41 , 32 );
2329
24- public int index , length ;
30+ public final int index , length ;
2531
26- Type (int index , int length ) {
32+ Type (final int index , final int length ) {
2733 this .index = index ;
2834 this .length = length ;
2935 }
@@ -39,12 +45,13 @@ public static Type lookup(int t) {
3945 throw new IllegalStateException ("Unknown Multihash type: " +t );
4046 return lookup .get (t );
4147 }
48+
4249 }
4350
44- public final Type type ;
51+ private final Type type ;
4552 private final byte [] hash ;
4653
47- public Multihash (Type type , byte [] hash ) {
54+ public Multihash (final Type type , final byte [] hash ) {
4855 if (hash .length > 127 )
4956 throw new IllegalStateException ("Unsupported hash size: " +hash .length );
5057 if (hash .length != type .length )
@@ -57,7 +64,7 @@ public Multihash(Multihash toClone) {
5764 this (toClone .type , toClone .hash ); // N.B. despite being a byte[], hash is immutable
5865 }
5966
60- public Multihash (byte [] multihash ) {
67+ public Multihash (final byte [] multihash ) {
6168 this (Type .lookup (multihash [0 ] & 0xff ), Arrays .copyOfRange (multihash , 2 , multihash .length ));
6269 }
6370
@@ -68,10 +75,13 @@ public byte[] toBytes() {
6875 System .arraycopy (hash , 0 , res , 2 , hash .length );
6976 return res ;
7077 }
78+
79+ public Type getType () {
80+ return type ;
81+ }
7182
7283 public byte [] getHash () {
73- byte [] res = Arrays .copyOf (hash )
74- return res ;
84+ return Arrays .copyOf (hash , hash .length );
7585 }
7686
7787 public void serialize (DataOutput dout ) throws IOException {
@@ -104,17 +114,8 @@ public int hashCode() {
104114 return Arrays .hashCode (hash ) ^ type .hashCode ();
105115 }
106116
107- final protected static char [] hexArray = "0123456789ABCDEF" .toCharArray ();
108-
109117 public String toHex () {
110- byte [] bytes = toBytes ();
111- char [] hexChars = new char [bytes .length * 2 ];
112- for ( int j = 0 ; j < bytes .length ; j ++ ) {
113- int v = bytes [j ] & 0xFF ;
114- hexChars [j * 2 ] = hexArray [v >>> 4 ];
115- hexChars [j * 2 + 1 ] = hexArray [v & 0x0F ];
116- }
117- return new String (hexChars );
118+ return Base16 .encode (toBytes ());
118119 }
119120
120121 public String toBase58 () {
@@ -123,11 +124,15 @@ public String toBase58() {
123124
124125 public static Multihash fromHex (String hex ) {
125126 if (hex .length () % 2 != 0 )
126- throw new IllegalStateException ("Uneven number of hex digits!" );
127- ByteArrayOutputStream bout = new ByteArrayOutputStream ();
128- for (int i =0 ; i < hex .length ()-1 ; i += 2 )
129- bout .write (Integer .valueOf (hex .substring (i , i +2 ), 16 ));
130- return new Multihash (bout .toByteArray ());
127+ throw new IllegalStateException ("Odd number of hex digits!" );
128+
129+ try (ByteArrayOutputStream bout = new ByteArrayOutputStream ()) {
130+ for (int i = 0 ; i < hex .length () - 1 ; i += 2 )
131+ bout .write (Integer .valueOf (hex .substring (i , i + 2 ), 16 ));
132+ return new Multihash (bout .toByteArray ());
133+ } catch (IOException e ) {
134+ throw new IllegalStateException ("Unable to handle Multihash conversion to Hex properly" );
135+ }
131136 }
132137
133138 public static Multihash fromBase58 (String base58 ) {
0 commit comments