SftpOpenRequest.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Text;
  3. using Renci.SshNet.Sftp.Responses;
  4. namespace Renci.SshNet.Sftp.Requests
  5. {
  6. internal class SftpOpenRequest : SftpRequest
  7. {
  8. #if TUNING
  9. private byte[] _fileName;
  10. private byte[] _attributes;
  11. #endif
  12. public override SftpMessageTypes SftpMessageType
  13. {
  14. get { return SftpMessageTypes.Open; }
  15. }
  16. #if TUNING
  17. public string Filename
  18. {
  19. get { return Encoding.GetString(_fileName, 0, _fileName.Length); }
  20. private set { _fileName = Encoding.GetBytes(value); }
  21. }
  22. #else
  23. public string Filename { get; private set; }
  24. #endif
  25. public Flags Flags { get; private set; }
  26. #if TUNING
  27. public SftpFileAttributes Attributes
  28. {
  29. get { return SftpFileAttributes.FromBytes(_attributes); }
  30. private set { _attributes = value.GetBytes(); }
  31. }
  32. #else
  33. public SftpFileAttributes Attributes { get; private set; }
  34. #endif
  35. public Encoding Encoding { get; private set; }
  36. #if TUNING
  37. /// <summary>
  38. /// Gets the size of the message in bytes.
  39. /// </summary>
  40. /// <value>
  41. /// The size of the messages in bytes.
  42. /// </value>
  43. protected override int BufferCapacity
  44. {
  45. get
  46. {
  47. var capacity = base.BufferCapacity;
  48. capacity += 4; // FileName length
  49. capacity += _fileName.Length; // FileName
  50. capacity += 4; // Flags
  51. capacity += _attributes.Length; // Attributes
  52. return capacity;
  53. }
  54. }
  55. #endif
  56. public SftpOpenRequest(uint protocolVersion, uint requestId, string fileName, Encoding encoding, Flags flags, Action<SftpHandleResponse> handleAction, Action<SftpStatusResponse> statusAction)
  57. : this(protocolVersion, requestId, fileName, encoding, flags, SftpFileAttributes.Empty, handleAction, statusAction)
  58. {
  59. }
  60. private SftpOpenRequest(uint protocolVersion, uint requestId, string fileName, Encoding encoding, Flags flags, SftpFileAttributes attributes, Action<SftpHandleResponse> handleAction, Action<SftpStatusResponse> statusAction)
  61. : base(protocolVersion, requestId, statusAction)
  62. {
  63. Encoding = encoding;
  64. Filename = fileName;
  65. Flags = flags;
  66. Attributes = attributes;
  67. SetAction(handleAction);
  68. }
  69. protected override void LoadData()
  70. {
  71. base.LoadData();
  72. throw new NotSupportedException();
  73. }
  74. protected override void SaveData()
  75. {
  76. base.SaveData();
  77. #if TUNING
  78. WriteBinaryString(_fileName);
  79. #else
  80. Write(Filename, Encoding);
  81. #endif
  82. Write((uint)Flags);
  83. #if TUNING
  84. Write(_attributes);
  85. #else
  86. Write(Attributes);
  87. #endif
  88. }
  89. }
  90. }