NativeInput.hx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package python.io;
  2. import haxe.io.Eof;
  3. import haxe.io.Input;
  4. import python.lib.Builtin;
  5. import python.lib.Bytearray;
  6. import python.lib.io.IOBase;
  7. import python.lib.io.RawIOBase;
  8. class NativeInput<T:IOBase> extends Input{
  9. var stream:T;
  10. var wasEof:Bool;
  11. function new (s:T) {
  12. this.stream = s;
  13. wasEof = false;
  14. if (!stream.readable()) throw "Write-only stream";
  15. }
  16. public var canSeek(get_canSeek, null):Bool;
  17. private function get_canSeek():Bool
  18. {
  19. return stream.seekable();
  20. }
  21. override public function close():Void
  22. {
  23. stream.close();
  24. }
  25. public function tell() : Int
  26. {
  27. return stream.tell();
  28. }
  29. function throwEof() {
  30. wasEof = true;
  31. throw new Eof();
  32. }
  33. public function eof() {
  34. return wasEof;
  35. }
  36. function readinto (b:Bytearray):Int {
  37. throw "abstract method, should be overriden";
  38. }
  39. override public function readBytes(s:haxe.io.Bytes, pos:Int, len:Int):Int
  40. {
  41. if( pos < 0 || len < 0 || pos + len > s.length )
  42. throw haxe.io.Error.OutsideBounds;
  43. stream.seek(pos, python.lib.io.IOBase.SeekSet.SeekCur);
  44. var ba = new Bytearray(len);
  45. var ret = readinto(ba);
  46. s.blit(pos, haxe.io.Bytes.ofData(ba) ,0,len);
  47. if (ret == 0)
  48. throwEof();
  49. return ret;
  50. }
  51. }