GithubHelp home page GithubHelp logo

cdefgah / bencoder4j Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 380 KB

Bit-torrent encoding and decoding implementation for Java programming language.

License: MIT License

Java 100.00%
bencoding bencode java torrent bit-torrent bencoder deserialization javalibrary library serialization

bencoder4j's Introduction

Bencode Serializer and Deserializer

Build Status Maven Central

Bit-torrent encoding and decoding implementation for the Java programming language.

Simple use case

This project is available as a Maven dependency:

<dependency>
    <groupId>com.github.cdefgah</groupId>
    <artifactId>bencoder4j</artifactId>
    <version>1.1.0</version>
</dependency>

Below, you can see a piece of code that writes a set of arbitrary objects to the file.ben, located in my home Downloads folder.

Bear in mind that provided examples fit my OS (Ubuntu Linux) and environment (path with my user name). Chances are that these parameters slightly differ in your case, so be sure to modify the filePath variable accordingly.

package example;

import com.github.cdefgah.bencoder4j.CircularReferenceException;
import com.github.cdefgah.bencoder4j.model.BencodedByteSequence;
import com.github.cdefgah.bencoder4j.model.BencodedDictionary;
import com.github.cdefgah.bencoder4j.model.BencodedInteger;
import com.github.cdefgah.bencoder4j.model.BencodedList;

import java.io.FileOutputStream;
import java.io.IOException;

public class WriterApp {

    public static void main(String[] args) throws IOException, CircularReferenceException {

        BencodedInteger bencodedInteger = new BencodedInteger(1234567890);
        BencodedByteSequence bencodedByteSequence = new BencodedByteSequence("Hi :)");

        BencodedList bencodedList = new BencodedList();
        BencodedInteger intListElement = new BencodedInteger(1);
        BencodedByteSequence bbsListElement = new BencodedByteSequence("abc");

        bencodedList.add(intListElement);
        bencodedList.add(bbsListElement);

        BencodedDictionary bencodedDictionary = new BencodedDictionary();

        BencodedByteSequence dictKey1 = new BencodedByteSequence("111");
        BencodedByteSequence dictKey2 = new BencodedByteSequence("222");

        BencodedInteger intDictElement = new BencodedInteger(2);
        BencodedByteSequence bbsDictElement = new BencodedByteSequence("xyz");

        bencodedDictionary.put(dictKey1, intDictElement);
        bencodedDictionary.put(dictKey2, bbsDictElement);


        String filePath = "/home/rafael/Downloads/file.ben";
        try (FileOutputStream fos = new FileOutputStream(filePath)) {

            bencodedInteger.writeObject(fos);
            bencodedByteSequence.writeObject(fos);
            bencodedList.writeObject(fos);
            bencodedDictionary.writeObject(fos);

        }
    }
}

Now, as we have the composed file.benin the Downloads folder, we can use the following code to read its contents.

package example;

import com.github.cdefgah.bencoder4j.BencodeFormatException;
import com.github.cdefgah.bencoder4j.io.BencodeStreamIterator;
import com.github.cdefgah.bencoder4j.model.BencodedObject;

import java.io.FileInputStream;
import java.io.IOException;

public class ReaderApp {
    public static void main(String[] args) throws IOException, BencodeFormatException {

        String filePath = "/home/rafael/Downloads/file.ben";
        try(FileInputStream fis = new FileInputStream(filePath)) {
            BencodeStreamIterator bis = new BencodeStreamIterator(fis);

            while (bis.hasNext()) {
                BencodedObject bencodedObject = bis.next();
                System.out.println(bencodedObject);
            }
        }
    }
}

Use cases given above are the most basic, simple examples. For more please refer to the tests for the project.

Documentation

API Documentation

Changelog

Licensing

MIT License

bencoder4j's People

Contributors

cdefgah avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

adeleluck

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.