(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / ByteFX.Data / mysqlclient / Packet.cs
blob60e45fc0524d908ca818de48f6153ad9154586be
1 using System;
2 using System.IO;
3 using System.Text;
5 namespace ByteFX.Data.MySqlClient
7 /// <summary>
8 /// Summary description for Packet.
9 /// </summary>
10 internal class Packet : MemoryStream
12 Encoding encoding;
13 byte sequence;
14 int completeLen;
15 public static int NULL_LEN=-1;
16 private int shortLen = 2;
17 private int intLen = 3;
18 private int longLen = 4;
19 private bool longInts = false;
21 public Packet(bool longInts) : base()
23 LongInts = longInts;
26 public Packet(byte[] bytes, bool longInts) : base(bytes.Length)
28 this.Write( bytes, 0, bytes.Length );
29 Position = 0;
30 LongInts = longInts;
33 public bool LongInts
35 get { return longInts; }
36 set
38 longInts = value;
39 if (longInts)
41 intLen = 4;
42 longLen = 8;
47 public int CompleteLength
49 get { return completeLen; }
50 set { completeLen = value; }
53 public byte Sequence
55 get { return sequence; }
56 set { sequence = value; }
59 public Encoding Encoding
61 set { encoding = value; }
62 get { return encoding; }
65 public void Clear()
67 Position = 0;
68 this.SetLength(0);
71 public byte this[int index]
73 get
75 long pos = Position;
76 Position = index;
77 byte b = (byte)ReadByte();
78 Position = pos;
79 return b;
83 public new int Length
85 get { return (int)base.Length; }
88 public bool IsLastPacket()
90 return (Length == 1 && this[0] == 0xfe);
93 public void Append( Packet p )
95 long oldPos = Position;
96 Position = Length;
97 this.Write( p.GetBuffer(), 0, p.Length );
98 Position = oldPos;
101 public Packet ReadPacket()
103 if (! HasMoreData) return null;
105 int len = this.ReadInteger(3);
106 byte seq = (byte)this.ReadByte();
107 byte[] buf = new byte[ len ];
108 this.Read( buf, 0, len );
109 Packet p = new Packet( buf, LongInts );
110 p.Sequence = seq;
111 p.Encoding = this.Encoding;
112 return p;
115 public int ReadNBytes()
117 byte c = (byte)ReadByte();
118 if (c < 1 || c > 4) throw new MySqlException("Unexpected byte count received");
119 return ReadInteger((int)c);
122 public string ReadLenString()
124 long len = ReadLenInteger();
126 byte[] buffer = new Byte[len];
127 Read(buffer, 0, (int)len);
128 return encoding.GetString( buffer, 0, (int)len);
132 /// <summary>
133 /// WriteInteger
134 /// </summary>
135 /// <param name="v"></param>
136 /// <param name="numbytes"></param>
137 public void WriteInteger( int v, int numbytes )
139 int val = v;
141 if (numbytes < 1 || numbytes > 4)
142 throw new ArgumentOutOfRangeException("Wrong byte count for WriteInteger");
144 for (int x=0; x < numbytes; x++)
146 WriteByte( (byte)(val&0xff) );
147 val >>= 8;
151 /// <summary>
152 ///
153 /// </summary>
154 /// <param name="numbytes"></param>
155 /// <returns></returns>
156 public int ReadInteger(int numbytes)
158 int val = 0;
159 int raise = 1;
160 for (int x=0; x < numbytes; x++)
162 int b = ReadByte();
163 val += (b*raise);
164 raise *= 256;
166 return val;
169 /// <summary>
170 ///
171 /// </summary>
172 /// <returns></returns>
173 public long ReadLenInteger()
175 byte c = (byte)ReadByte();
177 switch(c)
179 case 251 : return NULL_LEN;
180 case 252 : return ReadInteger(shortLen);
181 case 253 : return ReadInteger(intLen);
182 case 254 : return ReadInteger(longLen);
183 default : return c;
187 public bool HasMoreData
189 get { return Position < Length; }
192 #region String Functions
193 public string ReadString()
195 System.Text.StringBuilder sb = new System.Text.StringBuilder();
197 while ( HasMoreData )
199 byte b = (byte)ReadByte();
200 if (b == 0) break;
201 sb.Append( Convert.ToChar( b ));
204 return sb.ToString();
207 public void WriteString(string v, Encoding encoding)
209 WriteStringNoNull(v, encoding);
210 WriteByte(0);
213 public void WriteStringNoNull(string v, Encoding encoding)
215 byte[] bytes = encoding.GetBytes(v);
216 Write(bytes, 0, bytes.Length);
219 #endregion