Example #1
0
  /**
   * 以阻塞的方式立即下载一个文件,该方法会覆盖已经存在的文件
   *
   * @param task
   * @return "ok" if download success (else return errmessage);
   */
  static String download(DownloadTask task) {
    if (!openedStatus && show) openStatus();

    URL url;
    HttpURLConnection conn;
    try {
      url = new URL(task.getOrigin());
      conn = (HttpURLConnection) url.openConnection();
      conn.setConnectTimeout(30000);
      conn.setReadTimeout(30000);
      conn.setRequestProperty(
          "User-Agent",
          "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/"
              + Math.random());
      if ("www.imgjav.com".equals(url.getHost())) { // 该网站需要带cookie请求
        conn.setRequestProperty(
            "User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36");
        // conn.setRequestProperty("Cookie", "__cfduid=d219ea333c7a9b5743b572697b631925a1446093229;
        // cf_clearance=6ae62d843f5d09acf393f9e4eb130d9366840c82-1446093303-28800");
        conn.setRequestProperty(
            "Cookie",
            "__cfduid=d6ee846b378bb7d5d173a05541f8a2b6a1446090548; cf_clearance=ea10e8db31f8b6ee51570b118dd89b7e616d7b62-1446099714-28800");
        conn.setRequestProperty("Host", "www.imgjav.com");
      }
      Path directory = Paths.get(task.getDest()).getParent();
      if (!Files.exists(directory)) Files.createDirectories(directory);
    } catch (Exception e) {
      e.printStackTrace();
      return e.getMessage();
    }

    try (InputStream is = conn.getInputStream();
        BufferedInputStream in = new BufferedInputStream(is);
        FileOutputStream fos = new FileOutputStream(task.getDest());
        OutputStream out = new BufferedOutputStream(fos); ) {
      int length = conn.getContentLength();
      if (length < 1) throw new IOException("length<1");
      byte[] binary = new byte[length];
      byte[] buff = new byte[65536];
      int len;
      int index = 0;
      while ((len = in.read(buff)) != -1) {
        System.arraycopy(buff, 0, binary, index, len);
        index += len;
        allLen += len; // allLen有线程安全的问题 ,可能会不正确。无需精确数据,所以不同步了
        task.setReceivePercent(String.format("%.2f", ((float) index / length) * 100) + "%");
      }
      out.write(binary);
    } catch (IOException e) {
      e.printStackTrace();
      return e.getMessage();
    }
    return "ok";
  }
Example #2
0
 public static void load(Collection<FileDesc> files, Path root, int blocSize, Pattern pattern)
     throws IOException {
   root = root.toAbsolutePath().normalize();
   Visitor visitor = new Visitor(root, blocSize, pattern);
   Files.walkFileTree(root, visitor);
   for (Future<FileDesc> future : visitor.futures()) {
     try {
       files.add(future.get());
     } catch (Exception e) {
       log.error("", e);
     }
   }
 }
Example #3
0
 private static void extractAndLoadNativeLib(String nativeLibName, Path target) {
   //		System.err.println("loading "+nativeLibName);
   final Path path = Paths.get(target.toString(), nativeLibName);
   if (!path.toFile().exists()) {
     try (InputStream is =
         CudaEngine.class
             .getClassLoader()
             .getResourceAsStream("/lib/" + nativeLibName)) { // TODO TK property for lib dir
       Files.copy(is, path);
     } catch (IOException e) {
       e.printStackTrace();
     } catch (NullPointerException e) { // TODO find a way to do it instead of eclipse
       final Path eclipsePath = FileSystems.getDefault().getPath("lib", nativeLibName);
       try {
         Files.copy(eclipsePath, path);
       } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
       }
     }
   }
   System.load(path.toString());
   //		System.load(nativeLibName);
 }
Example #4
0
 private static void extractAndLoadNativeLibs() throws IOException {
   Path target = Paths.get(ioTmpDir, "/tklib");
   if (!target.toFile().exists()) {
     Files.createDirectories(target);
   }
   final boolean windows = System.getProperty("os.name").equalsIgnoreCase("windows");
   String fileExtension = windows ? "dll" : "so";
   String prefix = windows ? "" : "lib";
   String libPattern = fileExtension.equals("dll") ? "-windows" : "-linux" + "-x86";
   if (System.getProperty("sun.arch.data.model").equals("64")) {
     libPattern += "_64";
   }
   libPattern += "." + fileExtension;
   //		System.err.println(libPattern);
   System.setProperty("java.library.path", target.toString());
   //		System.err.println(System.getProperty("java.library.path"));
   extractAndLoadNativeLib(prefix + "JCudaDriver" + libPattern, target);
   extractAndLoadNativeLib(prefix + "JCudaRuntime" + libPattern, target);
   extractAndLoadNativeLib(prefix + "JCurand" + libPattern, target);
 }
Example #5
0
 // void compileKernelsPtx()
 static void compileKernelsPtx() throws IOException {
   if (!new File(ioTmpDir, PHEROMONES_CU + ".ptx").exists()) { // TODO externalize
     try (InputStream is =
         CudaEngine.class.getResourceAsStream(
             "/turtlekit/cuda/kernels/" + PHEROMONES_CU + ".cu")) {
       final Path path = Paths.get(ioTmpDir, PHEROMONES_CU + ".cu");
       try {
         Files.copy(is, path);
       } catch (FileAlreadyExistsException e) {
       }
       System.err.println("--------------- Compiling ptx ----------------------");
       KernelLauncher.create(
           path.toString(),
           Kernel.DIFFUSION_TO_TMP.name(),
           false,
           "--use_fast_math",
           "--prec-div=false"); // ,"--gpu-architecture=sm_20");
     } catch (IOException e) {
       throw e;
     }
   }
 }