|
此文章由 wil 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 wil 所有!转贴必须注明作者、出处和本声明,并保持内容完整
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.codec.digest.DigestUtils;
import com.twmacinta.util.MD5;
public class md5 {
public static byte[] createChecksum(String filename, int bufferSize) throws Exception {
InputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[bufferSize];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
static final String HEXES = "0123456789ABCDEF";
public static String getHex(byte[] raw) {
if (raw == null) {
return null;
}
final StringBuilder hex = new StringBuilder(2 * raw.length);
for (final byte b : raw) {
hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
}
return hex.toString();
}
public static void main(String args[]) {
try {
String inputFilePath = "E:\\Games\\Street.Fighter.X.Tekken.full.rip.rar";
System.out.println("file size: " + new File(inputFilePath).length());
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
System.out.println();
System.out.println("---- http://www.rgagnon.com/javadetails/java-0416.html: bufferSize = 1024");
System.out.println(format.format(new Date()) + " : Starting process file " + inputFilePath);
String checksum1 = getHex(createChecksum(inputFilePath, 1024));
System.out.println(format.format(new Date()) + " : The MD5 hash is " + checksum1);
System.out.println();
System.out.println("---- http://www.rgagnon.com/javadetails/java-0416.html: bufferSize = 2048");
System.out.println(format.format(new Date()) + " : Starting process file " + inputFilePath);
String checksum11 = getHex(createChecksum(inputFilePath, 2048));
System.out.println(format.format(new Date()) + " : The MD5 hash is " + checksum11);
System.out.println();
System.out.println("---- http://www.rgagnon.com/javadetails/java-0416.html: bufferSize = 4096");
System.out.println(format.format(new Date()) + " : Starting process file " + inputFilePath);
String checksum111 = getHex(createChecksum(inputFilePath, 4096));
System.out.println(format.format(new Date()) + " : The MD5 hash is " + checksum111);
System.out.println();
System.out.println("---- http://www.rgagnon.com/javadetails/java-0416.html: bufferSize = 8192");
System.out.println(format.format(new Date()) + " : Starting process file " + inputFilePath);
String checksum1111 = getHex(createChecksum(inputFilePath, 8192));
System.out.println(format.format(new Date()) + " : The MD5 hash is " + checksum1111);
System.out.println();
System.out.println("---- http://commons.apache.org/codec/ ... st/DigestUtils.html");
System.out.println(format.format(new Date()) + " : Starting process file " + inputFilePath);
String checksum2 = DigestUtils.md5Hex(new FileInputStream(inputFilePath));
System.out.println(format.format(new Date()) + " : The MD5 hash is " + checksum2);
System.out.println();
System.out.println("---- http://www.twmacinta.com/myjava/fast_md5.php: No Native!");
System.out.println(format.format(new Date()) + " : Starting process file " + inputFilePath);
String checksum3 = MD5.asHex(MD5.getHash(new File(inputFilePath)));
System.out.println(format.format(new Date()) + " : The MD5 hash is " + checksum3);
} catch (Exception e) {
e.printStackTrace();
}
}
}
===========================================
file size: 4447788589
---- http://www.rgagnon.com/javadetails/java-0416.html: bufferSize = 1024
27/01/2013 05:48:37 AM : Starting process file E:\Games\Street.Fighter.X.Tekken.full.rip.rar
27/01/2013 05:48:56 AM : The MD5 hash is 648BE3603648E824AA3125DF9D1BA21A
---- http://www.rgagnon.com/javadetails/java-0416.html: bufferSize = 2048
27/01/2013 05:48:56 AM : Starting process file E:\Games\Street.Fighter.X.Tekken.full.rip.rar
27/01/2013 05:49:13 AM : The MD5 hash is 648BE3603648E824AA3125DF9D1BA21A
---- http://www.rgagnon.com/javadetails/java-0416.html: bufferSize = 4096
27/01/2013 05:49:13 AM : Starting process file E:\Games\Street.Fighter.X.Tekken.full.rip.rar
27/01/2013 05:49:28 AM : The MD5 hash is 648BE3603648E824AA3125DF9D1BA21A
---- http://www.rgagnon.com/javadetails/java-0416.html: bufferSize = 8192
27/01/2013 05:49:28 AM : Starting process file E:\Games\Street.Fighter.X.Tekken.full.rip.rar
27/01/2013 05:49:42 AM : The MD5 hash is 648BE3603648E824AA3125DF9D1BA21A
---- http://commons.apache.org/codec/ ... st/DigestUtils.html
27/01/2013 05:49:42 AM : Starting process file E:\Games\Street.Fighter.X.Tekken.full.rip.rar
27/01/2013 05:50:01 AM : The MD5 hash is 648be3603648e824aa3125df9d1ba21a
---- http://www.twmacinta.com/myjava/fast_md5.php: No Native!
27/01/2013 05:50:01 AM : Starting process file E:\Games\Street.Fighter.X.Tekken.full.rip.rar
27/01/2013 05:50:17 AM : The MD5 hash is 648be3603648e824aa3125df9d1ba21a
===========================================
4个多g的rar文件,文件也在ssd上,分别测试了几种方法。。。。
我也是爱较这个真,大家在论坛上随便说说的其实不该较真。。比较两个语言的性能,这个话题太大,大到相差不多的语言之间的性能比较都没有什么现实意义。就拿这个md5的例子来说,谁知道C#内部怎么实现的,它也不开源,没准是调用c的实现,这个java也同样能(http://www.twmacinta.com/myjava/fast_md5.php,这个实现就是先试图找本地原生语言支持的,但我的最后一个测试没有用到,还是跑的java程序)。如果大家公平点都在各自虚拟机里跑,C#在只调.NET,java只调JVM,这个我可以认真的说,同为虚拟机JVM确实比.NET要成熟些,Java是很烂但JVM绝对是精品,至于为什么这么说这个有兴趣就自己搜搜吧,话题太大。我个人觉得语言层面C#要比Java高级,虚拟机方面还是没法比的。不管怎么说,让这个帖子回归娱乐贴吧。。。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
评分
-
查看全部评分
|