(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / TlsStream.cs
blobcb112e9bdad92d7ca5416e4ee6de9554f8ef6427
1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 using System;
26 using System.IO;
27 using System.Net;
29 namespace Mono.Security.Protocol.Tls
31 internal class TlsStream : Stream
33 #region Fields
35 private bool canRead;
36 private bool canWrite;
37 private MemoryStream buffer;
39 #endregion
41 #region Properties
43 public bool EOF
45 get
47 if (this.Position < this.Length)
49 return false;
51 else
53 return true;
58 #endregion
60 #region Stream Properties
62 public override bool CanWrite
64 get { return this.canWrite; }
67 public override bool CanRead
69 get { return this.canRead; }
72 public override bool CanSeek
74 get { return this.buffer.CanSeek; }
77 public override long Position
79 get { return this.buffer.Position; }
80 set { this.buffer.Position = value; }
83 public override long Length
85 get { return this.buffer.Length; }
88 #endregion
90 #region Constructors
92 public TlsStream() : base()
94 this.buffer = new MemoryStream(0);
95 this.canRead = false;
96 this.canWrite = true;
99 public TlsStream(byte[] data) : base()
101 this.buffer = new MemoryStream(data);
102 this.canRead = true;
103 this.canWrite = false;
106 #endregion
108 #region Specific Read Methods
110 public new byte ReadByte()
112 return (byte)base.ReadByte();
115 public short ReadInt16()
117 byte[] bytes = this.ReadBytes(2);
119 return IPAddress.HostToNetworkOrder(BitConverter.ToInt16(bytes, 0));
122 public int ReadInt24()
124 byte[] b = this.ReadBytes(3);
126 return ((b[0] & 0xff) << 16) | ((b[1] & 0xff) << 8) | (b[2] & 0xff);
129 public int ReadInt32()
131 byte[] bytes = this.ReadBytes(4);
133 return IPAddress.HostToNetworkOrder(BitConverter.ToInt32(bytes, 0));
136 public long ReadInt64()
138 byte[] bytes = this.ReadBytes(8);
140 return IPAddress.HostToNetworkOrder(BitConverter.ToInt64(bytes, 0));
143 public byte[] ReadBytes(int count)
145 byte[] bytes = new byte[count];
146 this.Read(bytes, 0, count);
148 return bytes;
151 #endregion
153 #region Specific Write Methods
155 public void Write(byte value)
157 this.WriteByte(value);
160 public void Write(short value)
162 byte[] bytes = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder(value));
163 this.Write(bytes);
166 public void WriteInt24(int value)
168 int int24 = IPAddress.HostToNetworkOrder(value);
169 byte[] content = new byte[3];
171 Buffer.BlockCopy(BitConverter.GetBytes(int24), 1, content, 0, 3);
173 this.Write(content);
176 public void Write(int value)
178 byte[] bytes = BitConverter.GetBytes((int)IPAddress.HostToNetworkOrder(value));
179 this.Write(bytes);
182 public void Write(long value)
184 byte[] bytes = BitConverter.GetBytes((long)IPAddress.HostToNetworkOrder(value));
185 this.Write(bytes);
188 public void Write(byte[] buffer)
190 this.Write(buffer, 0, buffer.Length);
193 #endregion
195 #region Methods
197 public void Reset()
199 this.buffer.SetLength(0);
200 this.buffer.Position = 0;
203 public byte[] ToArray()
205 return this.buffer.ToArray();
208 #endregion
210 #region Stream Methods
212 public override void Flush()
214 this.buffer.Flush();
217 public override void SetLength(long length)
219 this.buffer.SetLength(length);
222 public override long Seek(long offset, System.IO.SeekOrigin loc)
224 return this.buffer.Seek(offset, loc);
227 public override int Read(byte[] buffer, int offset, int count)
229 if (this.canRead)
231 return this.buffer.Read(buffer, offset, count);
233 throw new InvalidOperationException("Read operations are not allowed by this stream");
236 public override void Write(byte[] buffer, int offset, int count)
238 if (this.canWrite)
240 this.buffer.Write(buffer, offset, count);
242 else
244 throw new InvalidOperationException("Write operations are not allowed by this stream");
248 #endregion