private static final byte[] encodeDictionary(HashMap<ByteBuffer, Object> dictionary) throws BencodingException
    {
        TreeMap<ByteBuffer, Object> sorted_dictionary = new TreeMap<ByteBuffer, Object>();
        sorted_dictionary.putAll(dictionary);
        byte[][] dictionary_parts = new byte[sorted_dictionary.keySet().size()*2][];
        int k = 0;
        for(Iterator<ByteBuffer> i = sorted_dictionary.keySet().iterator(); i.hasNext();)
        {
            ByteBuffer key = i.next();
            dictionary_parts[k++] = encodeString(key);
            dictionary_parts[k++] = encode(sorted_dictionary.get(key));
        }
        
        int total_length = 2;
        for(int i = 0; i < dictionary_parts.length; i++)
        {
            total_length += dictionary_parts[i].length;
        }
        byte[] bencoded_dictionary = new byte[total_length];
        bencoded_dictionary[0] = 'd';
        bencoded_dictionary[bencoded_dictionary.length-1] = 'e';
        int offset = 1;
        for(int i = 0; i < dictionary_parts.length; i++)
        {
            System.arraycopy(dictionary_parts[i],0,bencoded_dictionary,offset,dictionary_parts[i].length);
            offset += dictionary_parts[i].length;
        }
        return bencoded_dictionary;
    }