1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
6 //---------------------------------------------------------------------------
7 // Copyright (C) 2007-2010 LShift Ltd., Cohesive Financial
8 // Technologies LLC., and Rabbit Technologies Ltd.
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
14 // http://www.apache.org/licenses/LICENSE-2.0
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 //---------------------------------------------------------------------------
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
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
36 // The Original Code is The RabbitMQ .NET Client.
38 // The Initial Developers of the Original Code are LShift Ltd,
39 // Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
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.
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.
52 // All Rights Reserved.
54 // Contributor(s): ______________________________________.
56 //---------------------------------------------------------------------------
60 namespace RabbitMQ.Util {
62 /// Subclass of BinaryReader that reads integers etc in correct network order.
66 /// Kludge to compensate for .NET's broken little-endian-only BinaryReader.
67 /// Relies on BinaryReader always being little-endian.
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)
79 /// Construct a NetworkBinaryReader over the given input stream.
81 public NetworkBinaryReader(Stream input): base(input) {}
84 /// Construct a NetworkBinaryReader over the given input
85 /// stream, reading strings using the given encoding.
87 public NetworkBinaryReader(Stream input, Encoding encoding): base(input, encoding) {}
90 /// Override BinaryReader's method for network-order.
92 public override short ReadInt16() {
93 uint i = base.ReadUInt16();
94 return (short) (((i & 0xFF00) >> 8) |
99 /// Override BinaryReader's method for network-order.
101 public override ushort ReadUInt16() {
102 uint i = base.ReadUInt16();
103 return (ushort) (((i & 0xFF00) >> 8) |
104 ((i & 0x00FF) << 8));
108 /// Override BinaryReader's method for network-order.
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));
119 /// Override BinaryReader's method for network-order.
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));
130 /// Override BinaryReader's method for network-order.
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));
145 /// Override BinaryReader's method for network-order.
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));
160 /// Override BinaryReader's method for network-order.
162 public override float ReadSingle() {
163 byte[] bytes = ReadBytes(4);
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();
171 /// Override BinaryReader's method for network-order.
173 public override double ReadDouble() {
174 byte[] bytes = ReadBytes(8);
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();
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));