Skip to content

Commit

Permalink
Fixed the case when some file might not give correct hash value
Browse files Browse the repository at this point in the history
resulting in aborting the process of other files' subtitle download
  • Loading branch information
atulgpt committed May 21, 2017
1 parent 9fbf8c5 commit 1479296
Showing 1 changed file with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.xml.bind.DatatypeConverter;
import java.util.ArrayList;
import javax.swing.JOptionPane;
Expand All @@ -29,51 +27,48 @@ public ArrayList<String> getHash(String[] videoFilesName, String algo) {
}

private ArrayList<String> calHash(String[] videoFilesName, String algo) {
byte[] videoBytes = null;
//byte[] videoBytesFinal = null;
byte[] videoBytesFinal1 = null;
byte[] videoBytes;
byte[] videoBytesFinal1;
ArrayList<String> hashValue = new ArrayList<>();
for (String videoFullName : videoFilesName) {
try {
File videoFile = new File(videoFullName);
RandomAccessFile videoFile1 = new RandomAccessFile(videoFullName, "r");
if (videoFile.exists() && !videoFile.isDirectory()) {
//videoBytes = Files.readAllBytes(videoFile.toPath());
byte[] videoBytesFirst1 = new byte[size];
byte[] videoBytesLast1 = new byte[size];
videoFile1.read(videoBytesFirst1);
System.out.println("offset: " + videoFile1.length());
System.out.println("offset: " + videoFile1.length());
videoFile1.seek((int) videoFile1.length() - size);
videoFile1.read(videoBytesLast1);
videoFile1.close();
//byte[] videoBytesFirst = Arrays.copyOfRange(videoBytes, 0, size);
//byte[] videoBytesLast = Arrays.copyOfRange(videoBytes, videoBytes.length - size, videoBytes.length);
//videoBytesFinal = new byte[2 * size];
videoBytesFinal1 = new byte[2 * size];
//System.arraycopy(videoBytesFirst, 0, videoBytesFinal, 0, videoBytesFirst.length);
//System.arraycopy(videoBytesLast, 0, videoBytesFinal, videoBytesFirst.length, videoBytesLast.length);
System.arraycopy(videoBytesFirst1, 0, videoBytesFinal1, 0, videoBytesFirst1.length);
System.arraycopy(videoBytesLast1, 0, videoBytesFinal1, videoBytesFirst1.length, videoBytesLast1.length);

if (videoBytesFinal1 != null) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(algo);
messageDigest.update(videoBytesFinal1);
byte[] digestedBytes = messageDigest.digest();
String hash = DatatypeConverter.printHexBinary(digestedBytes).toLowerCase();
hashValue.add(hash);
} catch (NoSuchAlgorithmException e) {
System.out.println("Error in hash: " + e);
hashValue.add("");
}
} else {
hashValue.add("");
}
} else {
JOptionPane.showMessageDialog(null, "File path is wrong!", "Error", JOptionPane.ERROR_MESSAGE);
hashValue.add("");
}
} catch (IOException e) {
System.out.println("Error in IO: " + e);
JOptionPane.showMessageDialog(null, "Error in reading file!", "Error", JOptionPane.ERROR_MESSAGE);
hashValue.add("");
}
if (videoBytesFinal1 != null) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(algo);
messageDigest.update(videoBytesFinal1);
byte[] digestedBytes = messageDigest.digest();
String hash = DatatypeConverter.printHexBinary(digestedBytes).toLowerCase();
hashValue.add(hash);
} catch (NoSuchAlgorithmException e) {
System.out.println("Error in hash: " + e);
}
} else {
}

}
return hashValue;
}
Expand Down

0 comments on commit 1479296

Please sign in to comment.