ca9470628fe090eb6c0b9f823103adb739877e52
[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, 2008 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 by LShift Ltd., Cohesive Financial Technologies
42 //   LLC., and Rabbit Technologies Ltd. are Copyright (C) 2007, 2008
43 //   LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit
44 //   Technologies Ltd.;
45 //
46 //   All Rights Reserved.
47 //
48 //   Contributor(s): ______________________________________.
49 //
50 //---------------------------------------------------------------------------
51 using System.IO;
52 using System.Text;
53
54 namespace RabbitMQ.Util {
55     /// <summary>
56     /// Subclass of BinaryReader that reads integers etc in correct network order.
57     /// </summary>
58     /// <remarks>
59     /// <para>
60     /// Kludge to compensate for .NET's broken little-endian-only BinaryReader.
61     /// Relies on BinaryReader always being little-endian.
62     /// </para>
63     /// </remarks>
64     public class NetworkBinaryReader: BinaryReader {
65         // Not particularly efficient. To be more efficient, we could
66         // reuse BinaryReader's implementation details: m_buffer and
67         // FillBuffer, if they weren't private
68         // members. Private/protected claim yet another victim, film
69         // at 11. (I could simply cut-n-paste all that good code from
70         // BinaryReader, but two wrongs do not make a right)
71
72         /// <summary>
73         /// Construct a NetworkBinaryReader over the given input stream.
74         /// </summary>
75         public NetworkBinaryReader(Stream input): base(input) {}
76
77         /// <summary>
78         /// Construct a NetworkBinaryReader over the given input
79         /// stream, reading strings using the given encoding.
80         /// </summary>
81         public NetworkBinaryReader(Stream input, Encoding encoding): base(input, encoding) {}
82
83         /// <summary>
84         /// Override BinaryReader's method for network-order.
85         /// </summary>
86         public override short ReadInt16() {
87             uint i = base.ReadUInt16();
88             return (short) (((i & 0xFF00) >> 8) |
89                             ((i & 0x00FF) << 8));
90         }
91
92         /// <summary>
93         /// Override BinaryReader's method for network-order.
94         /// </summary>
95         public override ushort ReadUInt16() {
96             uint i = base.ReadUInt16();
97             return (ushort) (((i & 0xFF00) >> 8) |
98                              ((i & 0x00FF) << 8));
99         }
100
101         /// <summary>
102         /// Override BinaryReader's method for network-order.
103         /// </summary>
104         public override int ReadInt32() {
105             uint i = base.ReadUInt32();
106             return (int) (((i & 0xFF000000) >> 24) |
107                           ((i & 0x00FF0000) >> 8) |
108                           ((i & 0x0000FF00) << 8) |
109                           ((i & 0x000000FF) << 24));
110         }
111
112         /// <summary>
113         /// Override BinaryReader's method for network-order.
114         /// </summary>
115         public override uint ReadUInt32() {
116             uint i = base.ReadUInt32();
117             return (((i & 0xFF000000) >> 24) |
118                     ((i & 0x00FF0000) >> 8) |
119                     ((i & 0x0000FF00) << 8) |
120                     ((i & 0x000000FF) << 24));
121         }
122
123         /// <summary>
124         /// Override BinaryReader's method for network-order.
125         /// </summary>
126         public override long ReadInt64() {
127             ulong i = base.ReadUInt64();
128             return (long) (((i & 0xFF00000000000000) >> 56) |
129                            ((i & 0x00FF000000000000) >> 40) |
130                            ((i & 0x0000FF0000000000) >> 24) |
131                            ((i & 0x000000FF00000000) >> 8) |
132                            ((i & 0x00000000FF000000) << 8) |
133                            ((i & 0x0000000000FF0000) << 24) |
134                            ((i & 0x000000000000FF00) << 40) |
135                            ((i & 0x00000000000000FF) << 56));
136         }
137
138         /// <summary>
139         /// Override BinaryReader's method for network-order.
140         /// </summary>
141         public override ulong ReadUInt64() {
142             ulong i = base.ReadUInt64();
143             return (((i & 0xFF00000000000000) >> 56) |
144                     ((i & 0x00FF000000000000) >> 40) |
145                     ((i & 0x0000FF0000000000) >> 24) |
146                     ((i & 0x000000FF00000000) >> 8) |
147                     ((i & 0x00000000FF000000) << 8) |
148                     ((i & 0x0000000000FF0000) << 24) |
149                     ((i & 0x000000000000FF00) << 40) |
150                     ((i & 0x00000000000000FF) << 56));
151         }
152
153         /// <summary>
154         /// Override BinaryReader's method for network-order.
155         /// </summary>
156         public override float ReadSingle() {
157             byte[] bytes = ReadBytes(4);
158             byte temp;
159             temp = bytes[0]; bytes[0] = bytes[3]; bytes[3] = temp;
160             temp = bytes[1]; bytes[1] = bytes[2]; bytes[2] = temp;
161             return TemporaryBinaryReader(bytes).ReadSingle();
162         }
163
164         /// <summary>
165         /// Override BinaryReader's method for network-order.
166         /// </summary>
167         public override double ReadDouble() {
168             byte[] bytes = ReadBytes(8);
169             byte temp;
170             temp = bytes[0]; bytes[0] = bytes[7]; bytes[7] = temp;
171             temp = bytes[1]; bytes[1] = bytes[6]; bytes[6] = temp;
172             temp = bytes[2]; bytes[2] = bytes[5]; bytes[5] = temp;
173             temp = bytes[3]; bytes[3] = bytes[4]; bytes[4] = temp;
174             return TemporaryBinaryReader(bytes).ReadDouble();
175         }
176
177         ///<summary>Helper method for constructing a temporary
178         ///BinaryReader over a byte[].</summary>
179         public static BinaryReader TemporaryBinaryReader(byte[] bytes) {
180             return new BinaryReader(new MemoryStream(bytes));
181         }
182     }
183 }