FileInput.hx 896 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package asys.io;
  2. import haxe.io.Bytes;
  3. class FileInput extends haxe.io.Input {
  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 readByte():Int {
  20. var buf = Bytes.alloc(1);
  21. file.readBuffer(buf, 0, 1, position++);
  22. return buf.get(0);
  23. }
  24. override public function readBytes(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 read = file.readBuffer(buf, pos, len, position).bytesRead;
  28. position += read;
  29. return read;
  30. }
  31. override public function close():Void {
  32. file.close();
  33. }
  34. }