aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/InputStream.cs
blob: 2f3f070b50ffd9e331669051ea9a44987909a618 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.IO;

namespace SharpCifs.Util.Sharpen
{
    public class InputStream : IDisposable
	{
		private long _mark;
		protected Stream Wrapped;
		protected Stream BaseStream;

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

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

		public virtual int Available ()
		{
		    if (Wrapped is WrappedSystemStream)
				return ((WrappedSystemStream)Wrapped).InputStream.Available ();
		    return 0;
		}

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

		public void Dispose ()
		{
			Close ();
		}

        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);
		}

		public virtual void Mark (int readlimit)
		{
			if (Wrapped is WrappedSystemStream)
				((WrappedSystemStream)Wrapped).InputStream.Mark (readlimit);
			else {
				if (BaseStream is WrappedSystemStream)
					((WrappedSystemStream)BaseStream).OnMark (readlimit);
				if (Wrapped != null)
					_mark = Wrapped.Position;
			}
		}
		
		public virtual bool MarkSupported ()
		{
		    if (Wrapped is WrappedSystemStream)
				return ((WrappedSystemStream)Wrapped).InputStream.MarkSupported ();
		    return ((Wrapped != null) && Wrapped.CanSeek);
		}

	    public virtual int Read ()
		{
			if (Wrapped == null) {
				throw new NotImplementedException ();
			}
			return Wrapped.ReadByte ();
		}

		public virtual int Read (byte[] buf)
		{
			return Read (buf, 0, buf.Length);
		}

		public virtual int Read (byte[] b, int off, int len)
		{
			if (Wrapped is WrappedSystemStream)
				return ((WrappedSystemStream)Wrapped).InputStream.Read (b, off, len);
			
			if (Wrapped != null) {
				int num = Wrapped.Read (b, off, len);
				return ((num <= 0) ? -1 : num);
			}
			int totalRead = 0;
			while (totalRead < len) {
				int nr = Read ();
				if (nr == -1)
					return -1;
				b[off + totalRead] = (byte)nr;
				totalRead++;
			}
			return totalRead;
		}

		public virtual void Reset ()
		{
			if (Wrapped is WrappedSystemStream)
				((WrappedSystemStream)Wrapped).InputStream.Reset ();
			else {
				if (Wrapped == null)
					throw new IOException ();
				Wrapped.Position = _mark;
			}
		}

		public virtual long Skip (long cnt)
		{
			if (Wrapped is WrappedSystemStream)
				return ((WrappedSystemStream)Wrapped).InputStream.Skip (cnt);
			
			long n = cnt;
			while (n > 0) {
				if (Read () == -1)
					return cnt - n;
				n--;
			}
			return cnt - n;
		}
		
		internal virtual bool CanSeek ()
		{
		    if (Wrapped != null)
				return Wrapped.CanSeek;
		    return false;
		}

	    internal virtual long Position {
			get
			{
			    if (Wrapped != null)
					return Wrapped.Position;
			    throw new NotSupportedException ();
			}
	        set {
				if (Wrapped != null)
					Wrapped.Position = value;
				else
					throw new NotSupportedException ();
			}
		}

        public virtual long Length
        {
            get
            {
                if (Wrapped != null)
                {
                    return Wrapped.Length;    
                }

                throw new NotSupportedException();
            }
        }

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