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, 2008 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 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
46 // All Rights Reserved.
48 // Contributor(s): ______________________________________.
50 //---------------------------------------------------------------------------
54 namespace RabbitMQ.Util {
56 /// Subclass of BinaryReader that reads integers etc in correct network order.
60 /// Kludge to compensate for .NET's broken little-endian-only BinaryReader.
61 /// Relies on BinaryReader always being little-endian.
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)
73 /// Construct a NetworkBinaryReader over the given input stream.
75 public NetworkBinaryReader(Stream input): base(input) {}
78 /// Construct a NetworkBinaryReader over the given input
79 /// stream, reading strings using the given encoding.
81 public NetworkBinaryReader(Stream input, Encoding encoding): base(input, encoding) {}
84 /// Override BinaryReader's method for network-order.
86 public override short ReadInt16() {
87 uint i = base.ReadUInt16();
88 return (short) (((i & 0xFF00) >> 8) |
93 /// Override BinaryReader's method for network-order.
95 public override ushort ReadUInt16() {
96 uint i = base.ReadUInt16();
97 return (ushort) (((i & 0xFF00) >> 8) |
102 /// Override BinaryReader's method for network-order.
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));
113 /// Override BinaryReader's method for network-order.
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));
124 /// Override BinaryReader's method for network-order.
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));
139 /// Override BinaryReader's method for network-order.
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));
154 /// Override BinaryReader's method for network-order.
156 public override float ReadSingle() {
157 byte[] bytes = ReadBytes(4);
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();
165 /// Override BinaryReader's method for network-order.
167 public override double ReadDouble() {
168 byte[] bytes = ReadBytes(8);
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();
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));