当前位置: 首页>>代码示例>>Java>>正文


Java Multihash类代码示例

本文整理汇总了Java中io.ipfs.multihash.Multihash的典型用法代码示例。如果您正苦于以下问题:Java Multihash类的具体用法?Java Multihash怎么用?Java Multihash使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Multihash类属于io.ipfs.multihash包,在下文中一共展示了Multihash类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: pinUpdate

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
@Test
public void pinUpdate() throws IOException {
    MerkleNode child1 = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes())).get(0);
    Multihash hashChild1 = child1.hash;
    System.out.println("child1: " + hashChild1);

    CborObject.CborMerkleLink root1 = new CborObject.CborMerkleLink(hashChild1);
    MerkleNode root1Res = ipfs.block.put(Collections.singletonList(root1.toByteArray()), Optional.of("cbor")).get(0);
    System.out.println("root1: " + root1Res.hash);
    ipfs.pin.add(root1Res.hash);

    CborObject.CborList root2 = new CborObject.CborList(Arrays.asList(new CborObject.CborMerkleLink(hashChild1), new CborObject.CborLong(42)));
    MerkleNode root2Res = ipfs.block.put(Collections.singletonList(root2.toByteArray()), Optional.of("cbor")).get(0);
    List<MultiAddress> update = ipfs.pin.update(root1Res.hash, root2Res.hash, true);

    Map<Multihash, Object> ls = ipfs.pin.ls(IPFS.PinType.all);
    boolean childPresent = ls.containsKey(hashChild1);
    if (!childPresent)
        throw new IllegalStateException("Child not present!");

    ipfs.repo.gc();
    Map<Multihash, Object> ls2 = ipfs.pin.ls(IPFS.PinType.all);
    boolean childPresentAfterGC = ls2.containsKey(hashChild1);
    if (!childPresentAfterGC)
        throw new IllegalStateException("Child not present!");
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:27,代码来源:APITest.java

示例2: rawLeafNodePinUpdate

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
@Test
public void rawLeafNodePinUpdate() throws IOException {
    MerkleNode child1 = ipfs.block.put("some data".getBytes(), Optional.of("raw"));
    Multihash hashChild1 = child1.hash;
    System.out.println("child1: " + hashChild1.type);

    CborObject.CborMerkleLink root1 = new CborObject.CborMerkleLink(hashChild1);
    MerkleNode root1Res = ipfs.block.put(Collections.singletonList(root1.toByteArray()), Optional.of("cbor")).get(0);
    System.out.println("root1: " + root1Res.hash);
    ipfs.pin.add(root1Res.hash);

    MerkleNode child2 = ipfs.block.put("G'day new tree".getBytes(), Optional.of("raw"));
    Multihash hashChild2 = child2.hash;

    CborObject.CborList root2 = new CborObject.CborList(Arrays.asList(
            new CborObject.CborMerkleLink(hashChild1),
            new CborObject.CborMerkleLink(hashChild2),
            new CborObject.CborLong(42))
    );
    MerkleNode root2Res = ipfs.block.put(Collections.singletonList(root2.toByteArray()), Optional.of("cbor")).get(0);
    List<MultiAddress> update = ipfs.pin.update(root1Res.hash, root2Res.hash, false);
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:23,代码来源:APITest.java

示例3: indirectPinTest

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
@Test
    public void indirectPinTest() throws IOException {
        Multihash EMPTY = ipfs.object._new(Optional.empty()).hash;
        io.ipfs.api.MerkleNode data = ipfs.object.patch(EMPTY, "set-data", Optional.of("childdata".getBytes()), Optional.empty(), Optional.empty());
        Multihash child = data.hash;

        io.ipfs.api.MerkleNode tmp1 = ipfs.object.patch(EMPTY, "set-data", Optional.of("parent1_data".getBytes()), Optional.empty(), Optional.empty());
        Multihash parent1 = ipfs.object.patch(tmp1.hash, "add-link", Optional.empty(), Optional.of(child.toString()), Optional.of(child)).hash;
        ipfs.pin.add(parent1);

        io.ipfs.api.MerkleNode tmp2 = ipfs.object.patch(EMPTY, "set-data", Optional.of("parent2_data".getBytes()), Optional.empty(), Optional.empty());
        Multihash parent2 = ipfs.object.patch(tmp2.hash, "add-link", Optional.empty(), Optional.of(child.toString()), Optional.of(child)).hash;
        ipfs.pin.add(parent2);
        ipfs.pin.rm(parent1, true);

        Map<Multihash, Object> ls = ipfs.pin.ls(IPFS.PinType.all);
        boolean childPresent = ls.containsKey(child);
        if (!childPresent)
            throw new IllegalStateException("Child not present!");

        ipfs.repo.gc();
        Map<Multihash, Object> ls2 = ipfs.pin.ls(IPFS.PinType.all);
        boolean childPresentAfterGC = ls2.containsKey(child);
        if (!childPresentAfterGC)
            throw new IllegalStateException("Child not present!");
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:27,代码来源:APITest.java

示例4: rootNull

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
/**
 *  Test that a cbor null is allowed as an object root
 */
@Test
public void rootNull() throws IOException {
    CborObject.CborNull cbor = new CborObject.CborNull();
    byte[] obj = cbor.toByteArray();
    MerkleNode block = ipfs.block.put(Arrays.asList(obj), Optional.of("cbor")).get(0);
    byte[] retrievedObj = ipfs.block.get(block.hash);
    Assert.assertTrue("get inverse of put", Arrays.equals(retrievedObj, obj));

    List<Multihash> add = ipfs.pin.add(block.hash);
    ipfs.repo.gc();
    ipfs.repo.gc();

    // These commands can be used to reproduce this on the command line
    String reproCommand1 = "printf \"" + toEscapedHex(obj) + "\" | ipfs block put --format=cbor";
    System.out.println();
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:20,代码来源:APITest.java

示例5: merkleLinkInList

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
/**
 *  Test that merkle links in a cbor list are followed during recursive pins
 */
@Test
public void merkleLinkInList() throws IOException {
    Random r = new Random();
    CborObject.CborByteArray target = new CborObject.CborByteArray(("g'day IPFS!" + r.nextInt()).getBytes());
    byte[] rawTarget = target.toByteArray();
    MerkleNode targetRes = ipfs.block.put(Arrays.asList(rawTarget), Optional.of("cbor")).get(0);

    CborObject.CborMerkleLink link = new CborObject.CborMerkleLink(targetRes.hash);
    CborObject.CborList source = new CborObject.CborList(Arrays.asList(link));
    byte[] rawSource = source.toByteArray();
    MerkleNode sourceRes = ipfs.block.put(Arrays.asList(rawSource), Optional.of("cbor")).get(0);

    List<Multihash> add = ipfs.pin.add(sourceRes.hash);
    ipfs.repo.gc();
    ipfs.repo.gc();

    byte[] bytes = ipfs.block.get(targetRes.hash);
    Assert.assertTrue("same contents after GC", Arrays.equals(bytes, rawTarget));
    // These commands can be used to reproduce this on the command line
    String reproCommand1 = "printf \"" + toEscapedHex(rawTarget) + "\" | ipfs block put --format=cbor";
    String reproCommand2 = "printf \"" + toEscapedHex(rawSource) + "\" | ipfs block put --format=cbor";
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:26,代码来源:APITest.java

示例6: refs

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
public List<Multihash> refs(Multihash hash, boolean recursive) throws IOException {
    String jsonStream = new String(retrieve("refs?arg=" + hash + "&r=" + recursive));
    return JSONParser.parseStream(jsonStream).stream()
            .map(m -> (String) (((Map) m).get("Ref")))
            .map(Cid::decode)
            .collect(Collectors.toList());
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:8,代码来源:IPFS.java

示例7: local

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
public List<Multihash> local() throws IOException {
    String jsonStream = new String(retrieve("refs/local"));
    return JSONParser.parseStream(jsonStream).stream()
            .map(m -> (String) (((Map) m).get("Ref")))
            .map(Cid::decode)
            .collect(Collectors.toList());
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:8,代码来源:IPFS.java

示例8: patch

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, Optional<String> name, Optional<Multihash> target) throws IOException {
    if (!ObjectPatchTypes.contains(command))
        throw new IllegalStateException("Illegal Object.patch command type: "+command);
    String targetPath = "object/patch/"+command+"?arg=" + base.toBase58();
    if (name.isPresent())
        targetPath += "&arg=" + name.get();
    if (target.isPresent())
        targetPath += "&arg=" + target.get().toBase58();

    switch (command) {
        case "add-link":
            if (!target.isPresent())
                throw new IllegalStateException("add-link requires name and target!");
        case "rm-link":
            if (!name.isPresent())
                throw new IllegalStateException("link name is required!");
            return MerkleNode.fromJSON(retrieveMap(targetPath));
        case "set-data":
        case "append-data":
            if (!data.isPresent())
                throw new IllegalStateException("set-data requires data!");
            Multipart m = new Multipart("http://" + host + ":" + port + version+"object/patch/"+command+"?arg="+base.toBase58()+"&stream-channels=true", "UTF-8");
            m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data.get()));
            String res = m.finish();
            return MerkleNode.fromJSON(JSONParser.parse(res));

        default:
            throw new IllegalStateException("Unimplemented");
    }
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:31,代码来源:IPFS.java

示例9: dag

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
@Test
public void dag() throws IOException {
    byte[] object = "{\"data\":1234}".getBytes();
    MerkleNode put = ipfs.dag.put("json", object);

    Cid expected = Cid.decode("zdpuB2CbdLrUK5AgZusm4hraisDDDC135ugdmZWvMHhhsSYTb");

    Multihash result = put.hash;
    Assert.assertTrue("Correct cid returned", result.equals(expected));

    byte[] get = ipfs.dag.get(expected);
    Assert.assertTrue("Raw data equal", Arrays.equals(object, get));
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:14,代码来源:APITest.java

示例10: wrappedSingleFileTest

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
@Test
public void wrappedSingleFileTest() throws IOException {
    NamedStreamable.ByteArrayWrapper file = new NamedStreamable.ByteArrayWrapper("hello.txt", "G'day world! IPFS rocks!".getBytes());
    List<MerkleNode> addParts = ipfs.add(file, true);
    MerkleNode filePart = addParts.get(0);
    MerkleNode dirPart = addParts.get(1);
    byte[] catResult = ipfs.cat(filePart.hash);
    byte[] getResult = ipfs.get(filePart.hash);
    if (!Arrays.equals(catResult, file.getContents()))
        throw new IllegalStateException("Different contents!");
    List<Multihash> pinRm = ipfs.pin.rm(dirPart.hash, true);
    if (!pinRm.get(0).equals(dirPart.hash))
        throw new IllegalStateException("Didn't remove file!");
    Object gc = ipfs.repo.gc();
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:16,代码来源:APITest.java

示例11: hashOnly

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
@Test
public void hashOnly() throws IOException {
    byte[] data = randomBytes(4096);
    NamedStreamable file = new NamedStreamable.ByteArrayWrapper(data);
    MerkleNode addResult = ipfs.add(file, false, true).get(0);
    List<Multihash> local = ipfs.refs.local();
    if (local.contains(addResult.hash))
        throw new IllegalStateException("Object shouldn't be present!");
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:10,代码来源:APITest.java

示例12: fileTest

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
public void fileTest(NamedStreamable file)  throws IOException{
    MerkleNode addResult = ipfs.add(file).get(0);
    byte[] catResult = ipfs.cat(addResult.hash);
    byte[] getResult = ipfs.get(addResult.hash);
    if (!Arrays.equals(catResult, file.getContents()))
        throw new IllegalStateException("Different contents!");
    List<Multihash> pinRm = ipfs.pin.rm(addResult.hash, true);
    if (!pinRm.get(0).equals(addResult.hash))
        throw new IllegalStateException("Didn't remove file!");
    Object gc = ipfs.repo.gc();
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:12,代码来源:APITest.java

示例13: objectPatch

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
@Test
    public void objectPatch() throws IOException {
        MerkleNode obj = ipfs.object._new(Optional.empty());
        Multihash base = obj.hash;
        // link tests
        String linkName = "alink";
        MerkleNode addLink = ipfs.object.patch(base, "add-link", Optional.empty(), Optional.of(linkName), Optional.of(base));
        MerkleNode withLink = ipfs.object.get(addLink.hash);
        if (withLink.links.size() != 1 || !withLink.links.get(0).hash.equals(base) || !withLink.links.get(0).name.get().equals(linkName))
            throw new RuntimeException("Added link not correct!");
        MerkleNode rmLink = ipfs.object.patch(addLink.hash, "rm-link", Optional.empty(), Optional.of(linkName), Optional.empty());
        if (!rmLink.hash.equals(base))
            throw new RuntimeException("Adding not inverse of removing link!");

        // data tests
//            byte[] data = "some random textual data".getBytes();
        byte[] data = new byte[1024];
        new Random().nextBytes(data);
        MerkleNode patched = ipfs.object.patch(base, "set-data", Optional.of(data), Optional.empty(), Optional.empty());
        byte[] patchedResult = ipfs.object.data(patched.hash);
        if (!Arrays.equals(patchedResult, data))
            throw new RuntimeException("object.patch: returned data != stored data!");

        MerkleNode twicePatched = ipfs.object.patch(patched.hash, "append-data", Optional.of(data), Optional.empty(), Optional.empty());
        byte[] twicePatchedResult = ipfs.object.data(twicePatched.hash);
        byte[] twice = new byte[2*data.length];
        for (int i=0; i < 2; i++)
            System.arraycopy(data, 0, twice, i*data.length, data.length);
        if (!Arrays.equals(twicePatchedResult, twice))
            throw new RuntimeException("object.patch: returned data after append != stored data!");

    }
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:33,代码来源:APITest.java

示例14: bulkBlockTest

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
@Test
public void bulkBlockTest() throws IOException {
    CborObject cbor = new CborObject.CborString("G'day IPFS!");
    byte[] raw = cbor.toByteArray();
    List<MerkleNode> bulkPut = ipfs.block.put(Arrays.asList(raw, raw, raw, raw, raw), Optional.of("cbor"));
    List<Multihash> hashes = bulkPut.stream().map(m -> m.hash).collect(Collectors.toList());
    byte[] result = ipfs.block.get(hashes.get(0));
    System.out.println();
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:10,代码来源:APITest.java

示例15: merkleLinkInMap

import io.ipfs.multihash.Multihash; //导入依赖的package包/类
/**
 *  Test that merkle links in values of a cbor map are followed during recursive pins
 */
@Test
public void merkleLinkInMap() throws IOException {
    Random r = new Random();
    CborObject.CborByteArray target = new CborObject.CborByteArray(("g'day IPFS!").getBytes());
    byte[] rawTarget = target.toByteArray();
    MerkleNode targetRes = ipfs.block.put(Arrays.asList(rawTarget), Optional.of("cbor")).get(0);

    CborObject.CborMerkleLink link = new CborObject.CborMerkleLink(targetRes.hash);
    Map<String, CborObject> m = new TreeMap<>();
    m.put("alink", link);
    m.put("arr", new CborObject.CborList(Collections.emptyList()));
    CborObject.CborMap source = CborObject.CborMap.build(m);
    byte[] rawSource = source.toByteArray();
    MerkleNode sourceRes = ipfs.block.put(Arrays.asList(rawSource), Optional.of("cbor")).get(0);

    CborObject.fromByteArray(rawSource);

    List<Multihash> add = ipfs.pin.add(sourceRes.hash);
    ipfs.repo.gc();
    ipfs.repo.gc();

    List<Multihash> refs = ipfs.refs(sourceRes.hash, true);
    Assert.assertTrue("refs returns links", refs.contains(targetRes.hash));

    byte[] bytes = ipfs.block.get(targetRes.hash);
    Assert.assertTrue("same contents after GC", Arrays.equals(bytes, rawTarget));
    // These commands can be used to reproduce this on the command line
    String reproCommand1 = "printf \"" + toEscapedHex(rawTarget) + "\" | ipfs block put --format=cbor";
    String reproCommand2 = "printf \"" + toEscapedHex(rawSource) + "\" | ipfs block put --format=cbor";
    System.out.println();
}
 
开发者ID:ipfs,项目名称:java-ipfs-api,代码行数:35,代码来源:APITest.java


注:本文中的io.ipfs.multihash.Multihash类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。