- *
- * Licensed under Apache License v2.0.
- */
-
-import java.io.*;
-
-import static io.ipfs.api.cbor.CborConstants.*;
-
-/**
- * Provides an encoder capable of encoding data into CBOR format to a given {@link OutputStream}.
- */
-public class CborEncoder {
- private static final int NEG_INT_MASK = TYPE_NEGATIVE_INTEGER << 5;
-
- private final OutputStream m_os;
-
- /**
- * Creates a new {@link CborEncoder} instance.
- *
- * @param os the actual output stream to write the CBOR-encoded data to, cannot be null
.
- */
- public CborEncoder(OutputStream os) {
- if (os == null) {
- throw new IllegalArgumentException("OutputStream cannot be null!");
- }
- m_os = os;
- }
-
- /**
- * Interprets a given float-value as a half-precision float value and
- * converts it to its raw integer form, as defined in IEEE 754.
- *
- * Taken from: this Stack Overflow answer.
- *
- *
- * @param fval the value to convert.
- * @return the raw integer representation of the given float value.
- */
- static int halfPrecisionToRawIntBits(float fval) {
- int fbits = Float.floatToIntBits(fval);
- int sign = (fbits >>> 16) & 0x8000;
- int val = (fbits & 0x7fffffff) + 0x1000;
-
- // might be or become NaN/Inf
- if (val >= 0x47800000) {
- if ((fbits & 0x7fffffff) >= 0x47800000) { // is or must become NaN/Inf
- if (val < 0x7f800000) {
- // was value but too large, make it +/-Inf
- return sign | 0x7c00;
- }
- return sign | 0x7c00 | (fbits & 0x007fffff) >>> 13; // keep NaN (and Inf) bits
- }
- return sign | 0x7bff; // unrounded not quite Inf
- }
- if (val >= 0x38800000) {
- // remains normalized value
- return sign | val - 0x38000000 >>> 13; // exp - 127 + 15
- }
- if (val < 0x33000000) {
- // too small for subnormal
- return sign; // becomes +/-0
- }
-
- val = (fbits & 0x7fffffff) >>> 23;
- // add subnormal bit, round depending on cut off and div by 2^(1-(exp-127+15)) and >> 13 | exp=0
- return sign | ((fbits & 0x7fffff | 0x800000) + (0x800000 >>> val - 102) >>> 126 - val);
- }
-
- /**
- * Writes the start of an indefinite-length array.
- *
- * After calling this method, one is expected to write the given number of array elements, which can be of any type. No length checks are performed.
- * After all array elements are written, one should write a single break value to end the array, see {@link #writeBreak()}.
- *
- *
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeArrayStart() throws IOException {
- writeSimpleType(TYPE_ARRAY, BREAK);
- }
-
- /**
- * Writes the start of a definite-length array.
- *
- * After calling this method, one is expected to write the given number of array elements, which can be of any type. No length checks are performed.
- *
- *
- * @param length the number of array elements to write, should >= 0.
- * @throws IllegalArgumentException in case the given length was negative;
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeArrayStart(int length) throws IOException {
- if (length < 0) {
- throw new IllegalArgumentException("Invalid array-length!");
- }
- writeType(TYPE_ARRAY, length);
- }
-
- /**
- * Writes a boolean value in canonical CBOR format.
- *
- * @param value the boolean to write.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeBoolean(boolean value) throws IOException {
- writeSimpleType(TYPE_FLOAT_SIMPLE, value ? TRUE : FALSE);
- }
-
- /**
- * Writes a "break" stop-value in canonical CBOR format.
- *
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeBreak() throws IOException {
- writeSimpleType(TYPE_FLOAT_SIMPLE, BREAK);
- }
-
- /**
- * Writes a byte string in canonical CBOR-format.
- *
- * @param bytes the byte string to write, can be null
in which case a byte-string of length 0 is written.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeByteString(byte[] bytes) throws IOException {
- writeString(TYPE_BYTE_STRING, bytes);
- }
-
- /**
- * Writes the start of an indefinite-length byte string.
- *
- * After calling this method, one is expected to write the given number of string parts. No length checks are performed.
- * After all string parts are written, one should write a single break value to end the string, see {@link #writeBreak()}.
- *
- *
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeByteStringStart() throws IOException {
- writeSimpleType(TYPE_BYTE_STRING, BREAK);
- }
-
- /**
- * Writes a double-precision float value in canonical CBOR format.
- *
- * @param value the value to write, values from {@link Double#MIN_VALUE} to {@link Double#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeDouble(double value) throws IOException {
- writeUInt64(TYPE_FLOAT_SIMPLE << 5, Double.doubleToRawLongBits(value));
- }
-
- /**
- * Writes a single-precision float value in canonical CBOR format.
- *
- * @param value the value to write, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeFloat(float value) throws IOException {
- writeUInt32(TYPE_FLOAT_SIMPLE << 5, Float.floatToRawIntBits(value));
- }
-
- /**
- * Writes a half-precision float value in canonical CBOR format.
- *
- * @param value the value to write, values from {@link Float#MIN_VALUE} to {@link Float#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeHalfPrecisionFloat(float value) throws IOException {
- writeUInt16(TYPE_FLOAT_SIMPLE << 5, halfPrecisionToRawIntBits(value));
- }
-
- /**
- * Writes a signed or unsigned integer value in canonical CBOR format, that is, tries to encode it in a little bytes as possible..
- *
- * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeInt(long value) throws IOException {
- // extends the sign over all bits...
- long sign = value >> 63;
- // in case value is negative, this bit should be set...
- int mt = (int) (sign & NEG_INT_MASK);
- // complement negative value...
- value = (sign ^ value);
-
- writeUInt(mt, value);
- }
-
- /**
- * Writes a signed or unsigned 16-bit integer value in CBOR format.
- *
- * @param value the value to write, values from [-65536..65535] are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeInt16(int value) throws IOException {
- // extends the sign over all bits...
- int sign = value >> 31;
- // in case value is negative, this bit should be set...
- int mt = (int) (sign & NEG_INT_MASK);
- // complement negative value...
- writeUInt16(mt, (sign ^ value) & 0xffff);
- }
-
- /**
- * Writes a signed or unsigned 32-bit integer value in CBOR format.
- *
- * @param value the value to write, values in the range [-4294967296..4294967295] are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeInt32(long value) throws IOException {
- // extends the sign over all bits...
- long sign = value >> 63;
- // in case value is negative, this bit should be set...
- int mt = (int) (sign & NEG_INT_MASK);
- // complement negative value...
- writeUInt32(mt, (int) ((sign ^ value) & 0xffffffffL));
- }
-
- /**
- * Writes a signed or unsigned 64-bit integer value in CBOR format.
- *
- * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeInt64(long value) throws IOException {
- // extends the sign over all bits...
- long sign = value >> 63;
- // in case value is negative, this bit should be set...
- int mt = (int) (sign & NEG_INT_MASK);
- // complement negative value...
- writeUInt64(mt, sign ^ value);
- }
-
- /**
- * Writes a signed or unsigned 8-bit integer value in CBOR format.
- *
- * @param value the value to write, values in the range [-256..255] are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeInt8(int value) throws IOException {
- // extends the sign over all bits...
- int sign = value >> 31;
- // in case value is negative, this bit should be set...
- int mt = (int) (sign & NEG_INT_MASK);
- // complement negative value...
- writeUInt8(mt, (sign ^ value) & 0xff);
- }
-
- /**
- * Writes the start of an indefinite-length map.
- *
- * After calling this method, one is expected to write any number of map entries, as separate key and value. Keys and values can both be of any type. No length checks are performed.
- * After all map entries are written, one should write a single break value to end the map, see {@link #writeBreak()}.
- *
- *
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeMapStart() throws IOException {
- writeSimpleType(TYPE_MAP, BREAK);
- }
-
- /**
- * Writes the start of a finite-length map.
- *
- * After calling this method, one is expected to write any number of map entries, as separate key and value. Keys and values can both be of any type. No length checks are performed.
- *
- *
- * @param length the number of map entries to write, should >= 0.
- * @throws IllegalArgumentException in case the given length was negative;
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeMapStart(int length) throws IOException {
- if (length < 0) {
- throw new IllegalArgumentException("Invalid length of map!");
- }
- writeType(TYPE_MAP, length);
- }
-
- /**
- * Writes a null
value in canonical CBOR format.
- *
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeNull() throws IOException {
- writeSimpleType(TYPE_FLOAT_SIMPLE, NULL);
- }
-
- /**
- * Writes a simple value, i.e., an "atom" or "constant" value in canonical CBOR format.
- *
- * @param simpleValue the (unsigned byte) value to write, values from 32 to 255 are supported (though not enforced).
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeSimpleValue(byte simpleValue) throws IOException {
- // convert to unsigned value...
- int value = (simpleValue & 0xff);
- writeType(TYPE_FLOAT_SIMPLE, value);
- }
-
- /**
- * Writes a signed or unsigned small (<= 23) integer value in CBOR format.
- *
- * @param value the value to write, values in the range [-24..23] are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeSmallInt(int value) throws IOException {
- // extends the sign over all bits...
- int sign = value >> 31;
- // in case value is negative, this bit should be set...
- int mt = (int) (sign & NEG_INT_MASK);
- // complement negative value...
- value = Math.min(0x17, (sign ^ value));
-
- m_os.write((int) (mt | value));
- }
-
- /**
- * Writes a semantic tag in canonical CBOR format.
- *
- * @param tag the tag to write, should >= 0.
- * @throws IllegalArgumentException in case the given tag was negative;
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeTag(long tag) throws IOException {
- if (tag < 0) {
- throw new IllegalArgumentException("Invalid tag specification, cannot be negative!");
- }
- writeType(TYPE_TAG, tag);
- }
-
- /**
- * Writes an UTF-8 string in canonical CBOR-format.
- *
- * Note that this method is platform specific, as the given string value will be encoded in a byte array
- * using the platform encoding! This means that the encoding must be standardized and known.
- *
- *
- * @param value the UTF-8 string to write, can be null
in which case an UTF-8 string of length 0 is written.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeTextString(String value) throws IOException {
- writeString(TYPE_TEXT_STRING, value == null ? null : value.getBytes("UTF-8"));
- }
-
- /**
- * Writes the start of an indefinite-length UTF-8 string.
- *
- * After calling this method, one is expected to write the given number of string parts. No length checks are performed.
- * After all string parts are written, one should write a single break value to end the string, see {@link #writeBreak()}.
- *
- *
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeTextStringStart() throws IOException {
- writeSimpleType(TYPE_TEXT_STRING, BREAK);
- }
-
- /**
- * Writes an "undefined" value in canonical CBOR format.
- *
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- public void writeUndefined() throws IOException {
- writeSimpleType(TYPE_FLOAT_SIMPLE, UNDEFINED);
- }
-
- /**
- * Encodes and writes the major type and value as a simple type.
- *
- * @param majorType the major type of the value to write, denotes what semantics the written value has;
- * @param value the value to write, values from [0..31] are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- protected void writeSimpleType(int majorType, int value) throws IOException {
- m_os.write((majorType << 5) | (value & 0x1f));
- }
-
- /**
- * Writes a byte string in canonical CBOR-format.
- *
- * @param majorType the major type of the string, should be either 0x40 or 0x60;
- * @param bytes the byte string to write, can be null
in which case a byte-string of length 0 is written.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- protected void writeString(int majorType, byte[] bytes) throws IOException {
- int len = (bytes == null) ? 0 : bytes.length;
- writeType(majorType, len);
- for (int i = 0; i < len; i++) {
- m_os.write(bytes[i]);
- }
- }
-
- /**
- * Encodes and writes the major type indicator with a given payload (length).
- *
- * @param majorType the major type of the value to write, denotes what semantics the written value has;
- * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- protected void writeType(int majorType, long value) throws IOException {
- writeUInt((majorType << 5), value);
- }
-
- /**
- * Encodes and writes an unsigned integer value, that is, tries to encode it in a little bytes as possible.
- *
- * @param mt the major type of the value to write, denotes what semantics the written value has;
- * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- protected void writeUInt(int mt, long value) throws IOException {
- if (value < 0x18L) {
- m_os.write((int) (mt | value));
- } else if (value < 0x100L) {
- writeUInt8(mt, (int) value);
- } else if (value < 0x10000L) {
- writeUInt16(mt, (int) value);
- } else if (value < 0x100000000L) {
- writeUInt32(mt, (int) value);
- } else {
- writeUInt64(mt, value);
- }
- }
-
- /**
- * Encodes and writes an unsigned 16-bit integer value
- *
- * @param mt the major type of the value to write, denotes what semantics the written value has;
- * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- protected void writeUInt16(int mt, int value) throws IOException {
- m_os.write(mt | TWO_BYTES);
- m_os.write(value >> 8);
- m_os.write(value & 0xFF);
- }
-
- /**
- * Encodes and writes an unsigned 32-bit integer value
- *
- * @param mt the major type of the value to write, denotes what semantics the written value has;
- * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- protected void writeUInt32(int mt, int value) throws IOException {
- m_os.write(mt | FOUR_BYTES);
- m_os.write(value >> 24);
- m_os.write(value >> 16);
- m_os.write(value >> 8);
- m_os.write(value & 0xFF);
- }
-
- /**
- * Encodes and writes an unsigned 64-bit integer value
- *
- * @param mt the major type of the value to write, denotes what semantics the written value has;
- * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- protected void writeUInt64(int mt, long value) throws IOException {
- m_os.write(mt | EIGHT_BYTES);
- m_os.write((int) (value >> 56));
- m_os.write((int) (value >> 48));
- m_os.write((int) (value >> 40));
- m_os.write((int) (value >> 32));
- m_os.write((int) (value >> 24));
- m_os.write((int) (value >> 16));
- m_os.write((int) (value >> 8));
- m_os.write((int) (value & 0xFF));
- }
-
- /**
- * Encodes and writes an unsigned 8-bit integer value
- *
- * @param mt the major type of the value to write, denotes what semantics the written value has;
- * @param value the value to write, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
- * @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
- */
- protected void writeUInt8(int mt, int value) throws IOException {
- m_os.write(mt | ONE_BYTE);
- m_os.write(value & 0xFF);
- }
-}
\ No newline at end of file
diff --git a/src/main/java/io/ipfs/api/cbor/CborObject.java b/src/main/java/io/ipfs/api/cbor/CborObject.java
deleted file mode 100644
index e2a34c9..0000000
--- a/src/main/java/io/ipfs/api/cbor/CborObject.java
+++ /dev/null
@@ -1,427 +0,0 @@
-package io.ipfs.api.cbor;
-
-import io.ipfs.cid.*;
-import io.ipfs.multiaddr.*;
-import io.ipfs.multihash.*;
-
-import java.io.*;
-import java.util.*;
-import java.util.stream.*;
-
-public interface CborObject {
-
- void serialize(CborEncoder encoder);
-
- default byte[] toByteArray() {
- ByteArrayOutputStream bout = new ByteArrayOutputStream();
- CborEncoder encoder = new CborEncoder(bout);
- serialize(encoder);
- return bout.toByteArray();
- }
-
- int LINK_TAG = 42;
-
- static CborObject fromByteArray(byte[] cbor) {
- return deserialize(new CborDecoder(new ByteArrayInputStream(cbor)));
- }
-
- static CborObject deserialize(CborDecoder decoder) {
- try {
- CborType type = decoder.peekType();
- switch (type.getMajorType()) {
- case CborConstants.TYPE_TEXT_STRING:
- return new CborString(decoder.readTextString());
- case CborConstants.TYPE_BYTE_STRING:
- return new CborByteArray(decoder.readByteString());
- case CborConstants.TYPE_UNSIGNED_INTEGER:
- return new CborLong(decoder.readInt());
- case CborConstants.TYPE_NEGATIVE_INTEGER:
- return new CborLong(decoder.readInt());
- case CborConstants.TYPE_FLOAT_SIMPLE:
- if (type.getAdditionalInfo() == CborConstants.NULL) {
- decoder.readNull();
- return new CborNull();
- }
- if (type.getAdditionalInfo() == CborConstants.TRUE) {
- decoder.readBoolean();
- return new CborBoolean(true);
- }
- if (type.getAdditionalInfo() == CborConstants.FALSE) {
- decoder.readBoolean();
- return new CborBoolean(false);
- }
- throw new IllegalStateException("Unimplemented simple type! " + type.getAdditionalInfo());
- case CborConstants.TYPE_MAP: {
- long nValues = decoder.readMapLength();
- SortedMap result = new TreeMap<>();
- for (long i=0; i < nValues; i++) {
- CborObject key = deserialize(decoder);
- CborObject value = deserialize(decoder);
- result.put(key, value);
- }
- return new CborMap(result);
- }
- case CborConstants.TYPE_ARRAY:
- long nItems = decoder.readArrayLength();
- List res = new ArrayList<>((int) nItems);
- for (long i=0; i < nItems; i++)
- res.add(deserialize(decoder));
- return new CborList(res);
- case CborConstants.TYPE_TAG:
- long tag = decoder.readTag();
- if (tag == LINK_TAG) {
- CborObject value = deserialize(decoder);
- if (value instanceof CborString)
- return new CborMerkleLink(Cid.decode(((CborString) value).value));
- if (value instanceof CborByteArray) {
- byte[] bytes = ((CborByteArray) value).value;
- if (bytes[0] == 0) // multibase for binary
- return new CborMerkleLink(Cid.cast(Arrays.copyOfRange(bytes, 1, bytes.length)));
- throw new IllegalStateException("Unknown Multibase decoding Merkle link: " + bytes[0]);
- }
- throw new IllegalStateException("Invalid type for merkle link: " + value);
- }
- throw new IllegalStateException("Unknown TAG in CBOR: " + type.getAdditionalInfo());
- default:
- throw new IllegalStateException("Unimplemented cbor type: " + type);
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- final class CborMap implements CborObject {
- public final SortedMap values;
-
- public CborMap(SortedMap values) {
- this.values = values;
- }
-
- public static CborMap build(Map values) {
- SortedMap transformed = values.entrySet()
- .stream()
- .collect(Collectors.toMap(
- e -> new CborString(e.getKey()),
- e -> e.getValue(),
- (a, b) -> a, TreeMap::new));
- return new CborMap(transformed);
- }
-
- @Override
- public void serialize(CborEncoder encoder) {
- try {
- encoder.writeMapStart(values.size());
- for (Map.Entry entry : values.entrySet()) {
- entry.getKey().serialize(encoder);
- entry.getValue().serialize(encoder);
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- CborMap cborMap = (CborMap) o;
-
- return values != null ? values.equals(cborMap.values) : cborMap.values == null;
-
- }
-
- @Override
- public int hashCode() {
- return values != null ? values.hashCode() : 0;
- }
- }
-
- final class CborMerkleLink implements CborObject {
- public final Multihash target;
-
- public CborMerkleLink(Multihash target) {
- this.target = target;
- }
-
- @Override
- public void serialize(CborEncoder encoder) {
- try {
- encoder.writeTag(LINK_TAG);
- byte[] cid = target.toBytes();
- byte[] withMultibaseHeader = new byte[cid.length + 1];
- System.arraycopy(cid, 0, withMultibaseHeader, 1, cid.length);
- encoder.writeByteString(withMultibaseHeader);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- CborMerkleLink that = (CborMerkleLink) o;
-
- return target != null ? target.equals(that.target) : that.target == null;
-
- }
-
- @Override
- public int hashCode() {
- return target != null ? target.hashCode() : 0;
- }
- }
-
- final class CborList implements CborObject {
- public final List value;
-
- public CborList(List value) {
- this.value = value;
- }
-
- @Override
- public void serialize(CborEncoder encoder) {
- try {
- encoder.writeArrayStart(value.size());
- for (CborObject object : value) {
- object.serialize(encoder);
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- CborList cborList = (CborList) o;
-
- return value != null ? value.equals(cborList.value) : cborList.value == null;
- }
-
- @Override
- public int hashCode() {
- return value != null ? value.hashCode() : 0;
- }
- }
-
- final class CborBoolean implements CborObject {
- public final boolean value;
-
- public CborBoolean(boolean value) {
- this.value = value;
- }
-
- @Override
- public void serialize(CborEncoder encoder) {
- try {
- encoder.writeBoolean(value);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- CborBoolean that = (CborBoolean) o;
-
- return value == that.value;
-
- }
-
- @Override
- public int hashCode() {
- return (value ? 1 : 0);
- }
-
- @Override
- public String toString() {
- return "CborBoolean{" +
- value +
- '}';
- }
- }
-
- final class CborByteArray implements CborObject, Comparable {
- public final byte[] value;
-
- public CborByteArray(byte[] value) {
- this.value = value;
- }
-
- @Override
- public int compareTo(CborByteArray other) {
- return compare(value, other.value);
- }
-
- public static int compare(byte[] a, byte[] b)
- {
- for (int i=0; i < Math.min(a.length, b.length); i++)
- if (a[i] != b[i])
- return a[i] & 0xff - b[i] & 0xff;
- return 0;
- }
-
- @Override
- public void serialize(CborEncoder encoder) {
- try {
- encoder.writeByteString(value);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- CborByteArray that = (CborByteArray) o;
-
- return Arrays.equals(value, that.value);
-
- }
-
- @Override
- public int hashCode() {
- return Arrays.hashCode(value);
- }
- }
-
- final class CborString implements CborObject, Comparable {
-
- public final String value;
-
- public CborString(String value) {
- this.value = value;
- }
-
- @Override
- public int compareTo(CborString cborString) {
- int lenDiff = value.length() - cborString.value.length();
- if (lenDiff != 0)
- return lenDiff;
- return value.compareTo(cborString.value);
- }
-
- @Override
- public void serialize(CborEncoder encoder) {
- try {
- encoder.writeTextString(value);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- CborString that = (CborString) o;
-
- return value.equals(that.value);
-
- }
-
- @Override
- public int hashCode() {
- return value.hashCode();
- }
-
- @Override
- public String toString() {
- return "CborString{\"" +
- value +
- "\"}";
- }
- }
-
- final class CborLong implements CborObject, Comparable {
- public final long value;
-
- public CborLong(long value) {
- this.value = value;
- }
-
- @Override
- public int compareTo(CborLong other) {
- return Long.compare(value, other.value);
- }
-
- @Override
- public void serialize(CborEncoder encoder) {
- try {
- encoder.writeInt(value);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- CborLong cborLong = (CborLong) o;
-
- return value == cborLong.value;
-
- }
-
- @Override
- public int hashCode() {
- return (int) (value ^ (value >>> 32));
- }
-
- @Override
- public String toString() {
- return "CborLong{" +
- value +
- '}';
- }
- }
-
- final class CborNull implements CborObject, Comparable {
- public CborNull() {}
-
- @Override
- public int compareTo(CborNull cborNull) {
- return 0;
- }
-
- @Override
- public void serialize(CborEncoder encoder) {
- try {
- encoder.writeNull();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- return true;
- }
-
- @Override
- public int hashCode() {
- return 0;
- }
-
- @Override
- public String toString() {
- return "CborNull{}";
- }
- }
-}
diff --git a/src/main/java/io/ipfs/api/cbor/CborType.java b/src/main/java/io/ipfs/api/cbor/CborType.java
deleted file mode 100644
index 68b325c..0000000
--- a/src/main/java/io/ipfs/api/cbor/CborType.java
+++ /dev/null
@@ -1,143 +0,0 @@
-package io.ipfs.api.cbor;
-
-/*
- * JACOB - CBOR implementation in Java.
- *
- * (C) Copyright - 2013 - J.W. Janssen
- *
- * Licensed under Apache License v2.0.
- */
-
-import static io.ipfs.api.cbor.CborConstants.*;
-
-/**
- * Represents the various major types in CBOR, along with their .
- *
- * The major type is encoded in the upper three bits of each initial byte. The lower 5 bytes represent any additional information.
- *
- */
-public class CborType {
- private final int m_major;
- private final int m_additional;
-
- private CborType(int major, int additional) {
- m_major = major;
- m_additional = additional;
- }
-
- /**
- * Returns a descriptive string for the given major type.
- *
- * @param mt the major type to return as string, values from [0..7] are supported.
- * @return the name of the given major type, as String, never null
.
- * @throws IllegalArgumentException in case the given major type is not supported.
- */
- public static String getName(int mt) {
- switch (mt) {
- case TYPE_ARRAY:
- return "array";
- case TYPE_BYTE_STRING:
- return "byte string";
- case TYPE_FLOAT_SIMPLE:
- return "float/simple value";
- case TYPE_MAP:
- return "map";
- case TYPE_NEGATIVE_INTEGER:
- return "negative integer";
- case TYPE_TAG:
- return "tag";
- case TYPE_TEXT_STRING:
- return "text string";
- case TYPE_UNSIGNED_INTEGER:
- return "unsigned integer";
- default:
- throw new IllegalArgumentException("Invalid major type: " + mt);
- }
- }
-
- /**
- * Decodes a given byte value to a {@link CborType} value.
- *
- * @param i the input byte (8-bit) to decode into a {@link CborType} instance.
- * @return a {@link CborType} instance, never null
.
- */
- public static CborType valueOf(int i) {
- return new CborType((i & 0xff) >>> 5, i & 0x1f);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null || getClass() != obj.getClass()) {
- return false;
- }
-
- CborType other = (CborType) obj;
- return (m_major == other.m_major) && (m_additional == other.m_additional);
- }
-
- /**
- * @return the additional information of this type, as integer value from [0..31].
- */
- public int getAdditionalInfo() {
- return m_additional;
- }
-
- /**
- * @return the major type, as integer value from [0..7].
- */
- public int getMajorType() {
- return m_major;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + m_additional;
- result = prime * result + m_major;
- return result;
- }
-
- /**
- * @return true
if this type allows for an infinite-length payload,
- * false
if only definite-length payloads are allowed.
- */
- public boolean isBreakAllowed() {
- return m_major == TYPE_ARRAY || m_major == TYPE_BYTE_STRING || m_major == TYPE_MAP
- || m_major == TYPE_TEXT_STRING;
- }
-
- /**
- * Determines whether the major type of a given {@link CborType} equals the major type of this {@link CborType}.
- *
- * @param other the {@link CborType} to compare against, cannot be null
.
- * @return true
if the given {@link CborType} is of the same major type as this {@link CborType}, false
otherwise.
- * @throws IllegalArgumentException in case the given argument was null
.
- */
- public boolean isEqualType(CborType other) {
- if (other == null) {
- throw new IllegalArgumentException("Parameter cannot be null!");
- }
- return m_major == other.m_major;
- }
-
- /**
- * Determines whether the major type of a given byte value (representing an encoded {@link CborType}) equals the major type of this {@link CborType}.
- *
- * @param encoded the encoded CBOR type to compare.
- * @return true
if the given byte value represents the same major type as this {@link CborType}, false
otherwise.
- */
- public boolean isEqualType(int encoded) {
- return m_major == ((encoded & 0xff) >>> 5);
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(getName(m_major)).append('(').append(m_additional).append(')');
- return sb.toString();
- }
-}
\ No newline at end of file
diff --git a/src/main/java/io/ipfs/api/cbor/Cborable.java b/src/main/java/io/ipfs/api/cbor/Cborable.java
deleted file mode 100644
index 2025e72..0000000
--- a/src/main/java/io/ipfs/api/cbor/Cborable.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package io.ipfs.api.cbor;
-
-public interface Cborable {
-
- CborObject toCbor();
-
- default byte[] serialize() {
- return toCbor().toByteArray();
- }
-}
diff --git a/src/main/java/io/ipfs/api/ipfsTest.java b/src/main/java/io/ipfs/api/ipfsTest.java
new file mode 100644
index 0000000..d6d4486
--- /dev/null
+++ b/src/main/java/io/ipfs/api/ipfsTest.java
@@ -0,0 +1,111 @@
+package io.ipfs.api;
+
+import io.ipfs.api.IPFS;
+import io.ipfs.api.NamedStreamable;
+
+import java.io.*;
+import java.lang.reflect.Array;
+import java.net.URL;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.*;
+import java.util.stream.Stream;
+import java.util.stream.Collectors;
+
+
+import io.ipfs.api.cbor.*;
+import io.ipfs.cid.*;
+import io.ipfs.multihash.Multihash;
+import io.ipfs.multiaddr.MultiAddress;
+
+
+public class ipfsTest{
+ public static class Node1 extends Thread{
+ MyIPFSClass ipfs = new MyIPFSClass();
+
+ public void run(){
+ double [] arr = {1,2,3,4};
+ int i = 0;
+ List