-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99a4774
commit 5c99291
Showing
11 changed files
with
652 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
*.class | ||
|
||
build/** | ||
dist/** | ||
.idea/** | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2015 Ian Preston | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
# java-multiaddr | ||
Java implementation of multiaddr | ||
|
||
## Usage | ||
MultiAddress m = new MultiAddress("/ip4/127.0.0.1/tcp/1234"); | ||
|
||
or | ||
|
||
MultiAddress m = new MultiAddress("/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC"); | ||
|
||
## Compilation | ||
To compile just run ant. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<project name="java-multiaddr" default="dist" basedir="."> | ||
<description> | ||
Java Multiaddr | ||
</description> | ||
|
||
<property name="src" location="src"/> | ||
<property name="build" location="build"/> | ||
<property name="dist" location="dist"/> | ||
|
||
<path id="dep.runtime"> | ||
<fileset dir="./lib"> | ||
<include name="**/*.jar" /> | ||
</fileset> | ||
</path> | ||
|
||
<target name="init"> | ||
<mkdir dir="${build}"/> | ||
</target> | ||
|
||
<target name="compile" depends="init" description="compile the source"> | ||
<javac includeantruntime="false" srcdir="${src}" destdir="${build}" debug="true" debuglevel="lines,vars,source"> | ||
<classpath> | ||
<fileset dir="lib"> | ||
<include name="**/*.jar" /> | ||
</fileset> | ||
</classpath> | ||
</javac> | ||
</target> | ||
|
||
<target name="dist" depends="compile" description="generate the distribution"> | ||
<mkdir dir="${dist}/lib"/> | ||
<copy todir="${dist}/lib"> | ||
<fileset dir="lib"/> | ||
</copy> | ||
<manifestclasspath property="manifest_cp" jarfile="myjar.jar"> | ||
<classpath refid="dep.runtime" /> | ||
</manifestclasspath> | ||
<jar jarfile="${dist}/Multiaddr.jar" basedir="${build}"> | ||
<manifest> | ||
<attribute name="Class-Path" value="${manifest_cp}"/> | ||
</manifest> | ||
</jar> | ||
<copy todir="."> | ||
<fileset file="${dist}/Multiaddr.jar"/> | ||
</copy> | ||
</target> | ||
|
||
|
||
<target name="test" depends="compile,dist"> | ||
<junit printsummary="yes" fork="true" haltonfailure="yes"> | ||
<jvmarg value="-Xmx1g"/> | ||
<classpath> | ||
<pathelement location="lib/junit-4.11.jar" /> | ||
<pathelement location="lib/hamcrest-core-1.3.jar" /> | ||
<pathelement location="Multiaddr.jar" /> | ||
</classpath> | ||
<test name="org.ipfs.api.MultiaddrTests" haltonfailure="yes"> | ||
<formatter type="plain"/> | ||
<formatter type="xml"/> | ||
</test> | ||
</junit> | ||
</target> | ||
|
||
<target name="clean" description="clean up"> | ||
<delete dir="${build}"/> | ||
<delete dir="${dist}"/> | ||
</target> | ||
</project> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.ipfs.api; | ||
|
||
import java.math.*; | ||
|
||
/** | ||
* Based on RFC 4648 | ||
*/ | ||
public class Base32 { | ||
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; | ||
private static final BigInteger BASE = BigInteger.valueOf(32); | ||
|
||
public static String encode(byte[] input) { | ||
// TODO: This could be a lot more efficient. | ||
BigInteger bi = new BigInteger(1, input); | ||
StringBuffer s = new StringBuffer(); | ||
while (bi.compareTo(BASE) >= 0) { | ||
BigInteger mod = bi.mod(BASE); | ||
s.insert(0, ALPHABET.charAt(mod.intValue())); | ||
bi = bi.subtract(mod).divide(BASE); | ||
} | ||
s.insert(0, ALPHABET.charAt(bi.intValue())); | ||
// Convert leading zeros too. | ||
for (byte anInput : input) { | ||
if (anInput == 0) | ||
s.insert(0, ALPHABET.charAt(0)); | ||
else | ||
break; | ||
} | ||
return s.toString(); | ||
} | ||
|
||
public static byte[] decode(String input) { | ||
byte[] bytes = decodeToBigInteger(input).toByteArray(); | ||
// We may have got one more byte than we wanted, if the high bit of the next-to-last byte was not zero. This | ||
// is because BigIntegers are represented with twos-compliment notation, thus if the high bit of the last | ||
// byte happens to be 1 another 8 zero bits will be added to ensure the number parses as positive. Detect | ||
// that case here and chop it off. | ||
boolean stripSignByte = bytes.length > 1 && bytes[0] == 0 && bytes[1] < 0; | ||
// Count the leading zeros, if any. | ||
int leadingZeros = 0; | ||
for (int i = 0; input.charAt(i) == ALPHABET.charAt(0); i++) { | ||
leadingZeros++; | ||
} | ||
// Now cut/pad correctly. Java 6 has a convenience for this, but Android can't use it. | ||
byte[] tmp = new byte[bytes.length - (stripSignByte ? 1 : 0) + leadingZeros]; | ||
System.arraycopy(bytes, stripSignByte ? 1 : 0, tmp, leadingZeros, tmp.length - leadingZeros); | ||
return tmp; | ||
} | ||
|
||
public static BigInteger decodeToBigInteger(String input) { | ||
BigInteger bi = BigInteger.valueOf(0); | ||
// Work backwards through the string. | ||
for (int i = input.length() - 1; i >= 0; i--) { | ||
int alphaIndex = ALPHABET.indexOf(input.charAt(i)); | ||
if (alphaIndex == -1) { | ||
throw new IllegalStateException("Illegal character " + input.charAt(i) + " at " + i); | ||
} | ||
bi = bi.add(BigInteger.valueOf(alphaIndex).multiply(BASE.pow(input.length() - 1 - i))); | ||
} | ||
return bi; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package org.ipfs.api; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class MultiAddress | ||
{ | ||
private final byte[] raw; | ||
|
||
public MultiAddress(String address) { | ||
this(decodeFromString(address)); | ||
} | ||
|
||
public MultiAddress(byte[] raw) { | ||
encodeToString(raw); // check validity | ||
this.raw = raw; | ||
} | ||
|
||
public byte[] getBytes() { | ||
return Arrays.copyOfRange(raw, 0, raw.length); | ||
} | ||
|
||
public boolean isTCPIP() { | ||
String[] parts = toString().substring(1).split("/"); | ||
if (parts.length != 4) | ||
return false; | ||
if (!parts[0].startsWith("ip")) | ||
return false; | ||
if (!parts[2].equals("tcp")) | ||
return false; | ||
return true; | ||
} | ||
|
||
public String getHost() { | ||
String[] parts = toString().substring(1).split("/"); | ||
if (parts[0].startsWith("ip")) | ||
return parts[1]; | ||
throw new IllegalStateException("This multiaddress doesn't have a host: "+toString()); | ||
} | ||
|
||
public int getTCPPort() { | ||
String[] parts = toString().substring(1).split("/"); | ||
if (parts[2].startsWith("tcp")) | ||
return Integer.parseInt(parts[3]); | ||
throw new IllegalStateException("This multiaddress doesn't have a tcp port: "+toString()); | ||
} | ||
|
||
private static byte[] decodeFromString(String addr) { | ||
while (addr.endsWith("/")) | ||
addr = addr.substring(0, addr.length()-1); | ||
String[] parts = addr.split("/"); | ||
if (parts[0].length() != 0) | ||
throw new IllegalStateException("MultiAddress must start with a /"); | ||
|
||
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | ||
try { | ||
for (int i = 1; i < parts.length;) { | ||
String part = parts[i++]; | ||
Protocol p = Protocol.get(part); | ||
p.appendCode(bout); | ||
if (p.size() == 0) | ||
continue; | ||
|
||
String component = parts[i++]; | ||
if (component.length() == 0) | ||
throw new IllegalStateException("Protocol requires address, but non provided!"); | ||
|
||
bout.write(p.addressToBytes(component)); | ||
} | ||
return bout.toByteArray(); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Error decoding multiaddress: "+addr); | ||
} | ||
} | ||
|
||
private static String encodeToString(byte[] raw) { | ||
StringBuilder b = new StringBuilder(); | ||
InputStream in = new ByteArrayInputStream(raw); | ||
try { | ||
while (true) { | ||
int code = (int)Protocol.readVarint(in); | ||
Protocol p = Protocol.get(code); | ||
b.append("/" + p.name()); | ||
if (p.size() == 0) | ||
continue; | ||
|
||
String addr = p.readAddress(in); | ||
if (addr.length() > 0) | ||
b.append("/" +addr); | ||
} | ||
} | ||
catch (EOFException eof) {} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return b.toString(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return encodeToString(raw); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (!(other instanceof MultiAddress)) | ||
return false; | ||
return Arrays.equals(raw, ((MultiAddress) other).raw); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(raw); | ||
} | ||
} |
Oops, something went wrong.