Fix problems with overlong directory names: phase #1
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlPasswordPacket.cs
1 // created on 10/6/2002 at 21:33
2
3 // Npgsql.NpgsqlPasswordPacket.cs
4 //
5 // Author:
6 //      Francisco Jr. (fxjrlists@yahoo.com.br)
7 //
8 //      Copyright (C) 2002 The Npgsql Development Team
9 //      npgsql-general@gborg.postgresql.org
10 //      http://gborg.postgresql.org/project/npgsql/projdisplay.php
11 //
12 //
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2.1 of the License, or (at your option) any later version.
17 //
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 // Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27
28 using System;
29 using System.IO;
30 using System.Text;
31 using System.Net;
32
33 namespace Npgsql
34 {
35     /// <summary>
36     /// This class represents a PasswordPacket message sent to backend
37     /// PostgreSQL.
38     /// </summary>
39     internal sealed class NpgsqlPasswordPacket
40     {
41         // Logging related values
42         private static readonly String CLASSNAME = "NpgsqlPasswordPacket";
43
44         private String password;
45         private ProtocolVersion protocolVersion;
46
47
48         public NpgsqlPasswordPacket(String password, ProtocolVersion protocolVersion)
49         {
50             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
51
52             this.password = password;
53             this.protocolVersion = protocolVersion;
54         }
55
56         public void WriteToStream(Stream outputStream, Encoding encoding)
57         {
58             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "WriteToStream");
59
60             switch (protocolVersion) {
61             case ProtocolVersion.Version2 :
62                 // Write the size of the packet.
63                 // 4 + (passwordlength + 1) -> Int32 + NULL terminated string.
64                 // output_stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(4 + (password.Length + 1))), 0, 4);
65                 PGUtil.WriteInt32(outputStream, 4 + encoding.GetByteCount(password) + 1);
66
67                 // Write String.
68                 PGUtil.WriteString(password, outputStream, encoding);
69
70                 break;
71
72             case ProtocolVersion.Version3 :
73                 outputStream.WriteByte((Byte)'p');
74                 PGUtil.WriteInt32(outputStream, 4 + encoding.GetByteCount(password) + 1);
75
76                 // Write String.
77                 PGUtil.WriteString(password, outputStream, encoding);
78
79                 break;
80
81             }
82         }
83     }
84 }