私が作っているトレントダウンロードシステム用のベンコーディングシステムを実装しています。
文字列のベンコーディングは非常に簡単です。たとえば「hello」などの文字列を取得し、文字列の長さ+「:」文字の後に文字列自体を記述してエンコードします。ベンコードされた「hello」は「5:hello」になります
現在、私はこのコードを持っています。
public BencodeString(String string) {
this.string = string;
}
public static BencodeString parseBencodeString(String string) {
byte[] bytes = string.getBytes();
int position = 0;
int size = 0;
StringBuilder sb = new StringBuilder();
while (bytes[position] >= '0' && bytes[position] <= '9') {
sb.append((char) bytes[position]);
position++;
}
if (bytes[position] != ':')
return null;
size = Integer.parseInt(sb.toString());
System.out.println(size);
if (size <= 0)
return null;
return new BencodeString(string.substring(position + 1, size + position
+ 1));
}
それは機能しますが、私はそれがもっとうまくできると感じています。これを行うための最良の方法は何ですか?
注:文字列は任意のサイズにすることができます(したがって、文字列の前に1桁以上)
ここに返信してくれたみんなに感謝します:)