NativeOutput.hx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.SeekSet;
  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. /*
  48. ** TODO not sure if this implementation is working
  49. override public function writeBytes( s : haxe.io.Bytes, pos : Int, len : Int ) : Int
  50. {
  51. if( pos < 0 || len < 0 || pos + len > s.length )
  52. throw haxe.io.Error.OutsideBounds;
  53. var ba = s.sub(pos, len).getData();
  54. var ret = stream.write(ba);
  55. return ret;
  56. }
  57. */
  58. }