FileInput.hx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.Int64;
  27. import haxe.io.Bytes;
  28. import haxe.io.Eof;
  29. import haxe.io.Input;
  30. import java.io.Exceptions;
  31. /**
  32. Use [sys.io.File.read] to create a [FileInput]
  33. **/
  34. class FileInput extends Input {
  35. var f:java.io.RandomAccessFile;
  36. public function new(f)
  37. {
  38. this.f = f;
  39. }
  40. override public function readByte():Int
  41. {
  42. try
  43. {
  44. return f.readUnsignedByte();
  45. }
  46. catch (e:EOFException) {
  47. throw new Eof();
  48. }
  49. catch (e:IOException) {
  50. throw haxe.io.Error.Custom(e);
  51. }
  52. }
  53. override public function readBytes(s:Bytes, pos:Int, len:Int):Int
  54. {
  55. try
  56. {
  57. return f.read(s.getData(), pos, len);
  58. }
  59. catch (e:EOFException) {
  60. throw new Eof();
  61. }
  62. catch (e:IOException) {
  63. throw haxe.io.Error.Custom(e);
  64. }
  65. }
  66. public function seek( p : Int, pos : FileSeek ) : Void
  67. {
  68. try
  69. {
  70. switch(pos)
  71. {
  72. case SeekBegin: f.seek(cast p);
  73. case SeekCur: f.seek(haxe.Int64.add(f.getFilePointer(), cast(p, Int64)));
  74. case SeekEnd: f.seek(haxe.Int64.add(f.length(), cast p));
  75. }
  76. }
  77. catch (e:EOFException) {
  78. throw new Eof();
  79. }
  80. catch (e:IOException) {
  81. throw haxe.io.Error.Custom(e);
  82. }
  83. }
  84. function tell() : Int
  85. {
  86. try
  87. {
  88. return cast f.getFilePointer();
  89. }
  90. catch (e:IOException) {
  91. throw haxe.io.Error.Custom(e);
  92. }
  93. }
  94. function eof() : Bool
  95. {
  96. try
  97. {
  98. return f.getFilePointer() == f.length();
  99. }
  100. catch (e:IOException) {
  101. throw haxe.io.Error.Custom(e);
  102. }
  103. }
  104. }