package org.nutz.lang;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

public class Streams {
	public static boolean equals(InputStream sA, InputStream sB) throws IOException {
		int dA;
		while ((dA = sA.read()) != -1) {
			if (dA != sB.read())
				return false;
		}
		if (sB.read() != -1)
			return false;
		return true;
	}

	public static void write(OutputStream ops, CharSequence cs) throws IOException {
		if (null == cs || null == ops)
			return;
		for (int i = 0; i < cs.length(); i++)
			ops.write(cs.charAt(i));
	}

	public static boolean safeClose(Closeable cb) {
		if (null != cb)
			try {
				cb.close();
			} catch (IOException e) {
				return false;
			}
		return true;
	}

	public static InputStream fileIn(String path) {
		InputStream ins = Files.findFileAsStream(path);
		if (null == ins) {
			File f = Files.findFile(path);
			if (null != f)
				try {
					return new FileInputStream(f);
				} catch (FileNotFoundException e) {}
		}
		return ins;
	}

	public static InputStream fileIn(File file) {
		try {
			return new FileInputStream(file);
		} catch (FileNotFoundException e) {
			throw Lang.wrapThrow(e);
		}
	}

	public static Reader fileInr(File file) {
		try {
			return new InputStreamReader(fileIn(file), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw Lang.wrapThrow(e);
		}
	}

	public static Reader fileInr(String path) {
		try {
			return new InputStreamReader(fileIn(path), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw Lang.wrapThrow(e);
		}
	}

	public static OutputStream fileOut(String path) {
		return fileOut(Files.findFile(path));
	}

	public static OutputStream fileOut(File file) {
		try {
			return new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			throw Lang.wrapThrow(e);
		}
	}

	public static Writer fileOutw(String path) {
		return fileOutw(Files.findFile(path));
	}

	public static Writer fileOutw(File file) {
		try {
			return new OutputStreamWriter(fileOut(file), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw Lang.wrapThrow(e);
		}
	}

}

@@ -431,32 +431,160 @@
 r;%0D%0A
-%0D%0Apublic class Streams %7B
+import java.nio.charset.Charset;%0D%0A%0D%0Apublic class Streams %7B%0D%0A%09%0D%0A%09public static final String DEFAULT_ENCODING = Charset.forName(%22UTF-8%22).displayName();%0D%0A%09
 %0D%0A%09p
@@ -1708,37 +1708,297 @@
 File file) %7B%0D%0A%09%09
-try %7B
+return fileInr(file, DEFAULT_ENCODING);%0D%0A%09%7D%0D%0A%0D%0A%09public static Reader fileInr(String path) %7B%0D%0A%09%09return fileInr(path, DEFAULT_ENCODING);%0D%0A%09%7D%0D%0A%09%0D%0A%09public static Reader fileInr(File file,String encoding) %7B%0D%0A%09%09try %7B%0D%0A%09%09%09if(encoding == null ) encoding = DEFAULT_ENCODING;
 %0D%0A%09%09%09return new 
@@ -2021,39 +2021,40 @@
 r(fileIn(file), 
-%22UTF-8%22
+encoding
 );%0D%0A%09%09%7D catch (U
@@ -2164,24 +2164,40 @@
 (String path
+,String encoding
 ) %7B%0D%0A%09%09try %7B
@@ -2190,32 +2190,87 @@
 ng) %7B%0D%0A%09%09try %7B%0D%0A
+%09%09%09if(encoding == null ) encoding = DEFAULT_ENCODING;%0D%0A
 %09%09%09return new In
@@ -2299,23 +2299,24 @@
 (path), 
-%22UTF-8%22
+encoding
 );%0D%0A%09%09%7D 
@@ -2807,37 +2807,322 @@
 File file) %7B%0D%0A%09%09
-try %7B
+return fileOutw(file, DEFAULT_ENCODING);%0D%0A%09%7D%0D%0A%0D%0A%09public static Writer fileOutw(String path,String encoding) %7B%0D%0A%09%09return fileOutw(Files.findFile(path),encoding);%0D%0A%09%7D%0D%0A%0D%0A%09public static Writer fileOutw(File file,String encoding) %7B%0D%0A%09%09try %7B%0D%0A%09%09%09if(encoding == null) encoding = DEFAULT_ENCODING;
 %0D%0A%09%09%09return new 
@@ -3155,23 +3155,24 @@
 (file), 
-%22UTF-8%22
+encoding
 );%0D%0A%09%09%7D 
@@ -3251,13 +3251,11 @@
 %09%7D%0D%0A%09%7D%0D%0A
-%0D%0A
 %7D%0D%0A

添加特性,允许使用自定义编码格式读写文件,默认为UTF-8