Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 1 | package com.facebook.thrift.protocol; |
| 2 | |
| 3 | import com.facebook.thrift.types.*; |
| 4 | import com.facebook.thrift.TException; |
| 5 | import com.facebook.thrift.transport.TTransport; |
| 6 | |
| 7 | /** |
| 8 | * Utility class with static methods for interacting with protocol data |
| 9 | * streams. |
| 10 | * |
| 11 | * @author Mark Slee <mcslee@facebook.com> |
| 12 | */ |
| 13 | public class TProtocolUtil { |
| 14 | public static int skip(TProtocol prot, TTransport in, TType type) |
| 15 | throws TException { |
| 16 | |
| 17 | switch (type) { |
| 18 | case BYTE: |
| 19 | { |
| 20 | UInt8 b = new UInt8(); |
| 21 | return prot.readByte(in, b); |
| 22 | } |
| 23 | case U32: |
| 24 | { |
| 25 | UInt32 u32 = new UInt32(); |
| 26 | return prot.readU32(in, u32); |
| 27 | } |
| 28 | case I32: |
| 29 | { |
| 30 | Int32 i32 = new Int32(); |
| 31 | return prot.readI32(in, i32); |
| 32 | } |
| 33 | case U64: |
| 34 | { |
| 35 | UInt64 u64 = new UInt64(); |
| 36 | return prot.readU64(in, u64); |
| 37 | } |
| 38 | case I64: |
| 39 | { |
| 40 | Int64 i64 = new Int64(); |
| 41 | return prot.readI64(in, i64); |
| 42 | } |
| 43 | case STRING: |
| 44 | { |
| 45 | TString s = new TString(); |
| 46 | return prot.readString(in, s); |
| 47 | } |
| 48 | case STRUCT: |
| 49 | { |
| 50 | int result = 0; |
| 51 | TString name = new TString(); |
| 52 | TStruct struct = new TStruct(); |
| 53 | TField field = new TField(); |
| 54 | result += prot.readStructBegin(in, struct); |
| 55 | while (true) { |
| 56 | result += prot.readFieldBegin(in, field); |
| 57 | if (field.type.equals(TType.STOP)) { |
| 58 | break; |
| 59 | } |
| 60 | result += skip(prot, in, field.type); |
| 61 | result += prot.readFieldEnd(in); |
| 62 | } |
| 63 | result += prot.readStructEnd(in); |
| 64 | return result; |
| 65 | } |
| 66 | case MAP: |
| 67 | { |
| 68 | int result = 0; |
| 69 | TMap map = new TMap(); |
| 70 | result += prot.readMapBegin(in, map); |
| 71 | for (int i = 0; i < map.size.get(); i++) { |
| 72 | result += skip(prot, in, map.keyType); |
| 73 | result += skip(prot, in, map.valueType); |
| 74 | } |
| 75 | result += prot.readMapEnd(in); |
| 76 | return result; |
| 77 | } |
| 78 | case SET: |
| 79 | { |
| 80 | int result = 0; |
| 81 | TSet set = new TSet(); |
| 82 | result += prot.readSetBegin(in, set); |
| 83 | for (int i = 0; i < set.size.get(); i++) { |
| 84 | result += skip(prot, in, set.elemType); |
| 85 | } |
| 86 | result += prot.readSetEnd(in); |
| 87 | return result; |
| 88 | } |
| 89 | case LIST: |
| 90 | { |
| 91 | int result = 0; |
| 92 | TList list = new TList(); |
| 93 | result += prot.readListBegin(in, list); |
| 94 | for (int i = 0; i < list.size.get(); i++) { |
| 95 | result += skip(prot, in, list.elemType); |
| 96 | } |
| 97 | result += prot.readListEnd(in); |
| 98 | return result; |
| 99 | } |
| 100 | default: |
| 101 | return 0; |
| 102 | } |
| 103 | } |
| 104 | } |