NativeInput.hx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (C)2005-2012 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 python.io;
  23. import haxe.io.Eof;
  24. import haxe.io.Input;
  25. import python.lib.Bytearray;
  26. import python.lib.io.IOBase;
  27. import python.lib.io.RawIOBase;
  28. class NativeInput<T:IOBase> extends Input{
  29. var stream:T;
  30. var wasEof:Bool;
  31. function new (s:T) {
  32. this.stream = s;
  33. wasEof = false;
  34. if (!stream.readable()) throw "Write-only stream";
  35. }
  36. public var canSeek(get_canSeek, null):Bool;
  37. private function get_canSeek():Bool
  38. {
  39. return stream.seekable();
  40. }
  41. override public function close():Void
  42. {
  43. stream.close();
  44. }
  45. public function tell() : Int
  46. {
  47. return stream.tell();
  48. }
  49. function throwEof() {
  50. wasEof = true;
  51. throw new Eof();
  52. }
  53. public function eof() {
  54. return wasEof;
  55. }
  56. function readinto (b:Bytearray):Int {
  57. throw "abstract method, should be overriden";
  58. }
  59. function seek (p:Int, mode:sys.io.FileSeek) {
  60. throw "abstract method, should be overriden";
  61. }
  62. override public function readBytes(s:haxe.io.Bytes, pos:Int, len:Int):Int
  63. {
  64. if( pos < 0 || len < 0 || pos + len > s.length )
  65. throw haxe.io.Error.OutsideBounds;
  66. if (canSeek) {
  67. seek(pos, SeekBegin);
  68. } else if (pos > 0) {
  69. throw "Cannot call readBytes for pos > 0 (" + pos + ") on not seekable stream";
  70. }
  71. var ba = new Bytearray(len);
  72. var ret = readinto(ba);
  73. s.blit(pos, haxe.io.Bytes.ofData(ba) ,0,len);
  74. if (ret == 0)
  75. throwEof();
  76. return ret;
  77. }
  78. }