私はrouter.bittorrent.comからのfind_node応答のノードをstringにデコードし、デコードされた "ノード"にfind_node要求を送りましたが、 "ノード"からのfind_node応答を復活させたことはありません。Javaのコンパクトノード情報をデコードするには?
byte[] nodesBytes = ((String)nodes).getBytes();
ByteBuffer buffer = ByteBuffer.wrap(nodesBytes);
int size = nodesBytes.length/26;
for (int i = 0; i < size; i++) {
byte[] bytes = new byte[26];
byte[] nodeIdBytes = Arrays.copyOfRange(bytes, 0, 20);
byte[] ipBytes = Arrays.copyOfRange(bytes, 20, 24);
byte[] portBytes = Arrays.copyOfRange(bytes, 24, 26);
RoutingTable.RoutingNode routingNode = new RoutingTable.RoutingNode();
try {
routingNode.nodeId = nodeIdBytes;
routingNode.ip = InetAddress.getByAddress(ipBytes);
routingNode.port = (((((short)portBytes[1]) << 8) & 0xFF00) + (((short)portBytes[0]) & 0x00FF));
} catch (UnknownHostException e) {
e.printStackTrace();
}
send(routingNode);
}
とデコードの文字列コード
private static String decodeString(ByteBuffer byteBuffer) {
try {
StringBuilder buffer = new StringBuilder();
int type = byteBuffer.get();
buffer.append((char) type);
do {
byte a = byteBuffer.get();
if (a == SEPARATOR) {
break;
} else {
buffer.append((char) a);
}
} while (true);
int length = Integer.parseInt(buffer.toString());
byte[] bytes = new byte[length];
byteBuffer.get(bytes);
String value = new String(bytes, "UTF-8");
logger.debug(value);
return value;
} catch (Exception e) {
logger.error("", e);
}
return "";
}
任意の問題がありますされています。このコードは、間違っているのですか?
PS: send()関数はうまく動作します。この場合に適切ではないかもしれない特定のエンコーディングを前提
デコード文字列を更新しました –
どのように関連しているのかわかりません。あなたのコードでも呼ばれていないし、私が言ったことは何も言及していない。コード内の識別可能な問題は、bdecoding(String対ByteBuffer)、エンディアン変換、およびビット操作です。 – the8472