12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package python.io;
- import haxe.io.Eof;
- import haxe.io.Input;
- import python.lib.Builtin;
- import python.lib.io.RawIOBase;
- import python.lib.io.IOBase.SeekSet;
- class NativeInput extends Input{
- var stream:RawIOBase;
- public var canSeek(get_canSeek, null):Bool;
- public function new (stream:RawIOBase) {
- this.stream = stream;
- if (!stream.readable()) throw "Write-only stream";
- }
- private function get_canSeek():Bool
- {
- return stream.seekable();
- }
- override public function close():Void
- {
- stream.close();
- }
- public function tell() : Int
- {
- return stream.tell();
- }
- override public function readByte():Int
- {
- var ret = stream.read(1);
- if (ret.length == 0) throw new Eof();
- return ret.get(0);
- }
- public function seek( p : Int, pos : sys.io.FileSeek ) : Void
- {
- var pos = switch(pos)
- {
- case SeekBegin: SeekSet.SeekStart;
- case SeekCur: SeekSet.SeekCur;
- case SeekEnd: SeekSet.SeekEnd;
- };
- stream.seek(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)
- throw new Eof();
- return ret;
- }
- /*
- public function eof() : Bool
- {
- return stream.Position == stream.Length;
- }
- */
- }
|