aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/IO/SharpCifs/Util/Sharpen/OutputStream.cs
blob: 1804d67451e06cebbadb143b6cc07574ed4622ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.IO;

namespace SharpCifs.Util.Sharpen
{
    public class OutputStream : IDisposable
    {
        protected Stream Wrapped;

        public static implicit operator OutputStream(Stream s)
        {
            return Wrap(s);
        }

        public static implicit operator Stream(OutputStream s)
        {
            return s.GetWrappedStream();
        }

        public virtual void Close()
        {
            if (Wrapped != null)
            {
                //Stream.`Close` method deleted
                //Wrapped.Close ();
                Wrapped.Dispose();
            }
        }

        public void Dispose()
        {
            Close();
        }

        public virtual void Flush()
        {
            if (Wrapped != null)
            {
                Wrapped.Flush();
            }
        }

        internal Stream GetWrappedStream()
        {
            // Always create a wrapper stream (not directly Wrapped) since the subclass
            // may be overriding methods that need to be called when used through the Stream class
            return new WrappedSystemStream(this);
        }

        static internal OutputStream Wrap(Stream s)
        {
            OutputStream stream = new OutputStream();
            stream.Wrapped = s;
            return stream;
        }

        public virtual void Write(int b)
        {
            if (Wrapped is WrappedSystemStream)
                ((WrappedSystemStream)Wrapped).OutputStream.Write(b);
            else
            {
                if (Wrapped == null)
                    throw new NotImplementedException();
                Wrapped.WriteByte((byte)b);
            }
        }

        public virtual void Write(byte[] b)
        {
            Write(b, 0, b.Length);
        }

        public virtual void Write(byte[] b, int offset, int len)
        {
            if (Wrapped is WrappedSystemStream)
                ((WrappedSystemStream)Wrapped).OutputStream.Write(b, offset, len);
            else
            {
                if (Wrapped != null)
                {
                    Wrapped.Write(b, offset, len);
                }
                else
                {
                    for (int i = 0; i < len; i++)
                    {
                        Write(b[i + offset]);
                    }
                }
            }
        }
    }
}