FileOutput.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package sys.io;
  26. import haxe.io.Bytes;
  27. import haxe.io.Eof;
  28. import haxe.io.Output;
  29. import java.io.Exceptions;
  30. /**
  31. Use [sys.io.File.write] to create a [FileOutput]
  32. **/
  33. class FileOutput extends Output {
  34. var f:java.io.RandomAccessFile;
  35. public function new(f)
  36. {
  37. this.f = f;
  38. }
  39. override public function writeByte(c:Int):Void
  40. {
  41. try
  42. {
  43. this.f.write(c);
  44. }
  45. catch (e:IOException) {
  46. throw haxe.io.Error.Custom(e);
  47. }
  48. }
  49. override public function write(s:Bytes):Void
  50. {
  51. try
  52. {
  53. this.f.write(s.getData());
  54. }
  55. catch (e:IOException) {
  56. throw haxe.io.Error.Custom(e);
  57. }
  58. }
  59. override public function writeBytes(s:Bytes, pos:Int, len:Int):Int
  60. {
  61. try
  62. {
  63. this.f.write(s.getData(), pos, len);
  64. return len;
  65. }
  66. catch (e:IOException) {
  67. throw haxe.io.Error.Custom(e);
  68. }
  69. }
  70. public function seek( p : Int, pos : FileSeek ) : Void
  71. {
  72. try
  73. {
  74. switch(pos)
  75. {
  76. case SeekBegin: f.seek(cast p);
  77. case SeekCur: f.seek(haxe.Int64.add(f.getFilePointer(), cast(p, haxe.Int64)));
  78. case SeekEnd: f.seek(haxe.Int64.add(f.length(), cast p));
  79. }
  80. }
  81. catch (e:EOFException) {
  82. throw new Eof();
  83. }
  84. catch (e:IOException) {
  85. throw haxe.io.Error.Custom(e);
  86. }
  87. }
  88. function tell() : Int
  89. {
  90. try
  91. {
  92. return cast f.getFilePointer();
  93. }
  94. catch (e:IOException) {
  95. throw haxe.io.Error.Custom(e);
  96. }
  97. }
  98. }