Skip to content

Commit f967fd1

Browse files
committed
fix toHex()
1 parent c3f2179 commit f967fd1

3 files changed

Lines changed: 62 additions & 5 deletions

File tree

pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@
7878
<groupId>org.apache.maven.plugins</groupId>
7979
<artifactId>maven-surefire-plugin</artifactId>
8080
<version>2.19.1</version>
81+
<configuration>
82+
<!-- Sets the VM argument line used when unit tests are run. -->
83+
<argLine>${surefireArgLine}</argLine>
84+
</configuration>
8185
</plugin>
8286
<plugin>
8387
<groupId>org.apache.maven.plugins</groupId>
@@ -91,6 +95,49 @@
9195
</archive>
9296
</configuration>
9397
</plugin>
98+
<plugin>
99+
<groupId>org.jacoco</groupId>
100+
<artifactId>jacoco-maven-plugin</artifactId>
101+
<version>0.7.8</version>
102+
<executions>
103+
<!--
104+
Prepares the property pointing to the JaCoCo runtime agent which
105+
is passed as VM argument when Maven the Surefire plugin is executed.
106+
-->
107+
<execution>
108+
<id>pre-unit-test</id>
109+
<goals>
110+
<goal>prepare-agent</goal>
111+
</goals>
112+
<configuration>
113+
<!-- Sets the path to the file which contains the execution data. -->
114+
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
115+
<!--
116+
Sets the name of the property containing the settings
117+
for JaCoCo runtime agent.
118+
-->
119+
<propertyName>surefireArgLine</propertyName>
120+
</configuration>
121+
</execution>
122+
<!--
123+
Ensures that the code coverage report for unit tests is created after
124+
unit tests have been run.
125+
-->
126+
<execution>
127+
<id>post-unit-test</id>
128+
<phase>test</phase>
129+
<goals>
130+
<goal>report</goal>
131+
</goals>
132+
<configuration>
133+
<!-- Sets the path to the file which contains the execution data. -->
134+
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
135+
<!-- Sets the output directory for the code coverage report. -->
136+
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
137+
</configuration>
138+
</execution>
139+
</executions>
140+
</plugin>
94141
</plugins>
95142
</build>
96143
</project>

src/main/java/io/ipfs/multihash/Multihash.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,17 @@ public int hashCode() {
9292
return Arrays.hashCode(hash) ^ type.hashCode();
9393
}
9494

95+
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
96+
9597
public String toHex() {
96-
StringBuilder res = new StringBuilder();
97-
for (byte b: toBytes())
98-
res.append(String.format("%x", b&0xff));
99-
return res.toString();
98+
byte[] bytes = toBytes();
99+
char[] hexChars = new char[bytes.length * 2];
100+
for ( int j = 0; j < bytes.length; j++ ) {
101+
int v = bytes[j] & 0xFF;
102+
hexChars[j * 2] = hexArray[v >>> 4];
103+
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
104+
}
105+
return new String(hexChars);
100106
}
101107

102108
public String toBase58() {

src/test/java/io/ipfs/multihash/MultihashTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import io.ipfs.multibase.*;
55
import java.util.*;
66
import java.security.MessageDigest;
7+
import java.io.PipedInputStream;
8+
import java.io.PipedOutputStream;
79

810
public class MultihashTest {
911

@@ -43,9 +45,11 @@ public void multihashTest() {
4345
// Test conversions
4446
assert(m.toBase58().equals(m2.toBase58()));
4547
assert(m.toBase58().equals((String)ex[2]));
48+
// Test fromHex and toHex
49+
Multihash m3 = Multihash.fromHex(m.toHex());
50+
assert(m3.equals(m));
4651
}
4752
catch (Exception e){
48-
// Usually because a hash function not supported
4953
System.out.println(e.getMessage());
5054
assert(false);
5155
}

0 commit comments

Comments
 (0)