瀏覽代碼

[lua] fix File and FileInput for reading/writing bytes

Justin Donaldson 9 年之前
父節點
當前提交
5b3641b6bb
共有 2 個文件被更改,包括 6 次插入7 次删除
  1. 3 6
      std/lua/_std/sys/io/File.hx
  2. 3 1
      std/lua/_std/sys/io/FileInput.hx

+ 3 - 6
std/lua/_std/sys/io/File.hx

@@ -44,18 +44,15 @@ class File {
 	}
 	}
 
 
 	public static function getBytes( path : String ) : haxe.io.Bytes {
 	public static function getBytes( path : String ) : haxe.io.Bytes {
-		var f = read(path, true);
-		var ret = f.readAll();
-		f.close();
-		return ret;
+		return haxe.io.Bytes.ofString(getContent(path));
 	}
 	}
 
 
 	public static function read( path : String, binary : Bool = true ) : FileInput {
 	public static function read( path : String, binary : Bool = true ) : FileInput {
-		return new FileInput(Io.open(path,'r'));
+		return new FileInput(Io.open(path, binary ? 'rb' : 'r'));
 	}
 	}
 
 
 	public static function write( path : String, binary : Bool = true ) : FileOutput {
 	public static function write( path : String, binary : Bool = true ) : FileOutput {
-		return new FileOutput(Io.open(path,'w'));
+		return new FileOutput(Io.open(path, binary ? 'wb' : 'w'));
 	}
 	}
 
 
 	public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void {
 	public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void {

+ 3 - 1
std/lua/_std/sys/io/FileInput.hx

@@ -53,7 +53,9 @@ class FileInput extends haxe.io.Input {
 	}
 	}
 
 
 	override inline public function readByte() : Int {
 	override inline public function readByte() : Int {
-		return NativeStringTools.byte(f.read(1));
+		var byte = f.read(1);
+		if (byte == null) throw new haxe.io.Eof();
+		return NativeStringTools.byte(byte);
 	}
 	}
 
 
 	override inline public function close() : Void {
 	override inline public function close() : Void {