ManagedIStream.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.InteropServices.ComTypes;
  6. namespace Standard;
  7. internal sealed class ManagedIStream : IStream, IDisposable
  8. {
  9. public ManagedIStream(Stream source)
  10. {
  11. Verify.IsNotNull<Stream>(source, "source");
  12. this._source = source;
  13. }
  14. private void _Validate()
  15. {
  16. if (this._source == null)
  17. {
  18. throw new ObjectDisposedException("this");
  19. }
  20. }
  21. [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Standard.HRESULT.ThrowIfFailed(System.String)")]
  22. [Obsolete("The method is not implemented", true)]
  23. public void Clone(out IStream ppstm)
  24. {
  25. ppstm = null;
  26. HRESULT.STG_E_INVALIDFUNCTION.ThrowIfFailed("The method is not implemented.");
  27. }
  28. public void Commit(int grfCommitFlags)
  29. {
  30. this._Validate();
  31. this._source.Flush();
  32. }
  33. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  34. [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
  35. public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
  36. {
  37. Verify.IsNotNull<IStream>(pstm, "pstm");
  38. this._Validate();
  39. byte[] array = new byte[4096];
  40. long num;
  41. int num2;
  42. for (num = 0L; num < cb; num += (long) num2)
  43. {
  44. num2 = this._source.Read(array, 0, array.Length);
  45. if (num2 == 0)
  46. {
  47. break;
  48. }
  49. pstm.Write(array, num2, IntPtr.Zero);
  50. }
  51. if (IntPtr.Zero != pcbRead)
  52. {
  53. Marshal.WriteInt64(pcbRead, num);
  54. }
  55. if (IntPtr.Zero != pcbWritten)
  56. {
  57. Marshal.WriteInt64(pcbWritten, num);
  58. }
  59. }
  60. [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Standard.HRESULT.ThrowIfFailed(System.String)")]
  61. [Obsolete("The method is not implemented", true)]
  62. public void LockRegion(long libOffset, long cb, int dwLockType)
  63. {
  64. HRESULT.STG_E_INVALIDFUNCTION.ThrowIfFailed("The method is not implemented.");
  65. }
  66. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  67. public void Read(byte[] pv, int cb, IntPtr pcbRead)
  68. {
  69. this._Validate();
  70. int val = this._source.Read(pv, 0, cb);
  71. if (IntPtr.Zero != pcbRead)
  72. {
  73. Marshal.WriteInt32(pcbRead, val);
  74. }
  75. }
  76. [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Standard.HRESULT.ThrowIfFailed(System.String)")]
  77. [Obsolete("The method is not implemented", true)]
  78. public void Revert()
  79. {
  80. HRESULT.STG_E_INVALIDFUNCTION.ThrowIfFailed("The method is not implemented.");
  81. }
  82. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  83. public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
  84. {
  85. this._Validate();
  86. long val = this._source.Seek(dlibMove, (SeekOrigin) dwOrigin);
  87. if (IntPtr.Zero != plibNewPosition)
  88. {
  89. Marshal.WriteInt64(plibNewPosition, val);
  90. }
  91. }
  92. public void SetSize(long libNewSize)
  93. {
  94. this._Validate();
  95. this._source.SetLength(libNewSize);
  96. }
  97. public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag)
  98. {
  99. pstatstg = default(System.Runtime.InteropServices.ComTypes.STATSTG);
  100. this._Validate();
  101. pstatstg.type = 2;
  102. pstatstg.cbSize = this._source.Length;
  103. pstatstg.grfMode = 2;
  104. pstatstg.grfLocksSupported = 2;
  105. }
  106. [Obsolete("The method is not implemented", true)]
  107. [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Standard.HRESULT.ThrowIfFailed(System.String)")]
  108. public void UnlockRegion(long libOffset, long cb, int dwLockType)
  109. {
  110. HRESULT.STG_E_INVALIDFUNCTION.ThrowIfFailed("The method is not implemented.");
  111. }
  112. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  113. public void Write(byte[] pv, int cb, IntPtr pcbWritten)
  114. {
  115. this._Validate();
  116. this._source.Write(pv, 0, cb);
  117. if (IntPtr.Zero != pcbWritten)
  118. {
  119. Marshal.WriteInt32(pcbWritten, cb);
  120. }
  121. }
  122. public void Dispose()
  123. {
  124. this._source = null;
  125. }
  126. private const int STGTY_STREAM = 2;
  127. private const int STGM_READWRITE = 2;
  128. private const int LOCK_EXCLUSIVE = 2;
  129. private Stream _source;
  130. }