FileOutput.hx 986 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package asys.io;
  2. import haxe.io.Bytes;
  3. class FileOutput extends haxe.io.Output {
  4. final file:asys.io.File;
  5. var position:Int = 0;
  6. function new(file:asys.io.File) {
  7. this.file = file;
  8. }
  9. public function seek(p:Int, pos:sys.io.FileSeek):Void {
  10. position = (switch (pos) {
  11. case SeekBegin: p;
  12. case SeekCur: position + p;
  13. case SeekEnd: file.stat().size + p;
  14. });
  15. }
  16. public function tell():Int {
  17. return position;
  18. }
  19. override public function writeByte(byte:Int):Void {
  20. var buf = Bytes.alloc(1);
  21. buf.set(1, byte);
  22. file.writeBuffer(buf, 0, 1, position++);
  23. }
  24. override public function writeBytes(buf:Bytes, pos:Int, len:Int):Int {
  25. if (pos < 0 || len < 0 || pos + len > buf.length)
  26. throw haxe.io.Error.OutsideBounds;
  27. var written = file.writeBuffer(buf, pos, len, position).bytesWritten;
  28. position += written;
  29. return written;
  30. }
  31. override public function flush():Void {
  32. file.datasync();
  33. }
  34. override public function close():Void {
  35. file.close();
  36. }
  37. }