implement bencode base types
This commit is contained in:
parent
da9c812187
commit
7df116d02b
3 changed files with 52 additions and 8 deletions
18
src/main/java/com/mykola2312/retracker/bencode/BInteger.java
Normal file
18
src/main/java/com/mykola2312/retracker/bencode/BInteger.java
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.mykola2312.retracker.bencode;
|
||||||
|
|
||||||
|
public class BInteger extends BValue {
|
||||||
|
private long value;
|
||||||
|
|
||||||
|
public BInteger(long value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override()
|
||||||
|
public BType getType() {
|
||||||
|
return BType.INTEGER;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long get() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/main/java/com/mykola2312/retracker/bencode/BString.java
Normal file
25
src/main/java/com/mykola2312/retracker/bencode/BString.java
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.mykola2312.retracker.bencode;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class BString extends BValue {
|
||||||
|
private byte[] bytes;
|
||||||
|
|
||||||
|
public BString(byte[] bytes) {
|
||||||
|
this.bytes = bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override()
|
||||||
|
public BType getType() {
|
||||||
|
return BType.STRING;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] get() {
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override()
|
||||||
|
public String toString() {
|
||||||
|
return new String(bytes, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,26 +10,27 @@ import java.util.Iterator;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
abstract public class BValue implements Iterable<BValue> {
|
abstract public class BValue implements Iterable<BValue> {
|
||||||
private BType type;
|
|
||||||
private BValue next = null;
|
private BValue next = null;
|
||||||
private BValue child = null;
|
private BValue child = null;
|
||||||
|
|
||||||
protected BValue(BType type) {
|
abstract public BType getType();
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BType getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BValue getNext() {
|
public BValue getNext() {
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setNext(BValue next) {
|
||||||
|
this.next = next;
|
||||||
|
}
|
||||||
|
|
||||||
public BValue getChild() {
|
public BValue getChild() {
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setChild(BValue child) {
|
||||||
|
this.child = child;
|
||||||
|
}
|
||||||
|
|
||||||
public Iterator<BValue> iterator() {
|
public Iterator<BValue> iterator() {
|
||||||
return new BValueIterator(child);
|
return new BValueIterator(child);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue