@Test public void testGetPeers() throws IOException { byte[] bytes = Files.toByteArray(new File(getClass().getResource("/getPeers.bin").getFile())); Map<String,Object> map = Utils.deBencode(bytes); if (Objects.nonNull(map.get("q")) && map.get("q").equals("get_peers")) { Map<String, String> subMap = (Map<String, String>) map.get("a"); String infoHash = subMap.get("info_hash"); assertEquals(20, infoHash.getBytes(Bencode.DEFAULT_CHARSET).length); } }
public static Map<String, Object> deBencode(byte[] data) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(data); BencodeInputStream bencode = new BencodeInputStream(in); Type type = bencode.nextType(); try { Map<String, Object> dict = bencode.readDictionary(); return dict; } catch (Exception e) { return new HashMap<>(); } }
Utils { public static Map<String, Object> deBencode(byte[] data) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(data); BencodeInputStream bencode = new BencodeInputStream(in); Type type = bencode.nextType(); try { Map<String, Object> dict = bencode.readDictionary(); return dict; } catch (Exception e) { return new HashMap<>(); } } }
Utils { public static Map<String, Object> deBencode(byte[] data) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(data); BencodeInputStream bencode = new BencodeInputStream(in); Type type = bencode.nextType(); try { Map<String, Object> dict = bencode.readDictionary(); return dict; } catch (Exception e) { return new HashMap<>(); } }  }
Utils { public static Map<String, Object> deBencode(byte[] data) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(data); BencodeInputStream bencode = new BencodeInputStream(in); Type type = bencode.nextType(); try { Map<String, Object> dict = bencode.readDictionary(); return dict; } catch (Exception e) { return new HashMap<>(); } }  static String getNeighbour(String s1, String s2); static String randomId(); static String getRandomString(int size); static byte[] enBencode(Map<String, Object> map); static Map<String, Object> deBencode(byte[] data); static List<Node> decodeNodes(String encodeNodes); static byte[] getByteArray(byte[] bytes, int start, int end); static int getPort(byte[] bytes); }
Utils { public static Map<String, Object> deBencode(byte[] data) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(data); BencodeInputStream bencode = new BencodeInputStream(in); Type type = bencode.nextType(); try { Map<String, Object> dict = bencode.readDictionary(); return dict; } catch (Exception e) { return new HashMap<>(); } }  static String getNeighbour(String s1, String s2); static String randomId(); static String getRandomString(int size); static byte[] enBencode(Map<String, Object> map); static Map<String, Object> deBencode(byte[] data); static List<Node> decodeNodes(String encodeNodes); static byte[] getByteArray(byte[] bytes, int start, int end); static int getPort(byte[] bytes); static List<Node> fixedNodes; }