| 1 |
import java.io.File; |
|---|
| 2 |
import java.io.FileInputStream; |
|---|
| 3 |
import java.io.InputStream; |
|---|
| 4 |
import java.math.BigInteger; |
|---|
| 5 |
import java.security.MessageDigest; |
|---|
| 6 |
import java.security.NoSuchAlgorithmException; |
|---|
| 7 |
import java.util.Vector; |
|---|
| 8 |
|
|---|
| 9 |
public class OverhaulFile { |
|---|
| 10 |
|
|---|
| 11 |
private int numChunks; |
|---|
| 12 |
private int chunkSize; |
|---|
| 13 |
private int maxClients; |
|---|
| 14 |
private int lastChunk; |
|---|
| 15 |
private Vector<String> md5List; |
|---|
| 16 |
|
|---|
| 17 |
public OverhaulFile() { |
|---|
| 18 |
lastChunk = 0; |
|---|
| 19 |
md5List = new Vector<String>(); |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
public synchronized String getMd5ListStr() { |
|---|
| 23 |
|
|---|
| 24 |
String ret = ""; |
|---|
| 25 |
for(int i = 0; i < md5List.size(); i++) { |
|---|
| 26 |
if (i != 0) { |
|---|
| 27 |
ret += " "; |
|---|
| 28 |
} |
|---|
| 29 |
ret += md5List.get(i); |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
return ret; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
public synchronized void setMd5List(File target) { |
|---|
| 36 |
try { |
|---|
| 37 |
InputStream is = new FileInputStream(target.getAbsolutePath()); |
|---|
| 38 |
for (int i = 1; i <= numChunks; i++) { |
|---|
| 39 |
|
|---|
| 40 |
int startByte = (i - 1) * chunkSize; |
|---|
| 41 |
int endByte = Math.min(i * chunkSize, (int) target.length()); |
|---|
| 42 |
int bytesToRead = endByte - startByte; |
|---|
| 43 |
|
|---|
| 44 |
byte[] buf= new byte[bytesToRead]; |
|---|
| 45 |
int n = is.read(buf); |
|---|
| 46 |
if (n != bytesToRead) { |
|---|
| 47 |
|
|---|
| 48 |
System.out.println("(-----(MD5)-----) Supposed to read: " + bytesToRead + " bytes, read: " + n + "bytes"); |
|---|
| 49 |
System.out.println("i=" +i + "; startByte=" + startByte + "; endByte=" + endByte); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
String checkSum = generateCheckSum(buf); |
|---|
| 53 |
md5List.add(checkSum); |
|---|
| 54 |
} |
|---|
| 55 |
} catch (Exception e) { |
|---|
| 56 |
|
|---|
| 57 |
} |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
public synchronized int getLastChunk() { |
|---|
| 66 |
return lastChunk; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
public synchronized void setLastChunk(int lastChunk) { |
|---|
| 70 |
this.lastChunk = lastChunk; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
public static String generateCheckSum(byte data[]) { |
|---|
| 74 |
String hashword; |
|---|
| 75 |
MessageDigest md = null; |
|---|
| 76 |
|
|---|
| 77 |
try { |
|---|
| 78 |
md = MessageDigest.getInstance("MD5"); |
|---|
| 79 |
} catch (NoSuchAlgorithmException e) { |
|---|
| 80 |
e.printStackTrace(); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
md.update(data); |
|---|
| 84 |
BigInteger hash = new BigInteger(1, md.digest()); |
|---|
| 85 |
hashword = hash.toString(16); |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
if (hashword.length() < 32) { |
|---|
| 89 |
int zeros = 32 - hashword.length(); |
|---|
| 90 |
for (int i = 0; i < zeros; i++) { |
|---|
| 91 |
hashword = "0" + hashword; |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
return hashword; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
public synchronized int getChunkSize() { |
|---|
| 101 |
return chunkSize; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
public synchronized void setChunkSize(int chunkSize) { |
|---|
| 105 |
this.chunkSize = chunkSize; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
public int getNumChunks() { |
|---|
| 109 |
return numChunks; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
public void setNumChunks(int numChunks) { |
|---|
| 113 |
this.numChunks = numChunks; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
public int getMaxClients() { |
|---|
| 117 |
return maxClients; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
public void setMaxClients(int maxClients) { |
|---|
| 121 |
this.maxClients = maxClients; |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|