Chunks


SUBMITTED BY: Guest

DATE: Nov. 6, 2013, 12:30 p.m.

FORMAT: Java

SIZE: 9.7 kB

HITS: 1005

  1. package Main;
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. import java.io.*;
  7. import java.security.*;
  8. import java.util.*;
  9. /**
  10. *
  11. * @author phihai
  12. */
  13. public class Chunks {
  14. public static long chunkSize = 512* 1024;
  15. public void split(File f) throws FileNotFoundException, IOException // Cắt chunks
  16. {
  17. BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
  18. long fileSize = f.length();
  19. int subfile;
  20. for (subfile = 0; subfile < fileSize / chunkSize; subfile++)
  21. {
  22. BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Share\\"+f.getName() + "." + subfile));
  23. for (int currentByte = 0; currentByte < chunkSize; currentByte++)
  24. {
  25. out.write(in.read());
  26. }
  27. out.close();
  28. }
  29. if (fileSize != chunkSize * (subfile - 1))
  30. {
  31. BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Share\\"+f.getName() + "." + subfile));
  32. int b;
  33. while ((b = in.read()) != -1)
  34. out.write(b);
  35. out.close();
  36. }
  37. in.close();
  38. Info_Chunk("Share\\"+f.getName());
  39. }
  40. public void join(String baseFilename) throws IOException // ghép chunks , basefilename là đường dẫn của tập tin
  41. {
  42. int numberParts = getNumberParts(baseFilename);
  43. BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(baseFilename));
  44. for (int part = 0; part < numberParts; part++)
  45. {
  46. BufferedInputStream in = new BufferedInputStream(new FileInputStream(baseFilename + "." + part));
  47. int b;
  48. while ( (b = in.read()) != -1 )
  49. out.write(b);
  50. in.close();
  51. }
  52. out.close();
  53. }
  54. private static int getNumberParts(String baseFilename) throws IOException // đến số lượng chunks
  55. {
  56. File directory = new File(baseFilename).getAbsoluteFile().getParentFile();
  57. final String justFilename = new File(baseFilename).getName();
  58. String[] matchingFiles = directory.list(new FilenameFilter()
  59. {
  60. public boolean accept(File dir, String name)
  61. {
  62. return name.startsWith(justFilename) && name.substring(justFilename.length()).matches("^\\.\\d+$");
  63. }
  64. });
  65. return matchingFiles.length;
  66. }
  67. public String[] getFileName(String dirname) throws IOException // lấy tên các tập tin có thể download từ file (filename.chunk)
  68. {
  69. File directory = new File(dirname);
  70. String[] matchingFiles = directory.list(new FilenameFilter()
  71. {
  72. public boolean accept(File dir, String name)
  73. {
  74. return name.endsWith(".chunk");
  75. }
  76. });
  77. return matchingFiles;
  78. }
  79. public void Info_Chunk(String baseFilename) throws IOException // xuất thông tin file + mã hash của tập tin vào file filenamme.chunks
  80. {
  81. String str;
  82. int numberParts = getNumberParts(baseFilename);
  83. File file = new File(baseFilename + "."+"chunk");
  84. BufferedWriter out = new BufferedWriter(new FileWriter(file));
  85. for (int part = 0; part < numberParts; part++)
  86. {
  87. str = part + " " + hash(baseFilename + "." + part) + "\r\n";
  88. out.write(str);
  89. }
  90. out.close();
  91. System.out.print(file.getName() +" đã được tạo \n");
  92. }
  93. // public String List_Chunk(String baseFilename) throws IOException
  94. // {
  95. // File directory = new File(baseFilename).getAbsoluteFile().getParentFile();
  96. // final String justFilename = new File(baseFilename).getName();
  97. // String[] matchingFiles = directory.list(new FilenameFilter()
  98. // {
  99. // public boolean accept(File dir, String name)
  100. // {
  101. // return name.startsWith(justFilename) && name.substring(justFilename.length()).matches("^\\.\\d+$");
  102. // }
  103. // });
  104. //
  105. // List<String> list = getHash(justFilename+".chunk");
  106. // String str = "";
  107. //
  108. // for (int part = 0; part < matchingFiles.length; part++)
  109. // {
  110. // if(list.contains(hash(matchingFiles[part])))
  111. // {
  112. // str += matchingFiles[part] + "\r\n";
  113. // }
  114. // }
  115. // return str;
  116. // }
  117. public List<String> List_Chunk(String baseFilename) throws IOException // Lay danh sach Hash cua cac file hien co trên client thực thi
  118. {
  119. File directory = new File(baseFilename).getAbsoluteFile().getParentFile();
  120. final String justFilename = new File(baseFilename).getName();
  121. String[] matchingFiles = directory.list(new FilenameFilter()
  122. {
  123. public boolean accept(File dir, String name)
  124. {
  125. return name.startsWith(justFilename) && name.substring(justFilename.length()).matches("^\\.\\d+$");
  126. }
  127. });
  128. List<String> list = new ArrayList<String>();
  129. for (int part = 0; part < matchingFiles.length; part++)
  130. {
  131. list.add(hash("Share\\"+matchingFiles[part]));
  132. }
  133. return list;
  134. }
  135. public String Check_Chunks(String hashChunks) // nhận thông tin mã hash từ client thực thi, client nhận hash sẽ tiến hành kiểm tra tồn tại của file thông qua mã hash này
  136. {
  137. String temp = "";
  138. File folder = new File("Share\\");
  139. File[] listOfFiles = folder.listFiles();
  140. for(int i=0;i<listOfFiles.length;i++)
  141. {
  142. if(listOfFiles[i].isFile())
  143. {
  144. if(hash(listOfFiles[i].getAbsolutePath()).equals(hashChunks))
  145. {
  146. temp = listOfFiles[i].getName();
  147. break;
  148. }
  149. }
  150. }
  151. return temp;
  152. }
  153. // public List<String> Find_Chunks(String baseFilename) // Tim cac chunks chua co
  154. // {
  155. // List<String> listFile = new ArrayList<String>();
  156. // final String justFilename = new File(baseFilename).getName();
  157. // try
  158. // {
  159. // String str ;
  160. // List<String> list = List_Chunk(baseFilename);
  161. //
  162. // BufferedReader br = new BufferedReader(new FileReader(justFilename+".chunk"));
  163. // System.out.print("Cac Chunk còn thiếu:\n");
  164. // while ((str = br.readLine()) != null)
  165. // {
  166. // String []array = str.split(" ");
  167. // if(!list.contains(array[1]))
  168. // {
  169. // listFile.add(array[1]);
  170. // System.out.print(array[0]+"\n");
  171. // }
  172. // }
  173. // }
  174. // catch (Exception e)
  175. // {
  176. // e.printStackTrace();
  177. // }
  178. // return listFile;
  179. // }
  180. // private static List<String> getHash(String Filename)
  181. // {
  182. // List<String> list = new ArrayList<String>();
  183. // try
  184. // {
  185. // String str ;
  186. //
  187. // BufferedReader br = new BufferedReader(new FileReader(Filename));
  188. // while ((str = br.readLine()) != null)
  189. // {
  190. // String []array = str.split(" ");
  191. // list.add(array[1]);
  192. // }
  193. // }
  194. // catch (Exception e)
  195. // {
  196. // e.printStackTrace();
  197. // }
  198. // return list;
  199. // }
  200. public static String hash(String filename) // filename đường dẫn của file, lấy thông tin của file hash ra mã SHA-1
  201. {
  202. String result = "";
  203. try
  204. {
  205. BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
  206. MessageDigest md = MessageDigest.getInstance("SHA-1");
  207. byte [] buffer = new byte[1024];
  208. int sizeRead;
  209. while ((sizeRead = in.read(buffer)) != -1)
  210. {
  211. md.update(buffer, 0, sizeRead);
  212. }
  213. in.close();
  214. byte [] hash = md.digest();
  215. for (int i=0; i < hash.length; i++) {
  216. result += Integer.toString( ( hash[i] & 0xff ) + 0x100, 16).substring( 1 );
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. ex.printStackTrace();
  222. }
  223. return result;
  224. }
  225. }

comments powered by Disqus