Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PackagePartStream.cs 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2016 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features and fast professional support,
  7. pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
  8. ***********************************************************************************/
  9. using System.IO;
  10. using System.Threading;
  11. // This classe is used to prevent deadlocks.
  12. // It is based on https://stackoverflow.com/questions/21482820/openxml-hanging-while-writing-elements
  13. namespace Xceed.Words.NET
  14. {
  15. public class PackagePartStream : Stream
  16. {
  17. #region Private Members
  18. private static readonly Mutex _mutex = new Mutex( false );
  19. private readonly Stream _stream;
  20. #endregion
  21. #region constructors
  22. public PackagePartStream( Stream stream )
  23. {
  24. _stream = stream;
  25. }
  26. #endregion
  27. #region Overrides Properties
  28. public override bool CanRead
  29. {
  30. get
  31. {
  32. return _stream.CanRead;
  33. }
  34. }
  35. public override bool CanSeek
  36. {
  37. get
  38. {
  39. return _stream.CanSeek;
  40. }
  41. }
  42. public override bool CanWrite
  43. {
  44. get
  45. {
  46. return _stream.CanWrite;
  47. }
  48. }
  49. public override long Length
  50. {
  51. get
  52. {
  53. return _stream.Length;
  54. }
  55. }
  56. public override long Position
  57. {
  58. get
  59. {
  60. return _stream.Position;
  61. }
  62. set
  63. {
  64. _stream.Position = value;
  65. }
  66. }
  67. #endregion
  68. #region Overrides Methods
  69. public override long Seek( long offset, SeekOrigin origin )
  70. {
  71. return _stream.Seek( offset, origin );
  72. }
  73. public override void SetLength( long value )
  74. {
  75. _stream.SetLength( value );
  76. }
  77. public override int Read( byte[] buffer, int offset, int count )
  78. {
  79. return _stream.Read( buffer, offset, count );
  80. }
  81. public override void Write( byte[] buffer, int offset, int count )
  82. {
  83. _mutex.WaitOne( Timeout.Infinite, false );
  84. _stream.Write( buffer, offset, count );
  85. _mutex.ReleaseMutex();
  86. }
  87. public override void Flush()
  88. {
  89. _mutex.WaitOne( Timeout.Infinite, false );
  90. _stream.Flush();
  91. _mutex.ReleaseMutex();
  92. }
  93. public override void Close()
  94. {
  95. _stream.Close();
  96. }
  97. protected override void Dispose( bool disposing )
  98. {
  99. _stream.Dispose();
  100. }
  101. #endregion
  102. }
  103. }