Updated Firebird provider with latest sources from sourceforge
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / ParameterBuffer.cs
1 /*\r
2  *      Firebird ADO.NET Data provider for .NET and     Mono \r
3  * \r
4  *         The contents of this file are subject to the Initial \r
5  *         Developer's Public License Version 1.0 (the "License"); \r
6  *         you may not use this file except in compliance with the \r
7  *         License. You may obtain a copy of the License at \r
8  *         http://www.firebirdsql.org/index.php?op=doc&id=idpl\r
9  *\r
10  *         Software distributed under the License is distributed on \r
11  *         an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either \r
12  *         express or implied. See the License for the specific \r
13  *         language governing rights and limitations under the License.\r
14  * \r
15  *      Copyright (c) 2002, 2005 Carlos Guzman Alvarez\r
16  *      All Rights Reserved.\r
17  */\r
18 \r
19 using System;\r
20 using System.IO;\r
21 using System.Text;\r
22 using System.Net;\r
23 \r
24 namespace FirebirdSql.Data.Common\r
25 {\r
26         internal abstract class ParameterBuffer\r
27         {\r
28                 #region Fields\r
29 \r
30                 private MemoryStream stream;\r
31                 private bool isLittleEndian;\r
32 \r
33                 #endregion\r
34 \r
35                 #region Properties\r
36 \r
37                 public short Length\r
38                 {\r
39                         get { return (short)this.ToArray().Length; }\r
40                 }\r
41 \r
42                 #endregion\r
43 \r
44                 #region Protected properties\r
45 \r
46                 protected bool IsLittleEndian\r
47                 {\r
48                         get { return this.isLittleEndian; }\r
49                 }\r
50 \r
51                 #endregion\r
52 \r
53                 #region Constructors\r
54 \r
55                 protected ParameterBuffer() : this(false)\r
56                 {\r
57                 }\r
58 \r
59                 protected ParameterBuffer(bool isLittleEndian)\r
60                 {\r
61                         this.stream = new MemoryStream();\r
62                         this.isLittleEndian = isLittleEndian;\r
63                 }\r
64 \r
65                 #endregion\r
66 \r
67                 #region Protected Methods\r
68 \r
69                 protected void WriteByte(int value)\r
70                 {\r
71                         this.WriteByte((byte)value);\r
72                 }\r
73 \r
74                 protected void WriteByte(byte value)\r
75                 {\r
76                         this.stream.WriteByte(value);\r
77                 }\r
78 \r
79                 protected void Write(short value)\r
80                 {\r
81                         if (!this.IsLittleEndian)\r
82                         {\r
83                                 value = (short)IPAddress.NetworkToHostOrder(value);\r
84                         }\r
85 \r
86                         byte[] buffer = BitConverter.GetBytes(value);\r
87 \r
88                         this.stream.Write(buffer, 0, buffer.Length);\r
89                 }\r
90 \r
91                 protected void Write(int value)\r
92                 {\r
93                         if (!this.IsLittleEndian)\r
94                         {\r
95                                 value = (int)IPAddress.NetworkToHostOrder(value);\r
96                         }\r
97 \r
98                         byte[] buffer = BitConverter.GetBytes(value);\r
99 \r
100                         this.stream.Write(buffer, 0, buffer.Length);\r
101                 }\r
102 \r
103                 protected void Write(byte[] buffer)\r
104                 {\r
105                         this.Write(buffer, 0, buffer.Length);\r
106                 }\r
107 \r
108                 protected void Write(byte[] buffer, int offset, int count)\r
109                 {\r
110                         this.stream.Write(buffer, offset, count);\r
111                 }\r
112 \r
113                 #endregion\r
114 \r
115                 #region Methods\r
116 \r
117                 public virtual void Append(int type)\r
118                 {\r
119                         this.WriteByte(type);\r
120                 }\r
121 \r
122                 public byte[] ToArray()\r
123                 {\r
124                         return stream.ToArray();\r
125                 }\r
126 \r
127                 #endregion\r
128         }\r
129 }\r