Pārlūkot izejas kodu

support getBytes through Buffer in NativeTextInput (not sure if that's a good implementation

frabbit 11 gadi atpakaļ
vecāks
revīzija
bb97cd2ccd

+ 3 - 13
std/python/io/NativeBytesInput.hx

@@ -7,6 +7,7 @@ import haxe.io.Input;
 import python.io.IInput;
 import python.io.IoTools;
 import python.lib.Builtin;
+import python.lib.ByteArray;
 import python.lib.io.RawIOBase;
 import python.lib.io.IOBase.SeekSet;
 
@@ -35,19 +36,8 @@ class NativeBytesInput extends NativeInput<RawIOBase> implements IInput {
 		return IoTools.seekInBinaryMode(stream, p, pos);
 	}
 
-
-	override public function readBytes(s:haxe.io.Bytes, pos:Int, len:Int):Int
-	{
-		if( pos < 0 || len < 0 || pos + len > s.length )
-			throw haxe.io.Error.OutsideBounds;
-
-		stream.seek(pos, python.lib.io.IOBase.SeekSet.SeekCur);
-		var ba = Builtin.bytearray(len);
-		var ret = stream.readinto(ba);
-		s.blit(pos, haxe.io.Bytes.ofData(ba) ,0,len);
-		if (ret == 0)
-			throwEof();
-		return ret;
+	override function readinto (b:ByteArray):Int {
+		return stream.readinto(b);
 	}
 
 

+ 0 - 17
std/python/io/NativeBytesOutput.hx

@@ -27,21 +27,4 @@ class NativeBytesOutput extends NativeOutput<RawIOBase>{
 	{
 		stream.write(Builtin.bytearray([c]));
 	}
-
-	/*
-	** TODO not sure if this implementation is working
-
-	override public function writeBytes( s : haxe.io.Bytes, pos : Int, len : Int ) : Int
-	{
-		if( pos < 0 || len < 0 || pos + len > s.length )
-			throw haxe.io.Error.OutsideBounds;
-
-		var ba = s.sub(pos, len).getData();
-
-		var ret = stream.write(ba);
-
-		return ret;
-	}
-	*/
-
 }

+ 21 - 0
std/python/io/NativeInput.hx

@@ -3,7 +3,10 @@ package python.io;
 
 import haxe.io.Eof;
 import haxe.io.Input;
+import python.lib.Builtin;
+import python.lib.ByteArray;
 import python.lib.io.IOBase;
+import python.lib.io.RawIOBase;
 
 class NativeInput<T:IOBase> extends Input{
 
@@ -41,4 +44,22 @@ class NativeInput<T:IOBase> extends Input{
 	public function eof() {
 		return wasEof;
 	}
+
+	function readinto (b:ByteArray):Int {
+		throw "abstract method, should be overriden";
+	}
+
+	override public function readBytes(s:haxe.io.Bytes, pos:Int, len:Int):Int
+	{
+		if( pos < 0 || len < 0 || pos + len > s.length )
+			throw haxe.io.Error.OutsideBounds;
+
+		stream.seek(pos, python.lib.io.IOBase.SeekSet.SeekCur);
+		var ba = Builtin.bytearray(len);
+		var ret = readinto(ba);
+		s.blit(pos, haxe.io.Bytes.ofData(ba) ,0,len);
+		if (ret == 0)
+			throwEof();
+		return ret;
+	}
 }

+ 3 - 16
std/python/io/NativeTextInput.hx

@@ -8,6 +8,7 @@ import python.io.IInput;
 import python.io.IoTools;
 import python.io.NativeInput;
 import python.lib.Builtin;
+import python.lib.ByteArray;
 import python.lib.io.RawIOBase;
 import python.lib.io.IOBase.SeekSet;
 import python.lib.io.TextIOBase;
@@ -34,22 +35,8 @@ class NativeTextInput extends NativeInput<TextIOBase> implements IInput {
 		IoTools.seekInTextMode(stream, tell, p, pos);
 	}
 
-
-	override public function readBytes(s:haxe.io.Bytes, pos:Int, len:Int):Int
-	{
-		throw "not implemented";
-		/*
-		if( pos < 0 || len < 0 || pos + len > s.length )
-			throw haxe.io.Error.OutsideBounds;
-
-		stream.seek(pos, python.lib.io.IOBase.SeekSet.SeekCur);
-		var ba = Builtin.bytearray(len);
-		var ret = stream.readinto(ba);
-		s.blit(pos, haxe.io.Bytes.ofData(ba) ,0,len);
-		if (ret == 0)
-			throwEof();
-		return ret;
-		*/
+	override function readinto (b:ByteArray):Int {
+		return stream.buffer.readinto(b);
 	}
 
 }

+ 0 - 16
std/python/io/NativeTextOutput.hx

@@ -26,20 +26,4 @@ class NativeTextOutput extends NativeOutput<TextIOBase> {
 		stream.write(String.fromCharCode(c));
 	}
 
-	/*
-	** TODO not sure if this implementation is working
-
-	override public function writeBytes( s : haxe.io.Bytes, pos : Int, len : Int ) : Int
-	{
-		if( pos < 0 || len < 0 || pos + len > s.length )
-			throw haxe.io.Error.OutsideBounds;
-
-		var ba = s.sub(pos, len).getData();
-
-		var ret = stream.write(ba);
-
-		return ret;
-	}
-	*/
-
 }

+ 0 - 2
std/python/lib/io/TextIOBase.hx

@@ -6,8 +6,6 @@ import python.lib.io.IOBase;
 
 extern class TextIOBase extends IOBase {
 
-	function new (buffer:BufferedIOBase):Void;
-
 	public var encoding:String;
 
 	public function write (s:String):Int;