NativeOutput.hx 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package python.io;
  2. import haxe.io.Output;
  3. import python.lib.Builtin;
  4. import python.lib.io.IOBase;
  5. import python.lib.io.RawIOBase;
  6. class NativeOutput extends Output{
  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.writable()) throw "Read 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. public function seek( p : Int, pos : sys.io.FileSeek ) : Void
  26. {
  27. var pos = switch(pos)
  28. {
  29. case SeekBegin: SeekSet.SeekStart;
  30. case SeekCur: SeekSet.SeekCur;
  31. case SeekEnd: SeekSet.SeekEnd;
  32. };
  33. stream.seek(p, pos);
  34. }
  35. override public function prepare(nbytes:Int):Void
  36. {
  37. stream.truncate(nbytes);
  38. }
  39. override public function flush():Void
  40. {
  41. stream.flush();
  42. }
  43. override public function writeByte(c:Int):Void
  44. {
  45. stream.write(Builtin.bytearray([c]));
  46. }
  47. }