aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/WrappedSystemStream.cs
blob: ef2993fa6109823b6548f2d7df8d2182b4a19758 (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
using System;
using System.IO;

namespace SharpCifs.Util.Sharpen
{
    internal class WrappedSystemStream : Stream
	{
		private InputStream _ist;
		private OutputStream _ost;
		int _position;
		int _markedPosition;

		public WrappedSystemStream (InputStream ist)
		{
			this._ist = ist;
		}

		public WrappedSystemStream (OutputStream ost)
		{
			this._ost = ost;
		}
		
		public InputStream InputStream {
			get { return _ist; }
		}

		public OutputStream OutputStream {
			get { return _ost; }
		}

        public void Close()    //remove `override`
        {
			if (_ist != null) {
                //Stream.`Close` method deleted
				//_ist.Close ();
                _ist.Dispose();
			}
			if (_ost != null) {
                //Stream.`Close` method deleted
				//_ost.Close ();
                _ost.Dispose();
			}
		}

		public override void Flush ()
		{
			_ost.Flush ();
		}

		public override int Read (byte[] buffer, int offset, int count)
		{
			int res = _ist.Read (buffer, offset, count);
			if (res != -1) {
				_position += res;
				return res;
			}
		    return 0;
		}

		public override int ReadByte ()
		{
			int res = _ist.Read ();
			if (res != -1)
				_position++;
			return res;
		}

		public override long Seek (long offset, SeekOrigin origin)
		{
			if (origin == SeekOrigin.Begin)
				Position = offset;
			else if (origin == SeekOrigin.Current)
				Position = Position + offset;
			else if (origin == SeekOrigin.End)
				Position = Length + offset;
			return Position;
		}

		public override void SetLength (long value)
		{
			throw new NotSupportedException ();
		}

		public override void Write (byte[] buffer, int offset, int count)
		{
			_ost.Write (buffer, offset, count);
			_position += count;
		}

		public override void WriteByte (byte value)
		{
			_ost.Write (value);
			_position++;
		}

		public override bool CanRead {
			get { return (_ist != null); }
		}

		public override bool CanSeek {
			get { return true; }
		}

		public override bool CanWrite {
			get { return (_ost != null); }
		}

		public override long Length {
			get { return _ist.Length; }
		}
		
		internal void OnMark (int nb)
		{
			_markedPosition = _position;
			_ist.Mark (nb);
		}
		
		public override long Position {
			get
			{
			    if (_ist != null && _ist.CanSeek ())
					return _ist.Position;
			    return _position;
			}
		    set
		    {
		        if (value == _position)
					return;
		        if (value == _markedPosition)
		            _ist.Reset ();
		        else if (_ist != null && _ist.CanSeek ()) {
		            _ist.Position = value;
		        }
		        else
		            throw new NotSupportedException ();
		    }
		}
	}
}