FileInput.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. var ret = 0;
  56. try
  57. {
  58. ret = f.read(s.getData(), pos, len);
  59. }
  60. catch (e:EOFException) {
  61. throw new Eof();
  62. }
  63. catch (e:IOException) {
  64. throw haxe.io.Error.Custom(e);
  65. }
  66. if (ret == -1)
  67. throw new Eof();
  68. return ret;
  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, 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. public 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. public function eof() : Bool
  99. {
  100. try
  101. {
  102. return f.getFilePointer() == f.length();
  103. }
  104. catch (e:IOException) {
  105. throw haxe.io.Error.Custom(e);
  106. }
  107. }
  108. }