Utils { public static byte[] enBencode(Map<String, Object> map) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); BencodeOutputStream bencoder = new BencodeOutputStream(out); bencoder.writeDictionary(map); return out.toByteArray(); }  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; }
@Test public void testEncode() throws IOException { Map<String, Object> map = new HashMap<>(); map.put("t", "aa"); map.put("y", "q"); map.put("q", "find_node"); Map<String, Object> subMap = new HashMap<>(); subMap.put("id", "abcdefghij0123456789"); subMap.put("target", "mnopqrstuvwxyz123456"); map.put("a", subMap); byte[] bytes = Utils.enBencode(map); assertEquals("d1:ad2:id20:abcdefghij01234567896:target20:mnopqrstuvwxyz123456e1:q9:find_node1:t2:aa1:y1:qe", new String(bytes)); }