Java:Nio的MD5

Java:Nio的MD5
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();
            }
        }

你可能感兴趣的:(Java:Nio的MD5)