https://github.com/cdefgah/bencoder4j
Bit-torrent encoding and decoding implementation for Java programming language.
https://github.com/cdefgah/bencoder4j
bencode bencoder bencoding bit-torrent deserialization java javalibrary library serialization torrent
Last synced: 4 months ago
JSON representation
Bit-torrent encoding and decoding implementation for Java programming language.
- Host: GitHub
- URL: https://github.com/cdefgah/bencoder4j
- Owner: cdefgah
- License: mit
- Created: 2018-01-07T23:28:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-12-16T17:30:35.000Z (5 months ago)
- Last Synced: 2024-12-16T18:31:05.488Z (5 months ago)
- Topics: bencode, bencoder, bencoding, bit-torrent, deserialization, java, javalibrary, library, serialization, torrent
- Language: Java
- Homepage:
- Size: 373 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Bencode Serializer and Deserializer
[](https://github.com/cdefgah/bencoder4j/actions) [](http://search.maven.org/#artifactdetails%7Ccom.github.cdefgah%7Cbencoder4j%7C1.1.0%7Cjar)
[Bit-torrent](https://en.wikipedia.org/wiki/Bencode) encoding and decoding implementation for the Java programming language.
### Simple use case
This project is available as a [Maven](https://maven.apache.org/) [dependency](http://search.maven.org/#artifactdetails%7Ccom.github.cdefgah%7Cbencoder4j%7C1.1.0%7Cjar):
```xml
com.github.cdefgah
bencoder4j
1.1.0```
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.
```java
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.ben`in the `Downloads` folder, we can use the following code to read its contents.
```java
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](https://github.com/cdefgah/bencoder4j/tree/master/src/test) for the project.
### Documentation
[API Documentation](https://cdefgah.github.io/bencoder4j/apidocs/allclasses-index.html)
[Changelog](https://github.com/cdefgah/bencoder4j/blob/master/CHANGELOG.md)
### Licensing
[MIT License](https://github.com/cdefgah/bencoder4j/blob/master/LICENSE)