बढ़ाने के लिए मैं फ़ाइलों पर चेकसम गणना करने के लिए निम्नलिखित समारोह का उपयोग कर रहा जरूरत:जावा: चेकसम गणना के प्रदर्शन
public static void generateChecksums(String strInputFile, String strCSVFile) {
ArrayList<String[]> outputList = new ArrayList<String[]>();
try {
MessageDigest m = MessageDigest.getInstance("MD5");
File aFile = new File(strInputFile);
InputStream is = new FileInputStream(aFile);
System.out.println(Calendar.getInstance().getTime().toString() +
" Processing Checksum: " + strInputFile);
double dLength = aFile.length();
try {
is = new DigestInputStream(is, m);
// read stream to EOF as normal...
int nTmp;
double dCount = 0;
String returned_content="";
while ((nTmp = is.read()) != -1) {
dCount++;
if (dCount % 600000000 == 0) {
System.out.println(". ");
} else if (dCount % 20000000 == 0) {
System.out.print(". ");
}
}
System.out.println();
} finally {
is.close();
}
byte[] digest = m.digest();
m.reset();
BigInteger bigInt = new BigInteger(1,digest);
String hashtext = bigInt.toString(16);
// Now we need to zero pad it if you actually/want the full 32 chars.
while(hashtext.length() < 32){
hashtext = "0" + hashtext;
}
String[] arrayTmp = new String[2];
arrayTmp[0] = aFile.getName();
arrayTmp[1] = hashtext;
outputList.add(arrayTmp);
System.out.println("Hash Code: " + hashtext);
UtilityFunctions.createCSV(outputList, strCSVFile, true);
} catch (NoSuchAlgorithmException nsae) {
System.out.println(nsae.getMessage());
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
समस्या यह है कि पाश फ़ाइल में पढ़ने के लिए वास्तव में धीमी है:
while ((nTmp = is.read()) != -1) {
dCount++;
if (dCount % 600000000 == 0) {
System.out.println(". ");
} else if (dCount % 20000000 == 0) {
System.out.print(". ");
}
}
एक 3 जीबी फ़ाइल जो एक स्थान से दूसरे स्थान पर प्रतिलिपि बनाने के लिए एक मिनट से भी कम समय लेती है, गणना करने के लिए एक घंटे लगती है। क्या ऐसा कुछ है जो मैं इसे तेज करने के लिए कर सकता हूं या क्या मुझे शेल कमांड का उपयोग करने जैसी अलग दिशा में जाने की कोशिश करनी चाहिए?
अद्यतन: धन्यवाद सनकी के सुझाव मैं यह जो हास्यास्पद तेजी से होता है करने के लिए कोड बदल शाफ़्ट (मैं 2048x तेजी से लगता है कि होगा ...): एक बफर
byte[] buff = new byte[2048];
while ((nTmp = is.read(buff)) != -1) {
dCount += 2048;
if (dCount % 614400000 == 0) {
System.out.println(". ");
} else if (dCount % 20480000 == 0) {
System.out.print(". ");
}
}
विचार बाहर मानक के प्रगति को बताता है खोजने की कोशिश करें। यह कमांड लाइन ftp क्लाइंट के "हैश" व्यवहार को अनुकरण करने का मेरा तरीका था। Dcount% 60000000 एक प्रिंट बनाम एक प्रिंट बनाम करता है। – opike