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