浏览代码

added copy & writeInput.

Nicolas Cannasse 19 年之前
父节点
当前提交
5b93d4c62c
共有 2 个文件被更改,包括 30 次插入0 次删除
  1. 8 0
      std/neko/io/File.hx
  2. 22 0
      std/neko/io/Output.hx

+ 8 - 0
std/neko/io/File.hx

@@ -53,6 +53,14 @@ class File {
 	public static function append( path : String, binary : Bool ) {
 		return new FileOutput(untyped file_open(path.__s,(if( binary ) "ab" else "a").__s));
 	}
+	
+	public static function copy( src : String, dst : String ) {
+		var s = read(src,true);
+		var d = write(dst,true);
+		d.writeInput(s);
+		s.close();
+		d.close();
+	}
 
 	public static function stdin() {
 		return new FileInput(file_stdin());

+ 22 - 0
std/neko/io/Output.hx

@@ -120,6 +120,28 @@ class Output {
 	**/
 	public function prepare( nbytes : Int ) {
 	}
+	
+	public function writeInput( i : Input, ?bufsize : Int ) {
+		if( bufsize == null )
+			bufsize = 4096;
+		var buf = neko.Lib.makeString(bufsize);
+		try {
+			while( true ) {
+				var len = i.readBytes(buf,0,bufsize);
+				if( len == 0 )
+					throw Error.Blocked;
+				var p = 0;
+				while( len > 0 ) {
+					var k = writeBytes(buf,p,len);
+					if( k == 0 )
+						throw Error.Blocked;
+					p += k;
+					len -= k;
+				}
+			}
+		} catch( e : Eof ) {			
+		}
+	}
 
 	static var _float_bytes = neko.Lib.load("std","float_bytes",2);
 	static var _double_bytes = neko.Lib.load("std","double_bytes",2);