Skip to content

Commit

Permalink
格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaodj committed Mar 15, 2013
1 parent 82dbd00 commit d08ee3a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 47 deletions.
2 changes: 1 addition & 1 deletion java/demo/.classpath
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/> <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>
11 changes: 7 additions & 4 deletions java/demo/.settings/org.eclipse.jdt.core.prefs
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,9 @@
#Tue Apr 24 16:30:38 CST 2012 #Thu Mar 14 12:42:54 CST 2013
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.compliance=1.5 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5 org.eclipse.jdt.core.compiler.source=1.6
83 changes: 43 additions & 40 deletions java/demo/src/main/java/org/zhaodj/util/FileHasher.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,56 +7,59 @@
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;


public class FileHasher { public class FileHasher {


private static final char[] DIGITS = { private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
'0', '1', '2', '3', '4', '5', '6', '7', 'b', 'c', 'd', 'e', 'f' };
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
}; public static String hash(String algorithm, String filepath) throws NoSuchAlgorithmException,

IOException {
public static String hash(String algorithm,String filepath) throws NoSuchAlgorithmException, IOException{ MessageDigest md = MessageDigest.getInstance(algorithm);
MessageDigest md=MessageDigest.getInstance(algorithm); InputStream is = new FileInputStream(filepath);
InputStream is=new FileInputStream(filepath); try {
int numRead=0; int numRead = 0;
byte[] buffer=new byte[1024]; byte[] buffer = new byte[1024];
while((numRead=is.read(buffer))>0){ while ((numRead = is.read(buffer)) > 0) {
md.update(buffer, 0, numRead); md.update(buffer, 0, numRead);
}
} finally {
is.close();
} }
is.close();
return new String(encodeHex(md.digest())); return new String(encodeHex(md.digest()));
} }

public static char[] encodeHex(byte[] data) { public static char[] encodeHex(byte[] data) {


int l = data.length; int l = data.length;


char[] out = new char[l << 1]; char[] out = new char[l << 1];


// two characters form the hex value. // two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) { for (int i = 0, j = 0; i < l; i++) {
out[j++] = DIGITS[(0xF0 & data[i]) >>> 4 ]; out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
out[j++] = DIGITS[ 0x0F & data[i] ]; out[j++] = DIGITS[0x0F & data[i]];
} }


return out; return out;
} }


public static void test(String algorithm,String filepath,int times) throws NoSuchAlgorithmException, IOException{ public static void test(String algorithm, String filepath, int times)
long begin=System.nanoTime(); throws NoSuchAlgorithmException, IOException {
for(int i=0;i<times;i++){ long begin = System.nanoTime();
hash(algorithm,filepath); for (int i = 0; i < times; i++) {
hash(algorithm, filepath);
} }
System.out.println(algorithm+":"+(System.nanoTime()-begin)); System.out.println(algorithm + ":" + (System.nanoTime() - begin));
} }

public static void main(String[] args) throws NoSuchAlgorithmException, IOException{ public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
String file="/home/zhaodj/图片/照片/IMG_3643.JPG"; String file = "/home/zhaodj/图片/照片/IMG_3643.JPG";
System.out.println("file:"+file); System.out.println("file:" + file);
test("MD5",file,1000); test("MD5", file, 1000);
test("SHA1",file,1000); test("SHA1", file, 1000);
file="/home/zhaodj/图片/照片/ada.jpg"; file = "/home/zhaodj/图片/照片/ada.jpg";
System.out.println("file:"+file); System.out.println("file:" + file);
test("MD5",file,1000); test("MD5", file, 1000);
test("SHA1",file,1000); test("SHA1", file, 1000);
} }


} }
2 changes: 0 additions & 2 deletions java/demo/src/main/java/org/zhaodj/util/FileUtil.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;


Expand Down

0 comments on commit d08ee3a

Please sign in to comment.