bencode对象编码实现

阅读更多
承接上文,使用递归可以很简单的就实现了一个bencode编码函数
package com.norkts.torrents;

import org.junit.Assert;

import java.util.*;

/**
 * bencode文件编码
 * @author norkts
 * @date 2017-06-23
 * @version 1.0
 */
public class Bencoder {

    /**
     * 将对象使用bencode格式编码
     * @param target
     * @return
     */
    public static String encode(Object target){

        StringBuilder sb = new StringBuilder();

        if(target instanceof Map){

            //字典类型
            sb.append("d");

            for(Map.Entry entry : ((Map)target).entrySet()){
                String key = entry.getKey();
                Object value = entry.getValue();
                sb.append(encode(key));
                sb.append(encode(value));
            }

            sb.append("e");
        }else if(target instanceof List){
            //列表类型处理
            sb.append("l");
            for(Object item : (List)target){
                sb.append(encode(item));
            }
            sb.append("e");

        }else if(target instanceof String){
            //字符串类型处理
            sb.append(((String)target).length() + ":" + target);
        }else{
            //数字处理
            sb.append("i" + Integer.toString((Integer)target, 10) + "e");
        }


        return sb.toString();
    }

    public static void main(String[] argv){
        Object target = new LinkedHashMap();
        ((Map)target).put("a", "1");
        ((Map)target).put("b", 2);
        ((Map)target).put("c", -3);

        Assert.assertEquals("d1:a1:11:bi2e1:ci-3ee", encode(target));

        target = new LinkedHashMap();
        ((Map) target).put("key1", "val1");
        ((Map) target).put("key2", 100);

        List li = new ArrayList();
        li.add("item1");
        li.add(-100);
        li.add(((ArrayList) li).clone());
        li.add(((LinkedHashMap) target).clone());

        ((Map) target).put("key3", li);

        System.out.println(encode(target));
        Assert.assertEquals("d4:key14:val14:key2i100e4:key3l5:item1i-100el5:item1i-100eed4:key14:val14:key2i100eeee", encode(target));

        System.out.println(BencodeDecoder.decode(encode(target).getBytes()));
    }
}

 

                            
                        
                    
                    
                    

你可能感兴趣的:(torrents,bittorrent,dht,bencode)