package cn.net.caoyao.cpzshandset.common.bitmap.download;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import cn.net.caoyao.cpzshandset.common.util.LogUtil;


public class SimpleDownloader implements Downloader {
	private static final String TAG = SimpleDownloader.class.getSimpleName();
	private static final int IO_BUFFER_SIZE = 8192;

	@SuppressWarnings("unused")
	public byte[] download(String urlString) {
		if (urlString == null) {
			return null;
		}
		
		if (urlString.trim().toLowerCase().startsWith("http"))
			return getFromHttp(urlString);
		//---我修改的
		label143:
		if (urlString.trim().toLowerCase().startsWith("file:")) {
			try {
				File f = new File(new URI(urlString));
				if ((!f.exists()) || (!f.canRead()))
					break label143;
				return getFromFile(f);
			} catch (URISyntaxException e) {
				LogUtil.e(TAG, "Error in read from file - " + urlString + " : " + e);
			}
		} else {
			File f = new File(urlString);
			if ((f.exists()) && (f.canRead())) {
				return getFromFile(f);
			}
		}

		label143: return null;
	}

	private byte[] getFromFile(File file) {
		if (file == null)
			return null;

		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = 0;
			byte[] buffer = new byte[1024];
			while ((len = fis.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			byte[] arrayOfByte1 = baos.toByteArray();
			return arrayOfByte1;
		} catch (Exception e) {
			LogUtil.e(TAG, "Error in read from file - " + file + " : " + e);
		} finally {
			if (fis != null) {
				try {
					fis.close();
					fis = null;
				} catch (IOException localIOException2) {
				}
			}
		}
		return null;
	}

	private byte[] getFromHttp(String urlString) {
		HttpURLConnection urlConnection = null;
		BufferedOutputStream out = null;
		FlushedInputStream in = null;
		try {
			URL url = new URL(urlString);
			urlConnection = (HttpURLConnection) url.openConnection();
			in = new FlushedInputStream(new BufferedInputStream(urlConnection.getInputStream(), 8192));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int b;
			while ((b = in.read()) != -1) {
				//----我修改的
//				int b;
				baos.write(b);
			}
			byte[] arrayOfByte = baos.toByteArray();
			return arrayOfByte;
		} catch (IOException e) {
			LogUtil.e(TAG, "Error in downloadBitmap - " + urlString + " : " + e);
		} finally {
			if (urlConnection != null)
				urlConnection.disconnect();
			try {
				if (out != null) {
					out.close();
				}
				if (in != null)
					in.close();
			} catch (IOException localIOException3) {
			}
		}
		return null;
	}

	public class FlushedInputStream extends FilterInputStream {
		public FlushedInputStream(InputStream inputStream) {
			//----我修改的
			super(inputStream);
		}

		public long skip(long n) throws IOException {
			long totalBytesSkipped = 0L;
			while (totalBytesSkipped < n) {
				long bytesSkipped = this.in.skip(n - totalBytesSkipped);
				if (bytesSkipped == 0L) {
					int by_te = read();
					if (by_te < 0) {
						break;
					}
					bytesSkipped = 1L;
				}

				totalBytesSkipped += bytesSkipped;
			}
			return totalBytesSkipped;
		}
	}
}