OutputStream.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. //Stream.`Close` method deleted
  20. //Wrapped.Close ();
  21. Wrapped.Dispose();
  22. }
  23. }
  24. public void Dispose ()
  25. {
  26. Close ();
  27. }
  28. public virtual void Flush ()
  29. {
  30. if (Wrapped != null) {
  31. Wrapped.Flush ();
  32. }
  33. }
  34. internal Stream GetWrappedStream ()
  35. {
  36. // Always create a wrapper stream (not directly Wrapped) since the subclass
  37. // may be overriding methods that need to be called when used through the Stream class
  38. return new WrappedSystemStream (this);
  39. }
  40. static internal OutputStream Wrap (Stream s)
  41. {
  42. OutputStream stream = new OutputStream ();
  43. stream.Wrapped = s;
  44. return stream;
  45. }
  46. public virtual void Write (int b)
  47. {
  48. if (Wrapped is WrappedSystemStream)
  49. ((WrappedSystemStream)Wrapped).OutputStream.Write (b);
  50. else {
  51. if (Wrapped == null)
  52. throw new NotImplementedException ();
  53. Wrapped.WriteByte ((byte)b);
  54. }
  55. }
  56. public virtual void Write (byte[] b)
  57. {
  58. Write (b, 0, b.Length);
  59. }
  60. public virtual void Write (byte[] b, int offset, int len)
  61. {
  62. if (Wrapped is WrappedSystemStream)
  63. ((WrappedSystemStream)Wrapped).OutputStream.Write (b, offset, len);
  64. else {
  65. if (Wrapped != null) {
  66. Wrapped.Write (b, offset, len);
  67. } else {
  68. for (int i = 0; i < len; i++) {
  69. Write (b[i + offset]);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }