Advertisement
hugoclo

Untitled

Apr 25th, 2020
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package prezauto;
  7.  
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.security.MessageDigest;
  16. import java.security.NoSuchAlgorithmException;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.Map.Entry;
  20. import java.util.SortedMap;
  21. import java.util.TreeMap;
  22.  
  23. /**
  24.  *
  25.  * @ merci à l'auteur d'origine
  26.  */
  27. public class Torrent {
  28.         private static void encodeObject(Object o, OutputStream out) throws IOException {
  29.         if (o instanceof String)
  30.             encodeString((String)o, out);
  31.         else if (o instanceof Map)
  32.             encodeMap((Map)o, out);
  33.         else if (o instanceof byte[])
  34.             encodeBytes((byte[])o, out);
  35.         else if (o instanceof Number)
  36.             encodeLong(((Number) o).longValue(), out);
  37.         else
  38.             throw new Error("Unencodable type");
  39.     }
  40.     private static void encodeLong(long value, OutputStream out) throws IOException {
  41.         out.write('i');
  42.         out.write(Long.toString(value).getBytes("US-ASCII"));
  43.         out.write('e');
  44.     }
  45.     private static void encodeBytes(byte[] bytes, OutputStream out) throws IOException {
  46.         out.write(Integer.toString(bytes.length).getBytes("US-ASCII"));
  47.         out.write(':');
  48.         out.write(bytes);
  49.     }
  50.     private static void encodeString(String str, OutputStream out) throws IOException {
  51.         encodeBytes(str.getBytes("UTF-8"), out);
  52.     }
  53.     private static void encodeMap(Map<String,Object> map, OutputStream out) throws IOException{
  54.         // Sort the map. A generic encoder should sort by key bytes
  55.         SortedMap<String,Object> sortedMap = new TreeMap<>(map);
  56.         out.write('d');
  57.         for (Entry<String, Object> e : sortedMap.entrySet()) {
  58.             encodeString(e.getKey(), out);
  59.             encodeObject(e.getValue(), out);
  60.         }
  61.         out.write('e');
  62.     }
  63.     private static byte[] hashPieces(File file, int pieceLength) throws IOException {
  64.         MessageDigest sha1;
  65.         try {
  66.             sha1 = MessageDigest.getInstance("SHA");
  67.         } catch (NoSuchAlgorithmException e) {
  68.             throw new Error("SHA1 not supported");
  69.         }
  70.         InputStream in;
  71.             in = new FileInputStream(file);
  72.         ByteArrayOutputStream pieces = new ByteArrayOutputStream();
  73.         byte[] bytes = new byte[pieceLength];
  74.         int pieceByteCount  = 0, readCount = in.read(bytes, 0, pieceLength);
  75.         while (readCount != -1) {
  76.             pieceByteCount += readCount;
  77.             sha1.update(bytes, 0, readCount);
  78.             if (pieceByteCount == pieceLength) {
  79.                 pieceByteCount = 0;
  80.                 pieces.write(sha1.digest());
  81.             }
  82.             readCount = in.read(bytes, 0, pieceLength-pieceByteCount);
  83.         }
  84.         in.close();
  85.         if (pieceByteCount > 0)
  86.             pieces.write(sha1.digest());
  87.         return pieces.toByteArray();
  88.     }
  89.     public void createTorrent(File file, File sharedFile, String announceURL) throws IOException {
  90.         final int pieceLength = 512*1024;
  91.         Map<String,Object> info = new HashMap<>();
  92.         info.put("name", sharedFile.getName());//
  93.         info.put("length", sharedFile.length());//
  94.         info.put("piece length", pieceLength);//
  95.         info.put("pieces", hashPieces(sharedFile, pieceLength));
  96.         info.put("private", 1);
  97.         Map<String,Object> metainfo = new HashMap<>();
  98.         metainfo.put("announce", announceURL);
  99.         metainfo.put("created by", "Prezauto");
  100.         metainfo.put("creation date", new java.util.Date().getTime());
  101.         metainfo.put("info", info);
  102.         OutputStream out = new FileOutputStream(file);
  103.         encodeMap(metainfo, out);
  104.         out.close();
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement