逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::
from:  http://www.iteye.com/topic/1127319 

MD5 nio的简单版,看你们老是怀疑java慢 

C++ MD5工具验证结果: 

File: K:\Games\World of Warcraft\Data\common.MPQ 
Size: 2226587191 bytes 
Modified: 2008年11月19日 星期三, 12:57:24 
MD5: CD9F9C5523F3BA3866B81CCC74ED6476 


java运行结果,毫秒 
耗时:12672,cd9f9c5523f3ba3866b81ccc74ed6476  
                String hashType = "MD5";
        FileInputStream fStream = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance(hashType);
            fStream = new FileInputStream(
                    //"K:\\Games\\World of Warcraft\\Scan.dll");
                    
//"K:\\Games\\World of Warcraft\\Data\\patch-3.MPQ");
                    "K:\\Games\\World of Warcraft\\Data\\common.MPQ");
            FileChannel fChannel = fStream.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(8*1024);
            long s = System.currentTimeMillis();
            for ( int count = fChannel.read( buffer ); count !=-1 ; count = fChannel.read( buffer )
                ) {
                buffer.flip();
                md5.update( buffer );
                if( !buffer.hasRemaining() ){
                    //System.out.println("count:"+count);
                    buffer.clear();
                }
            }
            s = System.currentTimeMillis() - s;
            System.out.println( "耗时:"+s+","+getString( md5.digest() ) );
            
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if( fStream!=null )
                    fStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
posted on 2012-10-23 10:04 逛奔的蜗牛 阅读(549) 评论(1)  编辑 收藏 引用 所属分类: Java

评论

# re: Java:Nio的MD5 2014-07-19 11:55 Game Ark
我已阅读这篇文章,如果我可能只是我渴望建议你一些有趣的问题或提示。也许你可以写关于这篇文章的后续文章。  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理