package com.hellocode.service;

import java.io.File;
import java.util.ArrayList;

import com.hellocode.main.PodCast;
import com.hellocode.util.FileUtil;
import com.hellocode.util.NetWorkingUtil;

public class DownLoad {
	public volatile static Integer job_count = 0;
	private String folder_name;
	public ArrayList<Thread> threads = new ArrayList<Thread>();

	// not contain file name.
	private void getFile(String url) {
		// TODO net.url-file-save to path

		FileUtil.createDir(RunTime.CONFIG.disk_main + File.separator
				+ folder_name);
		String name = this.getFileName(url);// copy
		String abs_name = RunTime.CONFIG.disk_main + File.separator
				+ this.folder_name + File.separator + name;

		System.out.println("$$$$$$:   " + abs_name);
		if (new File(abs_name).exists()) {
			// skip if had downloaded.
			return;
		}

		NetWorkingUtil.getHttpFile(url, abs_name);

	}

	// spit url 2 name
	private String getFileName(String url) {
		int i = url.lastIndexOf('/');
		String name = url.substring(i + 1);
		System.out.println("extract file name :== " + name);
		return name;
	}

	private synchronized void add() {
		synchronized (job_count) {
			job_count++;
		}
		System.out.println("job_count..." + job_count);
	}

	private synchronized void sub() {
		synchronized (job_count) {
			job_count--;
		}
		System.out.println("job_count..." + job_count);
	}

	/**
	 * single thread
	 * 
	 * @param fileList
	 * @param Path
	 */
	public void Process(final ArrayList<String> urlList, String ipath) {
		if (urlList.isEmpty()) {
			return;
		}
		this.folder_name = ipath;
		if (!this.checkDir()) {
			return;
		}
		final Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				String test = job_count.toString();
				DownLoad.this.add();
				for (String url : urlList) {
					DownLoad.this.getFile(url);
					System.out.println(test + "downloading..." + url);
					PodCast.main.lb_info.setText(":" + url);
				}
				DownLoad.this.sub();
				PodCast.main.lb_info.setText(job_count + "߳");
				if (job_count == 0) {
					PodCast.main.lb_info.setText("ȫ");
				}
				
			}
		});
		thread.setName(job_count.toString());
		thread.start();
	}

	// check dir exist, or create one
	private boolean checkDir() {
		// TODO
		return true;
	}

}

@@ -42,20 +42,252 @@
 java.io.
-File
+BufferedInputStream;%0D%0Aimport java.io.BufferedOutputStream;%0D%0Aimport java.io.File;%0D%0Aimport java.io.FileOutputStream;%0D%0Aimport java.io.IOException;%0D%0Aimport java.net.MalformedURLException;%0D%0Aimport java.net.URL;%0D%0Aimport java.net.URLConnection
 ;%0D%0Aimpor
@@ -541,16 +541,23 @@
 %09public 
+static 
 ArrayLis
@@ -1088,16 +1088,32 @@
 ;%0D%0A%09%09%7D%0D%0A
+%09%09//down load it
 %0D%0A%09%09NetW
@@ -1127,35 +1127,167 @@
 til.
-getHttpFile(url, abs_name);
+download(url, abs_name);%0D%0A%09%09%0D%0A%09%09// check the file size%0D%0A%09%09File file = new File(abs_name);%0D%0A%09%09if (file.exists() && file.length() == 0) %7B%0D%0A%09%09%09file.delete();%0D%0A%09%09%7D
 %0D%0A%0D%0A
@@ -2344,24 +2344,78 @@
 url);%0D%0A%09%09%09%09%09
+PodCast.main.lb_down_load.setText(%22:%22 + url);%0D%0A%09%09%09%09%09//
 PodCast.main
@@ -2489,36 +2489,41 @@
 PodCast.main.lb_
-info
+down_load
 .setText(job_cou
@@ -2515,16 +2515,20 @@
 setText(
+%22%22+ 
 job_coun
@@ -2534,24 +2534,30 @@
 nt + %22%DF%B3%22);%0D%0A
+%09%09%09%09%0D%0A
 %09%09%09%09if (job_
@@ -2572,24 +2572,72 @@
  0) %7B%0D%0A%09%09%09%09%09
+PodCast.main.lb_down_load.setText(%22%C8%AB%22);%0D%0A%09%09%09%09%09//
 PodCast.main
@@ -2667,20 +2667,16 @@
 %0A%09%09%09%09%7D%0D%0A
-%09%09%09%09
 %0D%0A%09%09%09%7D%0D%0A
@@ -2738,24 +2738,53 @@
 d.start();%0D%0A
+%09%09this.threads.add(thread);%0D%0A
 %09%7D%0D%0A%0D%0A%09// ch

use Socket down load, not HTTP NetworkUtil.java