NativeInput.hx 1.3 KB

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