2
0

FileOutput.hx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.io;
  23. import haxe.io.Bytes;
  24. import haxe.io.Eof;
  25. import haxe.io.Output;
  26. import java.io.EOFException;
  27. import java.io.IOException;
  28. class FileOutput extends Output {
  29. var f:java.io.RandomAccessFile;
  30. function new(f) {
  31. this.f = f;
  32. }
  33. override public function close() {
  34. try
  35. f.close()
  36. catch (e:Dynamic)
  37. throw e;
  38. }
  39. override public function writeByte(c:Int):Void {
  40. try {
  41. this.f.write(c);
  42. } catch (e:IOException) {
  43. throw haxe.io.Error.Custom(e);
  44. }
  45. }
  46. override public function write(s:Bytes):Void {
  47. try {
  48. this.f.write(s.getData());
  49. } catch (e:IOException) {
  50. throw haxe.io.Error.Custom(e);
  51. }
  52. }
  53. override public function writeBytes(s:Bytes, pos:Int, len:Int):Int {
  54. try {
  55. this.f.write(s.getData(), pos, len);
  56. return len;
  57. } catch (e:IOException) {
  58. throw haxe.io.Error.Custom(e);
  59. }
  60. }
  61. public function seek(p:Int, pos:FileSeek):Void {
  62. try {
  63. switch (pos) {
  64. case SeekBegin:
  65. f.seek(cast p);
  66. case SeekCur:
  67. f.seek(haxe.Int64.add(f.getFilePointer(), cast(p, haxe.Int64)));
  68. case SeekEnd:
  69. f.seek(haxe.Int64.add(f.length(), cast p));
  70. }
  71. } catch (e:EOFException) {
  72. throw new Eof();
  73. } catch (e:IOException) {
  74. throw haxe.io.Error.Custom(e);
  75. }
  76. }
  77. public function tell():Int {
  78. try {
  79. return cast f.getFilePointer();
  80. } catch (e:IOException) {
  81. throw haxe.io.Error.Custom(e);
  82. }
  83. }
  84. }