Advertisement
Guest User

ifs 0.3

a guest
Jan 11th, 2013
6,835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. package ifs;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.net.URLConnection;
  14. import java.util.Random;
  15.  
  16. /**
  17.  * ICQ File Scanner v0.3
  18.  *
  19.  */
  20. public class IFS {
  21.  
  22.     private static final int THREADS = 50;
  23.  
  24.     private static class MyRunnable implements Runnable {
  25.  
  26.         private static final String CHARS = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
  27.         private static final int ID_LENGTH = 6;
  28.         private static final String STR_MESSAGE = "(%d) %s";
  29.         private static final String STR_SUCCESS = "(%d) %s %d/%d=%f %s %d %s";
  30.         private static final String FILE_NAME = "ifs%d.txt";
  31.         private static final String URL = "http://files.icq.net/files/get?fileId=%s";
  32.         private static final int MAX_SIZE = 20 * 1024 * 1024;
  33.  
  34.         private final Random random = new Random();    
  35.         private final int mThreadId;
  36.         private int mSuccess = 0;
  37.         private int mTotal = 0;
  38.  
  39.         public MyRunnable(int id) {
  40.             mThreadId = id;
  41.         }
  42.  
  43.         @Override
  44.         public void run() {
  45.             final String path = "out";
  46.             File f = new File(path);
  47.             f.mkdirs();        
  48.             final String fileName = String.format(FILE_NAME, mThreadId);
  49.             while (true) {
  50.                 mTotal++;
  51.                 String s = generateString(random, CHARS, ID_LENGTH);
  52.                 final String url = String.format(URL, s);
  53.                 try {
  54.                     URL u = new URL(url);
  55.  
  56.                     URLConnection uc = u.openConnection();
  57.                     uc.connect();
  58.                     uc.getContent();
  59.                     String contentType = uc.getContentType();
  60.                     int contentLength = uc.getContentLength();
  61.                     String status = "done    ";
  62.                     if(contentLength < MAX_SIZE) {
  63.                         if ("image/jpeg".equals(contentType)){                         
  64.                             File file = new File(path, s + ".jpg");
  65.                             saveBinaryFile(uc, contentLength, u, file);
  66.                         }else if("image/tiff".equals(contentType)){
  67.                             File file = new File(path, s + ".tiff");
  68.                             saveBinaryFile(uc, contentLength, u, file);
  69.                         }else if("image/x-ms-bmp".equals(contentType)){
  70.                             File file = new File(path, s + ".bmp");
  71.                             saveBinaryFile(uc, contentLength, u, file);
  72.                         }else if("image/png".equals(contentType)){
  73.                             File file = new File(path, s + ".png");
  74.                             saveBinaryFile(uc, contentLength, u, file);
  75.                         }else if("image/gif".equals(contentType)){
  76.                             File file = new File(path, s + ".gif");
  77.                             saveBinaryFile(uc, contentLength, u, file);                        
  78.                         }else{
  79.                             status = "unknown";
  80.                         }
  81.                     }else{
  82.                         status = "too big";
  83.                     }
  84.                     mSuccess++;
  85.                     final double p = mSuccess / mTotal;
  86.                     final String out = String.format(STR_SUCCESS,
  87.                             mThreadId, status, mSuccess, mTotal, p, contentType,
  88.                             contentLength, url);
  89.                     System.out.println(out);
  90.                     writeToFile(fileName, out);
  91.                 } catch (MalformedURLException e) {
  92.                     System.out.println(String.format(STR_MESSAGE, mThreadId,
  93.                             e.getMessage()));
  94.                 } catch (FileNotFoundException e) {
  95.  
  96.                 } catch (IOException e) {
  97.                     System.out.println(String.format(STR_MESSAGE, mThreadId,
  98.                             e.getMessage()));
  99.                 }
  100.             }
  101.         }
  102.  
  103.         private void saveBinaryFile(URLConnection uc, int contentLength, URL u, File fileName) throws IOException {
  104.             InputStream raw = uc.getInputStream();
  105.             InputStream in = new BufferedInputStream(raw);
  106.             byte[] data = new byte[contentLength];
  107.             int bytesRead = 0;
  108.             int offset = 0;
  109.             while (offset < contentLength) {
  110.                 bytesRead = in.read(data, offset, data.length - offset);
  111.                 if (bytesRead == -1)
  112.                     break;
  113.                 offset += bytesRead;
  114.             }
  115.             in.close();
  116.  
  117.             if (offset != contentLength) {
  118.                 throw new IOException("Only read " + offset
  119.                         + " bytes; Expected " + contentLength + " bytes");
  120.             }
  121.  
  122.             FileOutputStream out = new FileOutputStream(fileName);
  123.             out.write(data);
  124.             out.flush();
  125.             out.close();
  126.         }
  127.  
  128.         private void writeToFile(final String fileName, final String out) {
  129.             FileWriter fWriter = null;
  130.             BufferedWriter writer = null;
  131.             try {
  132.                 fWriter = new FileWriter(fileName, true);
  133.                 writer = new BufferedWriter(fWriter);
  134.                 writer.append(out);
  135.                 writer.newLine();
  136.             } catch (IOException e) {
  137.                 System.out.println(String.format(STR_MESSAGE, mThreadId,
  138.                         e.getMessage()));
  139.             } finally {
  140.                 try {
  141.                     if (writer != null)
  142.                         writer.close();
  143.                 } catch (IOException e) {
  144.                 }
  145.             }
  146.         }
  147.  
  148.     }
  149.  
  150.     /**
  151.      * @param args
  152.      */
  153.     public static void main(String[] args) {
  154.         System.out.println("ICQ Files Scanner 0.3");
  155.         for (int i = 0; i < THREADS; i++) {
  156.             new Thread(new MyRunnable(i)).start();
  157.         }
  158.     }
  159.  
  160.     public static String generateString(Random rng, String characters,
  161.             int length) {
  162.         char[] text = new char[length];
  163.         for (int i = 0; i < length; i++) {
  164.             text[i] = characters.charAt(rng.nextInt(characters.length()));
  165.         }
  166.         return new String(text);
  167.     }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement