NativeInput.hx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package python.io;
  2. import haxe.io.Eof;
  3. import haxe.io.Input;
  4. import python.lib.io.RawIOBase;
  5. import python.lib.io.IOBase.SeekSet;
  6. class NativeInput extends Input{
  7. var stream:RawIOBase;
  8. public var canSeek(get_canSeek, null):Bool;
  9. public function new (stream:RawIOBase) {
  10. this.stream = stream;
  11. if (!stream.readable()) throw "Write-only stream";
  12. }
  13. private function get_canSeek():Bool
  14. {
  15. return stream.seekable();
  16. }
  17. override public function close():Void
  18. {
  19. stream.close();
  20. }
  21. public function tell() : Int
  22. {
  23. return stream.tell();
  24. }
  25. override public function readByte():Int
  26. {
  27. var ret = stream.read(1);
  28. if (ret.length == 0) throw new Eof();
  29. return ret.get(0);
  30. }
  31. public function seek( p : Int, pos : sys.io.FileSeek ) : Void
  32. {
  33. var pos = switch(pos)
  34. {
  35. case SeekBegin: SeekSet.SeekStart;
  36. case SeekCur: SeekSet.SeekCur;
  37. case SeekEnd: SeekSet.SeekEnd;
  38. };
  39. stream.seek(p, pos);
  40. }
  41. /*
  42. override public function readBytes(s:Bytes, pos:Int, len:Int):Int
  43. {
  44. if( pos < 0 || len < 0 || pos + len > s.length )
  45. throw Error.OutsideBounds;
  46. var ret = stream.Read(s.getData(), pos, len);
  47. if (ret == 0)
  48. throw new Eof();
  49. return ret;
  50. }
  51. */
  52. /*
  53. public function eof() : Bool
  54. {
  55. return stream.Position == stream.Length;
  56. }
  57. */
  58. }