implement bencode base types

This commit is contained in:
mykola2312 2024-10-13 09:09:23 +03:00
parent da9c812187
commit 7df116d02b
3 changed files with 52 additions and 8 deletions

View 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;
}
}

View 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);
}
}

View file

@ -10,26 +10,27 @@ import java.util.Iterator;
*/
abstract public class BValue implements Iterable<BValue> {
private BType type;
private BValue next = null;
private BValue child = null;
protected BValue(BType type) {
this.type = type;
}
public BType getType() {
return type;
}
abstract public BType getType();
public BValue getNext() {
return next;
}
public void setNext(BValue next) {
this.next = next;
}
public BValue getChild() {
return child;
}
public void setChild(BValue child) {
this.child = child;
}
public Iterator<BValue> iterator() {
return new BValueIterator(child);
}