package com.qiniu.io;

import android.content.Context;
import android.net.Uri;
import com.qiniu.auth.Client;
import com.qiniu.auth.JSONObjectRet;
import com.qiniu.conf.Conf;
import com.qiniu.utils.InputStreamAt;
import com.qiniu.utils.MultipartEntity;
import org.json.JSONObject;

import java.io.FileNotFoundException;
import java.util.Map;

public class IO {

	public static String UNDEFINED_KEY = null;

	private static Client mClient;

	public static void setClient(Client client) {
		mClient = client;
	}

	private static Client defaultClient() {
		if (mClient == null) {
			mClient = Client.defaultClient();
		}
		return mClient;
	}

	/**
	 * 上传二进制
	 *
	 * @param uptoken 用于上传的验证信息
	 * @param key     键值名, UNDEFINED_KEY 表示自动生成key
	 * @param isa     二进制数据
	 * @param extra   上传参数
	 * @param ret     回调函数
	 */
	public static void put(String uptoken, String key, InputStreamAt isa, PutExtra extra, JSONObjectRet ret) {

		MultipartEntity m = new MultipartEntity();
		if (key == null) {
			key = UNDEFINED_KEY;
		}
		if ( ! key.equals(UNDEFINED_KEY)) {
			m.addField("key", key);
		}
		
		if (uptoken == null || uptoken.length() == 0) {
			ret.onFailure(new Exception("uptoken not specify"));
			return;
		}
		
		if (extra.checkCrc == PutExtra.AUTO_CRC32) {
			extra.crc32 = isa.crc32();
		}
		if (extra.checkCrc != PutExtra.UNUSE_CRC32) {
			m.addField("crc32", extra.crc32 + "");
		}
		
		for (Map.Entry<String, String> i: extra.params.entrySet()) {
			m.addField(i.getKey(), i.getValue());
		}

		m.addField("token", uptoken);
		m.addFile("file", extra.mimeType, key, isa);
        m.setProcessNotify(ret);


		defaultClient().call(Conf.UP_HOST, m, ret);
	}

	/**
	 * 通过提供URI来上传指定的文件
	 *
	 * @param mContext
	 * @param uptoken 用于上传的验证信息
	 * @param key
	 * @param uri 通过图库或其他拿到的URI
	 * @param extra 上传参数
	 * @param ret 结果回调函数
	 */
	public static void putFile(Context mContext, String uptoken, String key, Uri uri, PutExtra extra, final JSONObjectRet ret) {

		final InputStreamAt isa;
		try {
			isa = InputStreamAt.fromInputStream(mContext, mContext.getContentResolver().openInputStream(uri));
		} catch (FileNotFoundException e) {
			ret.onFailure(e);
			return;
		}
		
		put(uptoken, key, isa, extra, new JSONObjectRet() {
			@Override
			public void onSuccess(JSONObject obj) {
				isa.close();
				ret.onSuccess(obj);
			}

            @Override
            public void onProcess(long current, long total) {
                ret.onProcess(current, total);
            }

            @Override
			public void onFailure(Exception ex) {
				isa.close();
				ret.onFailure(ex);
			}
		});
	}
}

@@ -245,16 +245,58 @@
 Entity;%0A
+import org.apache.http.client.HttpClient;%0A
 import o
@@ -669,24 +669,26 @@
 mClient;%0A%09%7D%0A
+%E5%93%88%E5%93%88
 %0A%09/**%0A%09 * %E4%B8%8A%E4%BC%A0
@@ -738,19 +738,17 @@
 aram key
-   
+%09
   %E9%94%AE%E5%80%BC%E5%90%8D, U
@@ -784,19 +784,17 @@
 aram isa
-   
+%09
   %E4%BA%8C%E8%BF%9B%E5%88%B6%E6%95%B0%E6%8D%AE%0A
@@ -831,19 +831,17 @@
 aram ret
-   
+%09
   %E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0%0A%09
@@ -1236,26 +1236,24 @@
 return;%0A%09%09%7D%0A
-%09%09
 %0A%09%09if (extra
@@ -1417,17 +1417,16 @@
 );%0A%09%09%7D%0A%09
-%09
 %0A%09%09for (
@@ -1606,24 +1606,18 @@
 , isa);%0A
-        
+%09%09
 m.setPro
@@ -1638,32 +1638,131 @@
 );%0A%0A
-%0A
 %09%09
-defaultClient().call(
+Client client = defaultClient();%0A%09%09Client.ClientExecuter executer = client.makeClientExecuter();%0A%09%09client.call(executer, 
 Conf
@@ -2457,42 +2457,24 @@
 %09%7D%0A%0A
-            @Override%0A            
+%09%09%09@Override%0A%09%09%09
 publ
@@ -2523,24 +2523,12 @@
 ) %7B%0A
-                
+%09%09%09%09
 ret.
@@ -2558,35 +2558,17 @@
 l);%0A
-            %7D%0A%0A            
+%09%09%09%7D%0A%0A%09%09%09
 @Ove
@@ -2670,8 +2670,9 @@
 );%0A%09%7D%0A%7D%0A
+%0A

update io