Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
Todd Lipcon | 53ae9f3 | 2009-12-07 00:42:38 +0000 | [diff] [blame] | 18 | * |
| 19 | * Contains some contributions under the Thrift Software License. |
| 20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for |
| 21 | * details. |
Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 22 | */ |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 23 | |
| 24 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 25 | using System.Text; |
| 26 | using Thrift.Transport; |
| 27 | |
| 28 | namespace Thrift.Protocol |
| 29 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 30 | public class TBinaryProtocol : TProtocol |
| 31 | { |
| 32 | protected const uint VERSION_MASK = 0xffff0000; |
| 33 | protected const uint VERSION_1 = 0x80010000; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 34 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 35 | protected bool strictRead_ = false; |
| 36 | protected bool strictWrite_ = true; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 37 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 38 | #region BinaryProtocol Factory |
| 39 | /** |
| 40 | * Factory |
| 41 | */ |
| 42 | public class Factory : TProtocolFactory { |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 43 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 44 | protected bool strictRead_ = false; |
| 45 | protected bool strictWrite_ = true; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 46 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 47 | public Factory() |
| 48 | :this(false, true) |
| 49 | { |
| 50 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 51 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 52 | public Factory(bool strictRead, bool strictWrite) |
| 53 | { |
| 54 | strictRead_ = strictRead; |
| 55 | strictWrite_ = strictWrite; |
| 56 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 57 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 58 | public TProtocol GetProtocol(TTransport trans) { |
| 59 | return new TBinaryProtocol(trans, strictRead_, strictWrite_); |
| 60 | } |
| 61 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 62 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 63 | #endregion |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 64 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 65 | public TBinaryProtocol(TTransport trans) |
| 66 | : this(trans, false, true) |
| 67 | { |
| 68 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 69 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 70 | public TBinaryProtocol(TTransport trans, bool strictRead, bool strictWrite) |
| 71 | :base(trans) |
| 72 | { |
| 73 | strictRead_ = strictRead; |
| 74 | strictWrite_ = strictWrite; |
| 75 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 76 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 77 | #region Write Methods |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 78 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 79 | public override void WriteMessageBegin(TMessage message) |
| 80 | { |
| 81 | if (strictWrite_) |
| 82 | { |
| 83 | uint version = VERSION_1 | (uint)(message.Type); |
| 84 | WriteI32((int)version); |
| 85 | WriteString(message.Name); |
| 86 | WriteI32(message.SeqID); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | WriteString(message.Name); |
| 91 | WriteByte((sbyte)message.Type); |
| 92 | WriteI32(message.SeqID); |
| 93 | } |
| 94 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 95 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 96 | public override void WriteMessageEnd() |
| 97 | { |
| 98 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 99 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 100 | public override void WriteStructBegin(TStruct struc) |
| 101 | { |
| 102 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 103 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 104 | public override void WriteStructEnd() |
| 105 | { |
| 106 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 107 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 108 | public override void WriteFieldBegin(TField field) |
| 109 | { |
| 110 | WriteByte((sbyte)field.Type); |
| 111 | WriteI16(field.ID); |
| 112 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 113 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 114 | public override void WriteFieldEnd() |
| 115 | { |
| 116 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 117 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 118 | public override void WriteFieldStop() |
| 119 | { |
| 120 | WriteByte((sbyte)TType.Stop); |
| 121 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 122 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 123 | public override void WriteMapBegin(TMap map) |
| 124 | { |
| 125 | WriteByte((sbyte)map.KeyType); |
| 126 | WriteByte((sbyte)map.ValueType); |
| 127 | WriteI32(map.Count); |
| 128 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 129 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 130 | public override void WriteMapEnd() |
| 131 | { |
| 132 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 133 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 134 | public override void WriteListBegin(TList list) |
| 135 | { |
| 136 | WriteByte((sbyte)list.ElementType); |
| 137 | WriteI32(list.Count); |
| 138 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 139 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 140 | public override void WriteListEnd() |
| 141 | { |
| 142 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 143 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 144 | public override void WriteSetBegin(TSet set) |
| 145 | { |
| 146 | WriteByte((sbyte)set.ElementType); |
| 147 | WriteI32(set.Count); |
| 148 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 149 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 150 | public override void WriteSetEnd() |
| 151 | { |
| 152 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 153 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 154 | public override void WriteBool(bool b) |
| 155 | { |
| 156 | WriteByte(b ? (sbyte)1 : (sbyte)0); |
| 157 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 158 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 159 | private byte[] bout = new byte[1]; |
| 160 | public override void WriteByte(sbyte b) |
| 161 | { |
| 162 | bout[0] = (byte)b; |
| 163 | trans.Write(bout, 0, 1); |
| 164 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 165 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 166 | private byte[] i16out = new byte[2]; |
| 167 | public override void WriteI16(short s) |
| 168 | { |
| 169 | i16out[0] = (byte)(0xff & (s >> 8)); |
| 170 | i16out[1] = (byte)(0xff & s); |
| 171 | trans.Write(i16out, 0, 2); |
| 172 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 173 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 174 | private byte[] i32out = new byte[4]; |
| 175 | public override void WriteI32(int i32) |
| 176 | { |
| 177 | i32out[0] = (byte)(0xff & (i32 >> 24)); |
| 178 | i32out[1] = (byte)(0xff & (i32 >> 16)); |
| 179 | i32out[2] = (byte)(0xff & (i32 >> 8)); |
| 180 | i32out[3] = (byte)(0xff & i32); |
| 181 | trans.Write(i32out, 0, 4); |
| 182 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 183 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 184 | private byte[] i64out = new byte[8]; |
| 185 | public override void WriteI64(long i64) |
| 186 | { |
| 187 | i64out[0] = (byte)(0xff & (i64 >> 56)); |
| 188 | i64out[1] = (byte)(0xff & (i64 >> 48)); |
| 189 | i64out[2] = (byte)(0xff & (i64 >> 40)); |
| 190 | i64out[3] = (byte)(0xff & (i64 >> 32)); |
| 191 | i64out[4] = (byte)(0xff & (i64 >> 24)); |
| 192 | i64out[5] = (byte)(0xff & (i64 >> 16)); |
| 193 | i64out[6] = (byte)(0xff & (i64 >> 8)); |
| 194 | i64out[7] = (byte)(0xff & i64); |
| 195 | trans.Write(i64out, 0, 8); |
| 196 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 197 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 198 | public override void WriteDouble(double d) |
| 199 | { |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 200 | #if !SILVERLIGHT |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 201 | WriteI64(BitConverter.DoubleToInt64Bits(d)); |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 202 | #else |
| 203 | var bytes = BitConverter.GetBytes(d); |
| 204 | WriteI64(BitConverter.ToInt64(bytes, 0)); |
| 205 | #endif |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 206 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 207 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 208 | public override void WriteBinary(byte[] b) |
| 209 | { |
| 210 | WriteI32(b.Length); |
| 211 | trans.Write(b, 0, b.Length); |
| 212 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 213 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 214 | #endregion |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 215 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 216 | #region ReadMethods |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 217 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 218 | public override TMessage ReadMessageBegin() |
| 219 | { |
| 220 | TMessage message = new TMessage(); |
| 221 | int size = ReadI32(); |
| 222 | if (size < 0) |
| 223 | { |
| 224 | uint version = (uint)size & VERSION_MASK; |
| 225 | if (version != VERSION_1) |
| 226 | { |
| 227 | throw new TProtocolException(TProtocolException.BAD_VERSION, "Bad version in ReadMessageBegin: " + version); |
| 228 | } |
| 229 | message.Type = (TMessageType)(size & 0x000000ff); |
| 230 | message.Name = ReadString(); |
| 231 | message.SeqID = ReadI32(); |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | if (strictRead_) |
| 236 | { |
| 237 | throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in readMessageBegin, old client?"); |
| 238 | } |
| 239 | message.Name = ReadStringBody(size); |
| 240 | message.Type = (TMessageType)ReadByte(); |
| 241 | message.SeqID = ReadI32(); |
| 242 | } |
| 243 | return message; |
| 244 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 245 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 246 | public override void ReadMessageEnd() |
| 247 | { |
| 248 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 249 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 250 | public override TStruct ReadStructBegin() |
| 251 | { |
| 252 | return new TStruct(); |
| 253 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 254 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 255 | public override void ReadStructEnd() |
| 256 | { |
| 257 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 258 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 259 | public override TField ReadFieldBegin() |
| 260 | { |
| 261 | TField field = new TField(); |
| 262 | field.Type = (TType)ReadByte(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 263 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 264 | if (field.Type != TType.Stop) |
| 265 | { |
| 266 | field.ID = ReadI16(); |
| 267 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 268 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 269 | return field; |
| 270 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 271 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 272 | public override void ReadFieldEnd() |
| 273 | { |
| 274 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 275 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 276 | public override TMap ReadMapBegin() |
| 277 | { |
| 278 | TMap map = new TMap(); |
| 279 | map.KeyType = (TType)ReadByte(); |
| 280 | map.ValueType = (TType)ReadByte(); |
| 281 | map.Count = ReadI32(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 282 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 283 | return map; |
| 284 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 285 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 286 | public override void ReadMapEnd() |
| 287 | { |
| 288 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 289 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 290 | public override TList ReadListBegin() |
| 291 | { |
| 292 | TList list = new TList(); |
| 293 | list.ElementType = (TType)ReadByte(); |
| 294 | list.Count = ReadI32(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 295 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 296 | return list; |
| 297 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 298 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 299 | public override void ReadListEnd() |
| 300 | { |
| 301 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 302 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 303 | public override TSet ReadSetBegin() |
| 304 | { |
| 305 | TSet set = new TSet(); |
| 306 | set.ElementType = (TType)ReadByte(); |
| 307 | set.Count = ReadI32(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 308 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 309 | return set; |
| 310 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 311 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 312 | public override void ReadSetEnd() |
| 313 | { |
| 314 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 315 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 316 | public override bool ReadBool() |
| 317 | { |
| 318 | return ReadByte() == 1; |
| 319 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 320 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 321 | private byte[] bin = new byte[1]; |
| 322 | public override sbyte ReadByte() |
| 323 | { |
| 324 | ReadAll(bin, 0, 1); |
| 325 | return (sbyte)bin[0]; |
| 326 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 327 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 328 | private byte[] i16in = new byte[2]; |
| 329 | public override short ReadI16() |
| 330 | { |
| 331 | ReadAll(i16in, 0, 2); |
| 332 | return (short)(((i16in[0] & 0xff) << 8) | ((i16in[1] & 0xff))); |
| 333 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 334 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 335 | private byte[] i32in = new byte[4]; |
| 336 | public override int ReadI32() |
| 337 | { |
| 338 | ReadAll(i32in, 0, 4); |
| 339 | return (int)(((i32in[0] & 0xff) << 24) | ((i32in[1] & 0xff) << 16) | ((i32in[2] & 0xff) << 8) | ((i32in[3] & 0xff))); |
| 340 | } |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 341 | |
| 342 | #pragma warning disable 675 |
| 343 | |
Jens Geyer | ee353e6 | 2013-07-06 09:28:49 +0200 | [diff] [blame] | 344 | private byte[] i64in = new byte[8]; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 345 | public override long ReadI64() |
| 346 | { |
| 347 | ReadAll(i64in, 0, 8); |
Jake Farrell | 98f9377 | 2012-10-20 16:47:51 +0000 | [diff] [blame] | 348 | unchecked { |
| 349 | return (long)( |
| 350 | ((long)(i64in[0] & 0xff) << 56) | |
| 351 | ((long)(i64in[1] & 0xff) << 48) | |
| 352 | ((long)(i64in[2] & 0xff) << 40) | |
| 353 | ((long)(i64in[3] & 0xff) << 32) | |
| 354 | ((long)(i64in[4] & 0xff) << 24) | |
| 355 | ((long)(i64in[5] & 0xff) << 16) | |
| 356 | ((long)(i64in[6] & 0xff) << 8) | |
| 357 | ((long)(i64in[7] & 0xff))); |
| 358 | } |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | #pragma warning restore 675 |
| 362 | |
Jens Geyer | ee353e6 | 2013-07-06 09:28:49 +0200 | [diff] [blame] | 363 | public override double ReadDouble() |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 364 | { |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 365 | #if !SILVERLIGHT |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 366 | return BitConverter.Int64BitsToDouble(ReadI64()); |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 367 | #else |
| 368 | var value = ReadI64(); |
| 369 | var bytes = BitConverter.GetBytes(value); |
| 370 | return BitConverter.ToDouble(bytes, 0); |
| 371 | #endif |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 372 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 373 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 374 | public override byte[] ReadBinary() |
| 375 | { |
| 376 | int size = ReadI32(); |
| 377 | byte[] buf = new byte[size]; |
| 378 | trans.ReadAll(buf, 0, size); |
| 379 | return buf; |
| 380 | } |
| 381 | private string ReadStringBody(int size) |
| 382 | { |
| 383 | byte[] buf = new byte[size]; |
| 384 | trans.ReadAll(buf, 0, size); |
| 385 | return Encoding.UTF8.GetString(buf, 0, buf.Length); |
| 386 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 387 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 388 | private int ReadAll(byte[] buf, int off, int len) |
| 389 | { |
| 390 | return trans.ReadAll(buf, off, len); |
| 391 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 392 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 393 | #endregion |
| 394 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 395 | } |