NativeInput.hx 996 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package sys.io;
  2. import haxe.Int64;
  3. import haxe.io.Bytes;
  4. import haxe.io.Eof;
  5. import haxe.io.Input;
  6. import java.io.Exceptions;
  7. class NativeInput extends Input
  8. {
  9. var stream:java.io.InputStream;
  10. public function new(stream)
  11. {
  12. this.stream = stream;
  13. }
  14. override public function readByte():Int
  15. {
  16. try
  17. {
  18. return stream.read();
  19. }
  20. catch (e:EOFException) {
  21. throw new Eof();
  22. }
  23. catch (e:IOException) {
  24. throw haxe.io.Error.Custom(e);
  25. }
  26. }
  27. override public function readBytes(s:Bytes, pos:Int, len:Int):Int
  28. {
  29. var ret = 0;
  30. try
  31. {
  32. ret = stream.read(s.getData(), pos, len);
  33. }
  34. catch (e:EOFException) {
  35. throw new Eof();
  36. }
  37. catch (e:IOException) {
  38. throw haxe.io.Error.Custom(e);
  39. }
  40. if (ret == -1)
  41. throw new Eof();
  42. return ret;
  43. }
  44. override public function close():Void
  45. {
  46. try
  47. {
  48. stream.close();
  49. }
  50. catch (e:IOException) {
  51. throw haxe.io.Error.Custom(e);
  52. }
  53. }
  54. }