OutputStream.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.IO;
  3. namespace SharpCifs.Util.Sharpen
  4. {
  5. public class OutputStream : IDisposable
  6. {
  7. protected Stream Wrapped;
  8. public static implicit operator OutputStream(Stream s)
  9. {
  10. return Wrap(s);
  11. }
  12. public static implicit operator Stream(OutputStream s)
  13. {
  14. return s.GetWrappedStream();
  15. }
  16. public virtual void Close()
  17. {
  18. if (Wrapped != null)
  19. {
  20. //Stream.`Close` method deleted
  21. //Wrapped.Close ();
  22. Wrapped.Dispose();
  23. }
  24. }
  25. public void Dispose()
  26. {
  27. Close();
  28. }
  29. public virtual void Flush()
  30. {
  31. if (Wrapped != null)
  32. {
  33. Wrapped.Flush();
  34. }
  35. }
  36. internal Stream GetWrappedStream()
  37. {
  38. // Always create a wrapper stream (not directly Wrapped) since the subclass
  39. // may be overriding methods that need to be called when used through the Stream class
  40. return new WrappedSystemStream(this);
  41. }
  42. static internal OutputStream Wrap(Stream s)
  43. {
  44. OutputStream stream = new OutputStream();
  45. stream.Wrapped = s;
  46. return stream;
  47. }
  48. public virtual void Write(int b)
  49. {
  50. if (Wrapped is WrappedSystemStream)
  51. ((WrappedSystemStream)Wrapped).OutputStream.Write(b);
  52. else
  53. {
  54. if (Wrapped == null)
  55. throw new NotImplementedException();
  56. Wrapped.WriteByte((byte)b);
  57. }
  58. }
  59. public virtual void Write(byte[] b)
  60. {
  61. Write(b, 0, b.Length);
  62. }
  63. public virtual void Write(byte[] b, int offset, int len)
  64. {
  65. if (Wrapped is WrappedSystemStream)
  66. ((WrappedSystemStream)Wrapped).OutputStream.Write(b, offset, len);
  67. else
  68. {
  69. if (Wrapped != null)
  70. {
  71. Wrapped.Write(b, offset, len);
  72. }
  73. else
  74. {
  75. for (int i = 0; i < len; i++)
  76. {
  77. Write(b[i + offset]);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }