Giter VIP home page Giter VIP logo

bencode's Introduction

bencode

Build Status Coverage Status Maven GitHub license

Bencode Input/Output Streams for Java

Requires JDK 1.8 or higher

Bencode Spec

Bencode Wikipedia

Javadoc

http://dampcake.github.io/bencode

Usage

Maven

<dependency>
    <groupId>com.dampcake</groupId>
    <artifactId>bencode</artifactId>
    <version>1.4.1</version>
</dependency>

Gradle

compile 'com.dampcake:bencode:1.4.1'

Examples

Bencode Data

Bencode bencode = new Bencode();
byte[] encoded = bencode.encode(new HashMap<Object, Object>() {{
    put("string", "value");
    put("number", 123456);
    put("list", new ArrayList<Object>() {{
        add("list-item-1");
        add("list-item-2");
    }});
    put("dict", new ConcurrentSkipListMap() {{
        put(123, "test");
        put(456, "thing");
    }});
}});

System.out.println(new String(encoded, bencode.getCharset()));

Outputs: d4:dictd3:1234:test3:4565:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee

Decode Bencoded Data:

Bencode bencode = new Bencode();
Map<String, Object> dict = bencode.decode("d4:dictd3:1234:test3:4565:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee".getBytes(), Type.DICTIONARY);

System.out.println(dict);

Outputs: {dict={123=test, 456=thing}, list=[list-item-1, list-item-2], number=123456, string=value}

Write bencoded data to a Stream:

ByteArrayOutputStream out = new ByteArrayOutputStream();
BencodeOutputStream bencoder = new BencodeOutputStream(out);

bencoder.writeDictionary(new HashMap<Object, Object>() {{
    put("string", "value");
    put("number", 123456);
    put("list", new ArrayList<Object>() {{
        add("list-item-1");
        add("list-item-2");
    }});
    put("dict", new ConcurrentSkipListMap() {{
        put("dict-item-1", "test");
        put("dict-item-2", "thing");
    }});
}});

System.out.println(new String(out.toByteArray()));

Outputs: d4:dictd11:dict-item-14:test11:dict-item-25:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee

Read bencoded data to a Stream:

String input = "d4:dictd11:dict-item-14:test11:dict-item-25:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee";
ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
BencodeInputStream bencode = new BencodeInputStream(in);

Type type = bencode.nextType(); // Returns Type.DICTIONARY
Map<String, Object> dict = bencode.readDictionary();

System.out.println(dict);

Outputs: {dict={dict-item-1=test, dict-item-2=thing}, list=[list-item-1, list-item-2], number=123456, string=value}

bencode's People

Contributors

alepar avatar andsdev avatar aruthane avatar dampcake avatar dependabot[bot] avatar eontechnology avatar manuel-sugawara avatar thelq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

bencode's Issues

Why did I get the hash incorrectly?

java env

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

critical code

/**
 * torrentFile see torrent.zip
 */
byte[] torrentData = FileUtils.readFileToByteArray(new File(torrentFile))
Bencode bencode = new Bencode();
Map<String, Object> torrentMap = bencode.decode(torrentData, Type.DICTIONARY);
Map<String, Object> info = (Map<String, Object>) torrentMap.get("info");
byte[] hashByte = hash(bencode.encode(info));
System.out.println(bytesToHex(hashByte));

public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_SYMBOLS[v >>> 4];
            hexChars[j * 2 + 1] = HEX_SYMBOLS[v & 0x0F];
        }
        return new String(hexChars);
    }

The correct hash is:
C8AE498D84E1ABC0EE9A72046E31E82B6A5C8C74
but i get hash is
D5FFE89589B9BACB195C2771C3FC328BF3880623

torrent.zip

thx, - ̗̀(๑ᵔ⌔ᵔ๑)

NumberFormatException: For input string: "-2.9155148901435E+18"

While using bencode-1.3 to decode torrent files, got exception below:

Caused by: java.lang.NumberFormatException: For input string: "-2.9155148901435E+18"
at java.lang.NumberFormatException.forInputString(Unknown Source) ~[?:1.8.0_152]
at java.lang.Long.parseLong(Unknown Source) ~[?:1.8.0_152]
at java.lang.Long.parseLong(Unknown Source) ~[?:1.8.0_152]
at com.dampcake.bencode.BencodeInputStream.readNumber(BencodeInputStream.java:201) ~[bencode-1.3.jar:?]
at com.dampcake.bencode.BencodeInputStream.readObject(BencodeInputStream.java:261) ~[bencode-1.3.jar:?]
at com.dampcake.bencode.BencodeInputStream.readDictionary(BencodeInputStream.java:245) ~[bencode-1.3.jar:?]
at com.dampcake.bencode.BencodeInputStream.readObject(BencodeInputStream.java:265) ~[bencode-1.3.jar:?]
at com.dampcake.bencode.BencodeInputStream.readDictionary(BencodeInputStream.java:245) ~[bencode-1.3.jar:?]
at dpf.mg.udi.gpinf.bittorrent.BencodedDict.(BencodedDict.java:27) ~[iped-parsers-impl-3.17.2.jar:?]
at dpf.mg.udi.gpinf.bittorrent.TorrentFileParser.parse(TorrentFileParser.java:58) ~[iped-parsers-impl-3.17.2.jar:?]
at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:280) ~[tika-core-1.20.jar:1.20]

Decode exception when decoding string from http://bttracker.debian.org/

Hi,

Just found may be an issue, when I try to use bencode.decode some string from debian bttracker, always got exception.

How to reproduce:

Make a get call use following url:
http://bttracker.debian.org:6969/announce?info_hash=N%f5%ad%16%c0%9e%b5%0b%8c%3b%90~%02u%24%eeC%ad%ad%01&peer_id=%ff%fe%fd%fc%fb%fa%f9%f8%f7%f6%f5%f4%f3%f2%f1%f0%ef%ee%ed%ec&port=6881&uploaded=0&downloaded=0&left=659554304%22

then will get following UTF_8 String back:

"d8:intervali900e5:peers2:ip13:213.49.181.984:porti6881e2:ip14:216.195.129.274:porti6881e2:ip12:146.71.73.214:porti6881e2:ip12:146.71.73.514:porti6881eee"

When I try to decode the above string,

Bencode bencode = new Bencode(StandardCharsets.UTF_8, true);
bencode.decode("d8:intervali900e5:peers2:ip13:213.49.181.984:porti6881e2:ip14:216.195.129.274:porti6881e2:ip12:146.71.73.214:porti6881e2:ip12:146.71.73.514:porti6881eee".getBytes(StandardCharsets.UTF_8,), Type.DICTIONARY);

then got an exception:

"Unexcept token 'i'"

I try to debug it, then found out that "d8:" these 3 chars are ok, till the fourth chars "i", then got the exception.

I just want to confirm, if it is the string encode problem? or something else?

btw: Env: Java 11, Mac OS. I use the example string in the README.md, the decoding works fine.

Thank you for your help!

best regards

Mike

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.