You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

67 lines
1.7 KiB

  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. namespace MQTTnet.Server.Scripting
  6. {
  7. public class PythonIOStream : Stream
  8. {
  9. readonly ILogger _logger;
  10. readonly Encoding _encoder = Encoding.UTF8;
  11. public PythonIOStream(ILogger<PythonIOStream> logger)
  12. {
  13. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  14. }
  15. public override void Flush()
  16. {
  17. }
  18. public override int Read(byte[] buffer, int offset, int count)
  19. {
  20. throw new NotSupportedException();
  21. }
  22. public override long Seek(long offset, SeekOrigin origin)
  23. {
  24. throw new NotSupportedException();
  25. }
  26. public override void SetLength(long value)
  27. {
  28. throw new NotSupportedException();
  29. }
  30. public override void Write(byte[] buffer, int offset, int count)
  31. {
  32. if (buffer == null) throw new ArgumentNullException(nameof(buffer));
  33. if (count == 0)
  34. {
  35. return;
  36. }
  37. var text = _encoder.GetString(buffer, offset, count);
  38. if (text.Equals(Environment.NewLine))
  39. {
  40. return;
  41. }
  42. _logger.LogDebug(text);
  43. }
  44. public override bool CanRead { get; } = false;
  45. public override bool CanSeek { get; } = false;
  46. public override bool CanWrite { get; } = true;
  47. public override long Length { get; } = 0L;
  48. public override long Position
  49. {
  50. get => throw new NotSupportedException();
  51. set => throw new NotSupportedException();
  52. }
  53. }
  54. }