Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +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. |
| 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. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | using System; |
| 25 | using System.Text; |
| 26 | using Thrift.Transport; |
| 27 | using System.Collections; |
| 28 | using System.IO; |
| 29 | using System.Collections.Generic; |
| 30 | |
| 31 | namespace Thrift.Protocol |
| 32 | { |
| 33 | public class TCompactProtocol : TProtocol |
| 34 | { |
| 35 | private static TStruct ANONYMOUS_STRUCT = new TStruct(""); |
| 36 | private static TField TSTOP = new TField("", TType.Stop, (short)0); |
| 37 | |
| 38 | private static byte[] ttypeToCompactType = new byte[16]; |
| 39 | |
| 40 | private const byte PROTOCOL_ID = 0x82; |
| 41 | private const byte VERSION = 1; |
| 42 | private const byte VERSION_MASK = 0x1f; // 0001 1111 |
| 43 | private const byte TYPE_MASK = 0xE0; // 1110 0000 |
Jens Geyer | a86886e | 2014-09-17 22:25:48 +0200 | [diff] [blame] | 44 | private const byte TYPE_BITS = 0x07; // 0000 0111 |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 45 | private const int TYPE_SHIFT_AMOUNT = 5; |
| 46 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 47 | /** |
| 48 | * All of the on-wire type codes. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 49 | */ |
| 50 | private static class Types |
| 51 | { |
| 52 | public const byte STOP = 0x00; |
| 53 | public const byte BOOLEAN_TRUE = 0x01; |
| 54 | public const byte BOOLEAN_FALSE = 0x02; |
| 55 | public const byte BYTE = 0x03; |
| 56 | public const byte I16 = 0x04; |
| 57 | public const byte I32 = 0x05; |
| 58 | public const byte I64 = 0x06; |
| 59 | public const byte DOUBLE = 0x07; |
| 60 | public const byte BINARY = 0x08; |
| 61 | public const byte LIST = 0x09; |
| 62 | public const byte SET = 0x0A; |
| 63 | public const byte MAP = 0x0B; |
| 64 | public const byte STRUCT = 0x0C; |
| 65 | } |
| 66 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 67 | /** |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 68 | * Used to keep track of the last field for the current and previous structs, |
| 69 | * so we can do the delta stuff. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 70 | */ |
| 71 | private Stack<short> lastField_ = new Stack<short>(15); |
| 72 | |
| 73 | private short lastFieldId_ = 0; |
| 74 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 75 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 76 | * If we encounter a boolean field begin, save the TField here so it can |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 77 | * have the value incorporated. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 78 | */ |
| 79 | private Nullable<TField> booleanField_; |
| 80 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 81 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 82 | * If we Read a field header, and it's a boolean field, save the boolean |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 83 | * value here so that ReadBool can use it. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 84 | */ |
| 85 | private Nullable<Boolean> boolValue_; |
| 86 | |
| 87 | |
| 88 | #region CompactProtocol Factory |
| 89 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 90 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 91 | * Factory |
| 92 | */ |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 93 | public class Factory : TProtocolFactory |
| 94 | { |
| 95 | public Factory() { } |
| 96 | |
| 97 | public TProtocol GetProtocol(TTransport trans) |
| 98 | { |
| 99 | return new TCompactProtocol(trans); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #endregion |
| 104 | |
| 105 | public TCompactProtocol(TTransport trans) |
| 106 | : base(trans) |
| 107 | { |
| 108 | ttypeToCompactType[(int)TType.Stop] = Types.STOP; |
| 109 | ttypeToCompactType[(int)TType.Bool] = Types.BOOLEAN_TRUE; |
| 110 | ttypeToCompactType[(int)TType.Byte] = Types.BYTE; |
| 111 | ttypeToCompactType[(int)TType.I16] = Types.I16; |
| 112 | ttypeToCompactType[(int)TType.I32] = Types.I32; |
| 113 | ttypeToCompactType[(int)TType.I64] = Types.I64; |
| 114 | ttypeToCompactType[(int)TType.Double] = Types.DOUBLE; |
| 115 | ttypeToCompactType[(int)TType.String] = Types.BINARY; |
| 116 | ttypeToCompactType[(int)TType.List] = Types.LIST; |
| 117 | ttypeToCompactType[(int)TType.Set] = Types.SET; |
| 118 | ttypeToCompactType[(int)TType.Map] = Types.MAP; |
| 119 | ttypeToCompactType[(int)TType.Struct] = Types.STRUCT; |
| 120 | } |
| 121 | |
| 122 | public void reset() |
| 123 | { |
| 124 | lastField_.Clear(); |
| 125 | lastFieldId_ = 0; |
| 126 | } |
| 127 | |
| 128 | #region Write Methods |
| 129 | |
| 130 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 131 | /** |
| 132 | * Writes a byte without any possibility of all that field header nonsense. |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 133 | * Used internally by other writing methods that know they need to Write a byte. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 134 | */ |
| 135 | private byte[] byteDirectBuffer = new byte[1]; |
| 136 | private void WriteByteDirect(byte b) |
| 137 | { |
| 138 | byteDirectBuffer[0] = b; |
| 139 | trans.Write(byteDirectBuffer); |
| 140 | } |
| 141 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 142 | /** |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 143 | * Writes a byte without any possibility of all that field header nonsense. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 144 | */ |
| 145 | private void WriteByteDirect(int n) |
| 146 | { |
| 147 | WriteByteDirect((byte)n); |
| 148 | } |
| 149 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 150 | /** |
| 151 | * Write an i32 as a varint. Results in 1-5 bytes on the wire. |
| 152 | * TODO: make a permanent buffer like WriteVarint64? |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 153 | */ |
| 154 | byte[] i32buf = new byte[5]; |
| 155 | private void WriteVarint32(uint n) |
| 156 | { |
| 157 | int idx = 0; |
| 158 | while (true) |
| 159 | { |
| 160 | if ((n & ~0x7F) == 0) |
| 161 | { |
| 162 | i32buf[idx++] = (byte)n; |
| 163 | // WriteByteDirect((byte)n); |
| 164 | break; |
| 165 | // return; |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | i32buf[idx++] = (byte)((n & 0x7F) | 0x80); |
| 170 | // WriteByteDirect((byte)((n & 0x7F) | 0x80)); |
| 171 | n >>= 7; |
| 172 | } |
| 173 | } |
| 174 | trans.Write(i32buf, 0, idx); |
| 175 | } |
| 176 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 177 | /** |
| 178 | * Write a message header to the wire. Compact Protocol messages contain the |
| 179 | * protocol version so we can migrate forwards in the future if need be. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 180 | */ |
| 181 | public override void WriteMessageBegin(TMessage message) |
| 182 | { |
| 183 | WriteByteDirect(PROTOCOL_ID); |
| 184 | WriteByteDirect((byte)((VERSION & VERSION_MASK) | ((((uint)message.Type) << TYPE_SHIFT_AMOUNT) & TYPE_MASK))); |
| 185 | WriteVarint32((uint)message.SeqID); |
| 186 | WriteString(message.Name); |
| 187 | } |
| 188 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 189 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 190 | * Write a struct begin. This doesn't actually put anything on the wire. We |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 191 | * use it as an opportunity to put special placeholder markers on the field |
| 192 | * stack so we can get the field id deltas correct. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 193 | */ |
| 194 | public override void WriteStructBegin(TStruct strct) |
| 195 | { |
| 196 | lastField_.Push(lastFieldId_); |
| 197 | lastFieldId_ = 0; |
| 198 | } |
| 199 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 200 | /** |
| 201 | * Write a struct end. This doesn't actually put anything on the wire. We use |
| 202 | * this as an opportunity to pop the last field from the current struct off |
| 203 | * of the field stack. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 204 | */ |
| 205 | public override void WriteStructEnd() |
| 206 | { |
| 207 | lastFieldId_ = lastField_.Pop(); |
| 208 | } |
| 209 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 210 | /** |
| 211 | * Write a field header containing the field id and field type. If the |
| 212 | * difference between the current field id and the last one is small (< 15), |
| 213 | * then the field id will be encoded in the 4 MSB as a delta. Otherwise, the |
| 214 | * field id will follow the type header as a zigzag varint. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 215 | */ |
| 216 | public override void WriteFieldBegin(TField field) |
| 217 | { |
| 218 | if (field.Type == TType.Bool) |
| 219 | { |
| 220 | // we want to possibly include the value, so we'll wait. |
| 221 | booleanField_ = field; |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | WriteFieldBeginInternal(field, 0xFF); |
| 226 | } |
| 227 | } |
| 228 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 229 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 230 | * The workhorse of WriteFieldBegin. It has the option of doing a |
| 231 | * 'type override' of the type header. This is used specifically in the |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 232 | * boolean field case. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 233 | */ |
| 234 | private void WriteFieldBeginInternal(TField field, byte typeOverride) |
| 235 | { |
| 236 | // short lastField = lastField_.Pop(); |
| 237 | |
| 238 | // if there's a type override, use that. |
| 239 | byte typeToWrite = typeOverride == 0xFF ? getCompactType(field.Type) : typeOverride; |
| 240 | |
| 241 | // check if we can use delta encoding for the field id |
| 242 | if (field.ID > lastFieldId_ && field.ID - lastFieldId_ <= 15) |
| 243 | { |
| 244 | // Write them together |
| 245 | WriteByteDirect((field.ID - lastFieldId_) << 4 | typeToWrite); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | // Write them separate |
| 250 | WriteByteDirect(typeToWrite); |
| 251 | WriteI16(field.ID); |
| 252 | } |
| 253 | |
| 254 | lastFieldId_ = field.ID; |
| 255 | // lastField_.push(field.id); |
| 256 | } |
| 257 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 258 | /** |
| 259 | * Write the STOP symbol so we know there are no more fields in this struct. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 260 | */ |
| 261 | public override void WriteFieldStop() |
| 262 | { |
| 263 | WriteByteDirect(Types.STOP); |
| 264 | } |
| 265 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 266 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 267 | * Write a map header. If the map is empty, omit the key and value type |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 268 | * headers, as we don't need any additional information to skip it. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 269 | */ |
| 270 | public override void WriteMapBegin(TMap map) |
| 271 | { |
| 272 | if (map.Count == 0) |
| 273 | { |
| 274 | WriteByteDirect(0); |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | WriteVarint32((uint)map.Count); |
| 279 | WriteByteDirect(getCompactType(map.KeyType) << 4 | getCompactType(map.ValueType)); |
| 280 | } |
| 281 | } |
| 282 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 283 | /** |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 284 | * Write a list header. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 285 | */ |
| 286 | public override void WriteListBegin(TList list) |
| 287 | { |
| 288 | WriteCollectionBegin(list.ElementType, list.Count); |
| 289 | } |
| 290 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 291 | /** |
| 292 | * Write a set header. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 293 | */ |
| 294 | public override void WriteSetBegin(TSet set) |
| 295 | { |
| 296 | WriteCollectionBegin(set.ElementType, set.Count); |
| 297 | } |
| 298 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 299 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 300 | * Write a boolean value. Potentially, this could be a boolean field, in |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 301 | * which case the field header info isn't written yet. If so, decide what the |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 302 | * right type header is for the value and then Write the field header. |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 303 | * Otherwise, Write a single byte. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 304 | */ |
| 305 | public override void WriteBool(Boolean b) |
| 306 | { |
| 307 | if (booleanField_ != null) |
| 308 | { |
| 309 | // we haven't written the field header yet |
| 310 | WriteFieldBeginInternal(booleanField_.Value, b ? Types.BOOLEAN_TRUE : Types.BOOLEAN_FALSE); |
| 311 | booleanField_ = null; |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | // we're not part of a field, so just Write the value. |
| 316 | WriteByteDirect(b ? Types.BOOLEAN_TRUE : Types.BOOLEAN_FALSE); |
| 317 | } |
| 318 | } |
| 319 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 320 | /** |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 321 | * Write a byte. Nothing to see here! |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 322 | */ |
| 323 | public override void WriteByte(sbyte b) |
| 324 | { |
| 325 | WriteByteDirect((byte)b); |
| 326 | } |
| 327 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 328 | /** |
| 329 | * Write an I16 as a zigzag varint. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 330 | */ |
| 331 | public override void WriteI16(short i16) |
| 332 | { |
| 333 | WriteVarint32(intToZigZag(i16)); |
| 334 | } |
| 335 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 336 | /** |
| 337 | * Write an i32 as a zigzag varint. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 338 | */ |
| 339 | public override void WriteI32(int i32) |
| 340 | { |
| 341 | WriteVarint32(intToZigZag(i32)); |
| 342 | } |
| 343 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 344 | /** |
| 345 | * Write an i64 as a zigzag varint. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 346 | */ |
| 347 | public override void WriteI64(long i64) |
| 348 | { |
| 349 | WriteVarint64(longToZigzag(i64)); |
| 350 | } |
| 351 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 352 | /** |
| 353 | * Write a double to the wire as 8 bytes. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 354 | */ |
| 355 | public override void WriteDouble(double dub) |
| 356 | { |
| 357 | byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 358 | fixedLongToBytes(BitConverter.DoubleToInt64Bits(dub), data, 0); |
| 359 | trans.Write(data); |
| 360 | } |
| 361 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 362 | /** |
| 363 | * Write a string to the wire with a varint size preceding. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 364 | */ |
| 365 | public override void WriteString(String str) |
| 366 | { |
| 367 | byte[] bytes = UTF8Encoding.UTF8.GetBytes(str); |
| 368 | WriteBinary(bytes, 0, bytes.Length); |
| 369 | } |
| 370 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 371 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 372 | * Write a byte array, using a varint for the size. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 373 | */ |
| 374 | public override void WriteBinary(byte[] bin) |
| 375 | { |
| 376 | WriteBinary(bin, 0, bin.Length); |
| 377 | } |
| 378 | |
| 379 | private void WriteBinary(byte[] buf, int offset, int length) |
| 380 | { |
| 381 | WriteVarint32((uint)length); |
| 382 | trans.Write(buf, offset, length); |
| 383 | } |
| 384 | |
| 385 | // |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 386 | // These methods are called by structs, but don't actually have any wire |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 387 | // output or purpose. |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 388 | // |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 389 | |
| 390 | public override void WriteMessageEnd() { } |
| 391 | public override void WriteMapEnd() { } |
| 392 | public override void WriteListEnd() { } |
| 393 | public override void WriteSetEnd() { } |
| 394 | public override void WriteFieldEnd() { } |
| 395 | |
| 396 | // |
| 397 | // Internal writing methods |
| 398 | // |
| 399 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 400 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 401 | * Abstract method for writing the start of lists and sets. List and sets on |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 402 | * the wire differ only by the type indicator. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 403 | */ |
| 404 | protected void WriteCollectionBegin(TType elemType, int size) |
| 405 | { |
| 406 | if (size <= 14) |
| 407 | { |
| 408 | WriteByteDirect(size << 4 | getCompactType(elemType)); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | WriteByteDirect(0xf0 | getCompactType(elemType)); |
| 413 | WriteVarint32((uint)size); |
| 414 | } |
| 415 | } |
| 416 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 417 | /** |
| 418 | * Write an i64 as a varint. Results in 1-10 bytes on the wire. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 419 | */ |
| 420 | byte[] varint64out = new byte[10]; |
| 421 | private void WriteVarint64(ulong n) |
| 422 | { |
| 423 | int idx = 0; |
| 424 | while (true) |
| 425 | { |
| 426 | if ((n & ~(ulong)0x7FL) == 0) |
| 427 | { |
| 428 | varint64out[idx++] = (byte)n; |
| 429 | break; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | varint64out[idx++] = ((byte)((n & 0x7F) | 0x80)); |
| 434 | n >>= 7; |
| 435 | } |
| 436 | } |
| 437 | trans.Write(varint64out, 0, idx); |
| 438 | } |
| 439 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 440 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 441 | * Convert l into a zigzag long. This allows negative numbers to be |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 442 | * represented compactly as a varint. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 443 | */ |
| 444 | private ulong longToZigzag(long n) |
| 445 | { |
Henrique Mendonça | da7982e | 2013-05-31 18:20:42 +0200 | [diff] [blame] | 446 | return (ulong)(n << 1) ^ (ulong)(n >> 63); |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 447 | } |
| 448 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 449 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 450 | * Convert n into a zigzag int. This allows negative numbers to be |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 451 | * represented compactly as a varint. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 452 | */ |
| 453 | private uint intToZigZag(int n) |
| 454 | { |
Henrique Mendonça | da7982e | 2013-05-31 18:20:42 +0200 | [diff] [blame] | 455 | return (uint)(n << 1) ^ (uint)(n >> 31); |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 458 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 459 | * Convert a long into little-endian bytes in buf starting at off and going |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 460 | * until off+7. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 461 | */ |
| 462 | private void fixedLongToBytes(long n, byte[] buf, int off) |
| 463 | { |
| 464 | buf[off + 0] = (byte)(n & 0xff); |
| 465 | buf[off + 1] = (byte)((n >> 8) & 0xff); |
| 466 | buf[off + 2] = (byte)((n >> 16) & 0xff); |
| 467 | buf[off + 3] = (byte)((n >> 24) & 0xff); |
| 468 | buf[off + 4] = (byte)((n >> 32) & 0xff); |
| 469 | buf[off + 5] = (byte)((n >> 40) & 0xff); |
| 470 | buf[off + 6] = (byte)((n >> 48) & 0xff); |
| 471 | buf[off + 7] = (byte)((n >> 56) & 0xff); |
| 472 | } |
| 473 | |
| 474 | #endregion |
| 475 | |
| 476 | #region ReadMethods |
| 477 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 478 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 479 | * Read a message header. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 480 | */ |
| 481 | public override TMessage ReadMessageBegin() |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 482 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 483 | byte protocolId = (byte)ReadByte(); |
| 484 | if (protocolId != PROTOCOL_ID) |
| 485 | { |
| 486 | throw new TProtocolException("Expected protocol id " + PROTOCOL_ID.ToString("X") + " but got " + protocolId.ToString("X")); |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 487 | } |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 488 | byte versionAndType = (byte)ReadByte(); |
| 489 | byte version = (byte)(versionAndType & VERSION_MASK); |
| 490 | if (version != VERSION) |
| 491 | { |
| 492 | throw new TProtocolException("Expected version " + VERSION + " but got " + version); |
| 493 | } |
Jens Geyer | a86886e | 2014-09-17 22:25:48 +0200 | [diff] [blame] | 494 | byte type = (byte)((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS); |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 495 | int seqid = (int)ReadVarint32(); |
| 496 | String messageName = ReadString(); |
| 497 | return new TMessage(messageName, (TMessageType)type, seqid); |
| 498 | } |
| 499 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 500 | /** |
| 501 | * Read a struct begin. There's nothing on the wire for this, but it is our |
| 502 | * opportunity to push a new struct begin marker onto the field stack. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 503 | */ |
| 504 | public override TStruct ReadStructBegin() |
| 505 | { |
| 506 | lastField_.Push(lastFieldId_); |
| 507 | lastFieldId_ = 0; |
| 508 | return ANONYMOUS_STRUCT; |
| 509 | } |
| 510 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 511 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 512 | * Doesn't actually consume any wire data, just removes the last field for |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 513 | * this struct from the field stack. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 514 | */ |
| 515 | public override void ReadStructEnd() |
| 516 | { |
| 517 | // consume the last field we Read off the wire. |
| 518 | lastFieldId_ = lastField_.Pop(); |
| 519 | } |
| 520 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 521 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 522 | * Read a field header off the wire. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 523 | */ |
| 524 | public override TField ReadFieldBegin() |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 525 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 526 | byte type = (byte)ReadByte(); |
| 527 | |
| 528 | // if it's a stop, then we can return immediately, as the struct is over. |
| 529 | if (type == Types.STOP) |
| 530 | { |
| 531 | return TSTOP; |
| 532 | } |
| 533 | |
| 534 | short fieldId; |
| 535 | |
| 536 | // mask off the 4 MSB of the type header. it could contain a field id delta. |
| 537 | short modifier = (short)((type & 0xf0) >> 4); |
| 538 | if (modifier == 0) |
| 539 | { |
| 540 | // not a delta. look ahead for the zigzag varint field id. |
| 541 | fieldId = ReadI16(); |
| 542 | } |
| 543 | else |
| 544 | { |
| 545 | // has a delta. add the delta to the last Read field id. |
| 546 | fieldId = (short)(lastFieldId_ + modifier); |
| 547 | } |
| 548 | |
| 549 | TField field = new TField("", getTType((byte)(type & 0x0f)), fieldId); |
| 550 | |
| 551 | // if this happens to be a boolean field, the value is encoded in the type |
| 552 | if (isBoolType(type)) |
| 553 | { |
| 554 | // save the boolean value in a special instance variable. |
| 555 | boolValue_ = (byte)(type & 0x0f) == Types.BOOLEAN_TRUE ? true : false; |
| 556 | } |
| 557 | |
| 558 | // push the new field onto the field stack so we can keep the deltas going. |
| 559 | lastFieldId_ = field.ID; |
| 560 | return field; |
| 561 | } |
| 562 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 563 | /** |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 564 | * Read a map header off the wire. If the size is zero, skip Reading the key |
| 565 | * and value type. This means that 0-length maps will yield TMaps without the |
| 566 | * "correct" types. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 567 | */ |
| 568 | public override TMap ReadMapBegin() |
| 569 | { |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 570 | int size = (int)ReadVarint32(); |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 571 | byte keyAndValueType = size == 0 ? (byte)0 : (byte)ReadByte(); |
| 572 | return new TMap(getTType((byte)(keyAndValueType >> 4)), getTType((byte)(keyAndValueType & 0xf)), size); |
| 573 | } |
| 574 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 575 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 576 | * Read a list header off the wire. If the list size is 0-14, the size will |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 577 | * be packed into the element type header. If it's a longer list, the 4 MSB |
| 578 | * of the element type header will be 0xF, and a varint will follow with the |
| 579 | * true size. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 580 | */ |
| 581 | public override TList ReadListBegin() |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 582 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 583 | byte size_and_type = (byte)ReadByte(); |
| 584 | int size = (size_and_type >> 4) & 0x0f; |
| 585 | if (size == 15) |
| 586 | { |
| 587 | size = (int)ReadVarint32(); |
| 588 | } |
| 589 | TType type = getTType(size_and_type); |
| 590 | return new TList(type, size); |
| 591 | } |
| 592 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 593 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 594 | * Read a set header off the wire. If the set size is 0-14, the size will |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 595 | * be packed into the element type header. If it's a longer set, the 4 MSB |
| 596 | * of the element type header will be 0xF, and a varint will follow with the |
| 597 | * true size. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 598 | */ |
| 599 | public override TSet ReadSetBegin() |
| 600 | { |
| 601 | return new TSet(ReadListBegin()); |
| 602 | } |
| 603 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 604 | /** |
| 605 | * Read a boolean off the wire. If this is a boolean field, the value should |
| 606 | * already have been Read during ReadFieldBegin, so we'll just consume the |
| 607 | * pre-stored value. Otherwise, Read a byte. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 608 | */ |
| 609 | public override Boolean ReadBool() |
| 610 | { |
| 611 | if (boolValue_ != null) |
| 612 | { |
| 613 | bool result = boolValue_.Value; |
| 614 | boolValue_ = null; |
| 615 | return result; |
| 616 | } |
| 617 | return ReadByte() == Types.BOOLEAN_TRUE; |
| 618 | } |
| 619 | |
| 620 | byte[] byteRawBuf = new byte[1]; |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 621 | /** |
| 622 | * Read a single byte off the wire. Nothing interesting here. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 623 | */ |
| 624 | public override sbyte ReadByte() |
| 625 | { |
| 626 | trans.ReadAll(byteRawBuf, 0, 1); |
| 627 | return (sbyte)byteRawBuf[0]; |
| 628 | } |
| 629 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 630 | /** |
| 631 | * Read an i16 from the wire as a zigzag varint. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 632 | */ |
| 633 | public override short ReadI16() |
| 634 | { |
| 635 | return (short)zigzagToInt(ReadVarint32()); |
| 636 | } |
| 637 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 638 | /** |
| 639 | * Read an i32 from the wire as a zigzag varint. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 640 | */ |
| 641 | public override int ReadI32() |
| 642 | { |
| 643 | return zigzagToInt(ReadVarint32()); |
| 644 | } |
| 645 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 646 | /** |
| 647 | * Read an i64 from the wire as a zigzag varint. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 648 | */ |
| 649 | public override long ReadI64() |
| 650 | { |
| 651 | return zigzagToLong(ReadVarint64()); |
| 652 | } |
| 653 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 654 | /** |
| 655 | * No magic here - just Read a double off the wire. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 656 | */ |
| 657 | public override double ReadDouble() |
| 658 | { |
| 659 | byte[] longBits = new byte[8]; |
| 660 | trans.ReadAll(longBits, 0, 8); |
| 661 | return BitConverter.Int64BitsToDouble(bytesToLong(longBits)); |
| 662 | } |
| 663 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 664 | /** |
| 665 | * Reads a byte[] (via ReadBinary), and then UTF-8 decodes it. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 666 | */ |
| 667 | public override String ReadString() |
| 668 | { |
| 669 | int length = (int)ReadVarint32(); |
| 670 | |
| 671 | if (length == 0) |
| 672 | { |
| 673 | return ""; |
| 674 | } |
| 675 | |
| 676 | return Encoding.UTF8.GetString(ReadBinary(length)); |
| 677 | } |
| 678 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 679 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 680 | * Read a byte[] from the wire. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 681 | */ |
| 682 | public override byte[] ReadBinary() |
| 683 | { |
| 684 | int length = (int)ReadVarint32(); |
| 685 | if (length == 0) return new byte[0]; |
| 686 | |
| 687 | byte[] buf = new byte[length]; |
| 688 | trans.ReadAll(buf, 0, length); |
| 689 | return buf; |
| 690 | } |
| 691 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 692 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 693 | * Read a byte[] of a known length from the wire. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 694 | */ |
| 695 | private byte[] ReadBinary(int length) |
| 696 | { |
| 697 | if (length == 0) return new byte[0]; |
| 698 | |
| 699 | byte[] buf = new byte[length]; |
| 700 | trans.ReadAll(buf, 0, length); |
| 701 | return buf; |
| 702 | } |
| 703 | |
| 704 | // |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 705 | // These methods are here for the struct to call, but don't have any wire |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 706 | // encoding. |
| 707 | // |
| 708 | public override void ReadMessageEnd() { } |
| 709 | public override void ReadFieldEnd() { } |
| 710 | public override void ReadMapEnd() { } |
| 711 | public override void ReadListEnd() { } |
| 712 | public override void ReadSetEnd() { } |
| 713 | |
| 714 | // |
| 715 | // Internal Reading methods |
| 716 | // |
| 717 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 718 | /** |
| 719 | * Read an i32 from the wire as a varint. The MSB of each byte is set |
| 720 | * if there is another byte to follow. This can Read up to 5 bytes. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 721 | */ |
| 722 | private uint ReadVarint32() |
| 723 | { |
| 724 | uint result = 0; |
| 725 | int shift = 0; |
| 726 | while (true) |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 727 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 728 | byte b = (byte)ReadByte(); |
| 729 | result |= (uint)(b & 0x7f) << shift; |
| 730 | if ((b & 0x80) != 0x80) break; |
| 731 | shift += 7; |
| 732 | } |
| 733 | return result; |
| 734 | } |
| 735 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 736 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 737 | * Read an i64 from the wire as a proper varint. The MSB of each byte is set |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 738 | * if there is another byte to follow. This can Read up to 10 bytes. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 739 | */ |
| 740 | private ulong ReadVarint64() |
| 741 | { |
| 742 | int shift = 0; |
| 743 | ulong result = 0; |
| 744 | while (true) |
Jens Geyer | 1c99e70 | 2014-03-17 22:50:39 +0200 | [diff] [blame] | 745 | { |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 746 | byte b = (byte)ReadByte(); |
| 747 | result |= (ulong)(b & 0x7f) << shift; |
| 748 | if ((b & 0x80) != 0x80) break; |
| 749 | shift += 7; |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 750 | } |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 751 | |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 752 | return result; |
| 753 | } |
| 754 | |
| 755 | #endregion |
| 756 | |
| 757 | // |
| 758 | // encoding helpers |
| 759 | // |
| 760 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 761 | /** |
| 762 | * Convert from zigzag int to int. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 763 | */ |
| 764 | private int zigzagToInt(uint n) |
| 765 | { |
| 766 | return (int)(n >> 1) ^ (-(int)(n & 1)); |
| 767 | } |
| 768 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 769 | /** |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 770 | * Convert from zigzag long to long. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 771 | */ |
| 772 | private long zigzagToLong(ulong n) |
| 773 | { |
| 774 | return (long)(n >> 1) ^ (-(long)(n & 1)); |
| 775 | } |
| 776 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 777 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 778 | * Note that it's important that the mask bytes are long literals, |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 779 | * otherwise they'll default to ints, and when you shift an int left 56 bits, |
| 780 | * you just get a messed up int. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 781 | */ |
| 782 | private long bytesToLong(byte[] bytes) |
| 783 | { |
| 784 | return |
| 785 | ((bytes[7] & 0xffL) << 56) | |
| 786 | ((bytes[6] & 0xffL) << 48) | |
| 787 | ((bytes[5] & 0xffL) << 40) | |
| 788 | ((bytes[4] & 0xffL) << 32) | |
| 789 | ((bytes[3] & 0xffL) << 24) | |
| 790 | ((bytes[2] & 0xffL) << 16) | |
| 791 | ((bytes[1] & 0xffL) << 8) | |
| 792 | ((bytes[0] & 0xffL)); |
| 793 | } |
| 794 | |
| 795 | // |
| 796 | // type testing and converting |
| 797 | // |
| 798 | |
| 799 | private Boolean isBoolType(byte b) |
| 800 | { |
| 801 | int lowerNibble = b & 0x0f; |
| 802 | return lowerNibble == Types.BOOLEAN_TRUE || lowerNibble == Types.BOOLEAN_FALSE; |
| 803 | } |
| 804 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 805 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 806 | * Given a TCompactProtocol.Types constant, convert it to its corresponding |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 807 | * TType value. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 808 | */ |
| 809 | private TType getTType(byte type) |
| 810 | { |
| 811 | switch ((byte)(type & 0x0f)) |
| 812 | { |
| 813 | case Types.STOP: |
| 814 | return TType.Stop; |
| 815 | case Types.BOOLEAN_FALSE: |
| 816 | case Types.BOOLEAN_TRUE: |
| 817 | return TType.Bool; |
| 818 | case Types.BYTE: |
| 819 | return TType.Byte; |
| 820 | case Types.I16: |
| 821 | return TType.I16; |
| 822 | case Types.I32: |
| 823 | return TType.I32; |
| 824 | case Types.I64: |
| 825 | return TType.I64; |
| 826 | case Types.DOUBLE: |
| 827 | return TType.Double; |
| 828 | case Types.BINARY: |
| 829 | return TType.String; |
| 830 | case Types.LIST: |
| 831 | return TType.List; |
| 832 | case Types.SET: |
| 833 | return TType.Set; |
| 834 | case Types.MAP: |
| 835 | return TType.Map; |
| 836 | case Types.STRUCT: |
| 837 | return TType.Struct; |
| 838 | default: |
| 839 | throw new TProtocolException("don't know what type: " + (byte)(type & 0x0f)); |
| 840 | } |
| 841 | } |
| 842 | |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 843 | /** |
| 844 | * Given a TType value, find the appropriate TCompactProtocol.Types constant. |
Jens Geyer | f509df9 | 2013-04-25 20:38:55 +0200 | [diff] [blame] | 845 | */ |
| 846 | private byte getCompactType(TType ttype) |
| 847 | { |
| 848 | return ttypeToCompactType[(int)ttype]; |
| 849 | } |
| 850 | } |
Henrique Mendonca | bd5db3a | 2012-10-03 09:26:32 +0000 | [diff] [blame] | 851 | } |