2010-06-23: Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / RabbitMQ.Client / src / util / NetworkBinaryReader.cs
1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
3 //
4 // The APL v2.0:
5 //
6 //---------------------------------------------------------------------------
7 //   Copyright (C) 2007-2010 LShift Ltd., Cohesive Financial
8 //   Technologies LLC., and Rabbit Technologies Ltd.
9 //
10 //   Licensed under the Apache License, Version 2.0 (the "License");
11 //   you may not use this file except in compliance with the License.
12 //   You may obtain a copy of the License at
13 //
14 //       http://www.apache.org/licenses/LICENSE-2.0
15 //
16 //   Unless required by applicable law or agreed to in writing, software
17 //   distributed under the License is distributed on an "AS IS" BASIS,
18 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 //   See the License for the specific language governing permissions and
20 //   limitations under the License.
21 //---------------------------------------------------------------------------
22 //
23 // The MPL v1.1:
24 //
25 //---------------------------------------------------------------------------
26 //   The contents of this file are subject to the Mozilla Public License
27 //   Version 1.1 (the "License"); you may not use this file except in
28 //   compliance with the License. You may obtain a copy of the License at
29 //   http://www.rabbitmq.com/mpl.html
30 //
31 //   Software distributed under the License is distributed on an "AS IS"
32 //   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 //   License for the specific language governing rights and limitations
34 //   under the License.
35 //
36 //   The Original Code is The RabbitMQ .NET Client.
37 //
38 //   The Initial Developers of the Original Code are LShift Ltd,
39 //   Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
40 //
41 //   Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 //   Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 //   are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 //   Technologies LLC, and Rabbit Technologies Ltd.
45 //
46 //   Portions created by LShift Ltd are Copyright (C) 2007-2010 LShift
47 //   Ltd. Portions created by Cohesive Financial Technologies LLC are
48 //   Copyright (C) 2007-2010 Cohesive Financial Technologies
49 //   LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 //   (C) 2007-2010 Rabbit Technologies Ltd.
51 //
52 //   All Rights Reserved.
53 //
54 //   Contributor(s): ______________________________________.
55 //
56 //---------------------------------------------------------------------------
57 using System.IO;
58 using System.Text;
59
60 namespace RabbitMQ.Util {
61     /// <summary>
62     /// Subclass of BinaryReader that reads integers etc in correct network order.
63     /// </summary>
64     /// <remarks>
65     /// <para>
66     /// Kludge to compensate for .NET's broken little-endian-only BinaryReader.
67     /// Relies on BinaryReader always being little-endian.
68     /// </para>
69     /// </remarks>
70     public class NetworkBinaryReader: BinaryReader {
71         // Not particularly efficient. To be more efficient, we could
72         // reuse BinaryReader's implementation details: m_buffer and
73         // FillBuffer, if they weren't private
74         // members. Private/protected claim yet another victim, film
75         // at 11. (I could simply cut-n-paste all that good code from
76         // BinaryReader, but two wrongs do not make a right)
77
78         /// <summary>
79         /// Construct a NetworkBinaryReader over the given input stream.
80         /// </summary>
81         public NetworkBinaryReader(Stream input): base(input) {}
82
83         /// <summary>
84         /// Construct a NetworkBinaryReader over the given input
85         /// stream, reading strings using the given encoding.
86         /// </summary>
87         public NetworkBinaryReader(Stream input, Encoding encoding): base(input, encoding) {}
88
89         /// <summary>
90         /// Override BinaryReader's method for network-order.
91         /// </summary>
92         public override short ReadInt16() {
93             uint i = base.ReadUInt16();
94             return (short) (((i & 0xFF00) >> 8) |
95                             ((i & 0x00FF) << 8));
96         }
97
98         /// <summary>
99         /// Override BinaryReader's method for network-order.
100         /// </summary>
101         public override ushort ReadUInt16() {
102             uint i = base.ReadUInt16();
103             return (ushort) (((i & 0xFF00) >> 8) |
104                              ((i & 0x00FF) << 8));
105         }
106
107         /// <summary>
108         /// Override BinaryReader's method for network-order.
109         /// </summary>
110         public override int ReadInt32() {
111             uint i = base.ReadUInt32();
112             return (int) (((i & 0xFF000000) >> 24) |
113                           ((i & 0x00FF0000) >> 8) |
114                           ((i & 0x0000FF00) << 8) |
115                           ((i & 0x000000FF) << 24));
116         }
117
118         /// <summary>
119         /// Override BinaryReader's method for network-order.
120         /// </summary>
121         public override uint ReadUInt32() {
122             uint i = base.ReadUInt32();
123             return (((i & 0xFF000000) >> 24) |
124                     ((i & 0x00FF0000) >> 8) |
125                     ((i & 0x0000FF00) << 8) |
126                     ((i & 0x000000FF) << 24));
127         }
128
129         /// <summary>
130         /// Override BinaryReader's method for network-order.
131         /// </summary>
132         public override long ReadInt64() {
133             ulong i = base.ReadUInt64();
134             return (long) (((i & 0xFF00000000000000) >> 56) |
135                            ((i & 0x00FF000000000000) >> 40) |
136                            ((i & 0x0000FF0000000000) >> 24) |
137                            ((i & 0x000000FF00000000) >> 8) |
138                            ((i & 0x00000000FF000000) << 8) |
139                            ((i & 0x0000000000FF0000) << 24) |
140                            ((i & 0x000000000000FF00) << 40) |
141                            ((i & 0x00000000000000FF) << 56));
142         }
143
144         /// <summary>
145         /// Override BinaryReader's method for network-order.
146         /// </summary>
147         public override ulong ReadUInt64() {
148             ulong i = base.ReadUInt64();
149             return (((i & 0xFF00000000000000) >> 56) |
150                     ((i & 0x00FF000000000000) >> 40) |
151                     ((i & 0x0000FF0000000000) >> 24) |
152                     ((i & 0x000000FF00000000) >> 8) |
153                     ((i & 0x00000000FF000000) << 8) |
154                     ((i & 0x0000000000FF0000) << 24) |
155                     ((i & 0x000000000000FF00) << 40) |
156                     ((i & 0x00000000000000FF) << 56));
157         }
158
159         /// <summary>
160         /// Override BinaryReader's method for network-order.
161         /// </summary>
162         public override float ReadSingle() {
163             byte[] bytes = ReadBytes(4);
164             byte temp;
165             temp = bytes[0]; bytes[0] = bytes[3]; bytes[3] = temp;
166             temp = bytes[1]; bytes[1] = bytes[2]; bytes[2] = temp;
167             return TemporaryBinaryReader(bytes).ReadSingle();
168         }
169
170         /// <summary>
171         /// Override BinaryReader's method for network-order.
172         /// </summary>
173         public override double ReadDouble() {
174             byte[] bytes = ReadBytes(8);
175             byte temp;
176             temp = bytes[0]; bytes[0] = bytes[7]; bytes[7] = temp;
177             temp = bytes[1]; bytes[1] = bytes[6]; bytes[6] = temp;
178             temp = bytes[2]; bytes[2] = bytes[5]; bytes[5] = temp;
179             temp = bytes[3]; bytes[3] = bytes[4]; bytes[4] = temp;
180             return TemporaryBinaryReader(bytes).ReadDouble();
181         }
182
183         ///<summary>Helper method for constructing a temporary
184         ///BinaryReader over a byte[].</summary>
185         public static BinaryReader TemporaryBinaryReader(byte[] bytes) {
186             return new BinaryReader(new MemoryStream(bytes));
187         }
188     }
189 }