package org.sunger.lib.http.client;

import android.os.Build;
import android.text.TextUtils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpResponse {
	private Map<String, List<String>> headers;
	private InputStream payload;
	private int contentLength;
	private String contentType;
	private int statusCode;
	private String fileName;
	private String result = "";

	public HttpResponse(int contentLength, String contentType, int statusCode,
			InputStream payload, Map<String, List<String>> rawHeaders) {
		this.contentLength = contentLength;
		this.contentType = contentType;
		this.statusCode = statusCode;
		this.payload = payload;
		init(rawHeaders);
	}

	private void init(Map<String, List<String>> rawHeaders) {
		initHeaders(rawHeaders);
		String contentDispositionStr = getFirstHeaderValue("Content-Disposition");
		if (!TextUtils.isEmpty(contentDispositionStr)) {
			fileName = contentDispositionStr.replace("attachment; filename=",
					"").trim();
		}
		if (TextUtils.isEmpty(fileName)) {
			result = readToText();
		}
	}

	private void initHeaders(Map<String, List<String>> rawHeaders) {
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
			final Map<String, List<String>> newHeaders = new HashMap<String, List<String>>(
					rawHeaders.size());
			for (final Map.Entry<String, List<String>> e : rawHeaders
					.entrySet()) {
				final String key = e.getKey();
				final int keyLen = key.length();
				final StringBuilder newKey = new StringBuilder(keyLen);
				for (int i = 0; i < keyLen; ++i) {
					final char c = key.charAt(i);
					final char c2;
					if (i == 0 || key.charAt(i - 1) == '-') {
						c2 = Character.toUpperCase(c);
					} else {
						c2 = c;
					}
					newKey.append(c2);
				}
				newHeaders.put(newKey.toString(), e.getValue());
			}
			this.headers = Collections.unmodifiableMap(newHeaders);
		} else {
			this.headers = Collections.unmodifiableMap(rawHeaders);
		}
	}

	private String readToText() {
		StringBuilder buffer = new StringBuilder();
		try {
			String enc = getContentCharset();
			if (enc == null) {
				enc = "UTF-8";
			}
			InputStream input = getPayload();
			InputStreamReader reader = new InputStreamReader(input, enc);
			final char[] inBuf = new char[64];
			for (int charsRead; (charsRead = reader.read(inBuf)) != -1;) {
				buffer.append(inBuf, 0, charsRead);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return buffer.toString();

	}

	public String getContentCharset() {
		final String contentType = getFirstHeaderValue("Content-Type");
		if (contentType == null) {
			return null;
		}
		final int i = contentType.indexOf('=');
		return i == -1 ? null : contentType.substring(i + 1).trim();
	}

	public InputStream getPayload() {
		return payload;
	}

	public int getStatusCode() {
		return statusCode;
	}

	public int getContentLength() {
		return contentLength;
	}

	public String getContentType() {
		return contentType;
	}

	public String getFileName() {
		return fileName;
	}

	public Map<String, List<String>> getHeaders() {
		return headers;
	}

	public String getResult() {
		return result;
	}

	public String getFirstHeaderValue(String name) {
		final List<String> values = headers.get(name);
		if (values == null || values.isEmpty()) {
			return null;
		}
		return values.get(0);
	}

}

@@ -279,16 +279,169 @@
 ponse %7B%0A
+%09private final static String CONTENT_DISPOSTION_CHAR = %22attachment; filename=%22;%0A%09private final static String CONTENT_DISPOSTION = %22Content-Disposition%22;%0A
 %09private
@@ -665,16 +665,33 @@
 esponse(
+String fileName, 
 int cont
@@ -720,17 +720,20 @@
 entType,
- 
+%0A%09%09%09
 int stat
@@ -739,20 +739,17 @@
 tusCode,
-%0A%09%09%09
+ 
 InputStr
@@ -760,17 +760,20 @@
 payload,
- 
+%0A%09%09%09
 Map%3CStri
@@ -804,16 +804,44 @@
 ders) %7B%0A
+%09%09this.fileName = fileName;%0A
 %09%09this.c
@@ -1129,166 +1129,242 @@
 lue(
-%22Content-Disposition%22);%0A%09%09if (!TextUtils.isEmpty(contentDispositionStr)) %7B%0A%09%09%09fileName = contentDispositionStr.replace(%22attachment; filename=%22,%0A%09%09%09%09%09%22%22).trim(
+CONTENT_DISPOSTION);%0A%09%09//%E5%BD%93%E6%96%87%E4%BB%B6%E5%90%8D%E4%B8%BA%E7%A9%BA%EF%BC%8C%E4%B8%94%E8%8E%B7%E5%8F%96%E5%88%B0Header%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8D%E4%B8%BA%E7%A9%BA%E7%9A%84%0A%09%09if (!TextUtils.isEmpty(contentDispositionStr)%0A%09%09%09%09&& TextUtils.isEmpty(fileName)) %7B%0A%09%09%09// %E5%88%87%E5%89%B2%E8%8E%B7%E5%8F%96%E6%96%87%E4%BB%B6%E5%90%8D%0A%09%09%09fileName = contentDispositionStr.substring(CONTENT_DISPOSTION_CHAR%0A%09%09%09%09%09.length()
 );%0A%09

Update HttpResponse.java