Mark Slee | 7eb0d63 | 2007-03-01 00:00:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 7 | package com.facebook.thrift.protocol; |
| 8 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 9 | import com.facebook.thrift.TException; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 10 | |
| 11 | /** |
| 12 | * Utility class with static methods for interacting with protocol data |
| 13 | * streams. |
| 14 | * |
| 15 | * @author Mark Slee <mcslee@facebook.com> |
| 16 | */ |
| 17 | public class TProtocolUtil { |
David Reiss | d92ca7d | 2008-03-18 19:32:00 +0000 | [diff] [blame^] | 18 | |
| 19 | /** |
| 20 | * The maximum recursive depth the skip() function will traverse before |
| 21 | * throwing a TException. |
| 22 | */ |
| 23 | private static int maxSkipDepth = Integer.MAX_VALUE; |
| 24 | |
| 25 | /** |
| 26 | * Specifies the maximum recursive depth that the skip function will |
| 27 | * traverse before throwing a TException. This is a global setting, so |
| 28 | * any call to skip in this JVM will enforce this value. |
| 29 | * |
| 30 | * @param depth the maximum recursive depth. A value of 2 would allow |
| 31 | * the skip function to skip a structure or collection with basic children, |
| 32 | * but it would not permit skipping a struct that had a field containing |
| 33 | * a child struct. A value of 1 would only allow skipping of simple |
| 34 | * types and empty structs/collections. |
| 35 | */ |
| 36 | public static void setMaxSkipDepth(int depth) { |
| 37 | maxSkipDepth = depth; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Skips over the next data element from the provided input TProtocol object. |
| 42 | * |
| 43 | * @param prot the protocol object to read from |
| 44 | * @param type the next value will be intepreted as this TType value. |
| 45 | */ |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 46 | public static void skip(TProtocol prot, byte type) |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 47 | throws TException { |
David Reiss | d92ca7d | 2008-03-18 19:32:00 +0000 | [diff] [blame^] | 48 | skip(prot, type, maxSkipDepth); |
| 49 | } |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 50 | |
David Reiss | d92ca7d | 2008-03-18 19:32:00 +0000 | [diff] [blame^] | 51 | /** |
| 52 | * Skips over the next data element from the provided input TProtocol object. |
| 53 | * |
| 54 | * @param prot the protocol object to read from |
| 55 | * @param type the next value will be intepreted as this TType value. |
| 56 | * @param maxDepth this function will only skip complex objects to this |
| 57 | * recursive depth, to prevent Java stack overflow. |
| 58 | */ |
| 59 | public static void skip(TProtocol prot, byte type, int maxDepth) |
| 60 | throws TException { |
| 61 | if (maxDepth <= 0) { |
| 62 | throw new TException("Maximum skip depth exceeded"); |
| 63 | } |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 64 | switch (type) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 65 | case TType.BOOL: |
| 66 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 67 | prot.readBool(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 68 | break; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 69 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 70 | case TType.BYTE: |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 71 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 72 | prot.readByte(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 73 | break; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 74 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 75 | case TType.I16: |
| 76 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 77 | prot.readI16(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 78 | break; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 79 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 80 | case TType.I32: |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 81 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 82 | prot.readI32(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 83 | break; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 84 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 85 | case TType.I64: |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 86 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 87 | prot.readI64(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 88 | break; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 89 | } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 90 | case TType.DOUBLE: |
| 91 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 92 | prot.readDouble(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 93 | break; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 94 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 95 | case TType.STRING: |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 96 | { |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 97 | prot.readBinary(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 98 | break; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 99 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 100 | case TType.STRUCT: |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 101 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 102 | prot.readStructBegin(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 103 | while (true) { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 104 | TField field = prot.readFieldBegin(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 105 | if (field.type == TType.STOP) { |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 106 | break; |
| 107 | } |
David Reiss | d92ca7d | 2008-03-18 19:32:00 +0000 | [diff] [blame^] | 108 | skip(prot, field.type, maxDepth - 1); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 109 | prot.readFieldEnd(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 110 | } |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 111 | prot.readStructEnd(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 112 | break; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 113 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 114 | case TType.MAP: |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 115 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 116 | TMap map = prot.readMapBegin(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 117 | for (int i = 0; i < map.size; i++) { |
David Reiss | d92ca7d | 2008-03-18 19:32:00 +0000 | [diff] [blame^] | 118 | skip(prot, map.keyType, maxDepth - 1); |
| 119 | skip(prot, map.valueType, maxDepth - 1); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 120 | } |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 121 | prot.readMapEnd(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 122 | break; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 123 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 124 | case TType.SET: |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 125 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 126 | TSet set = prot.readSetBegin(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 127 | for (int i = 0; i < set.size; i++) { |
David Reiss | d92ca7d | 2008-03-18 19:32:00 +0000 | [diff] [blame^] | 128 | skip(prot, set.elemType, maxDepth - 1); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 129 | } |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 130 | prot.readSetEnd(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 131 | break; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 132 | } |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 133 | case TType.LIST: |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 134 | { |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 135 | TList list = prot.readListBegin(); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 136 | for (int i = 0; i < list.size; i++) { |
David Reiss | d92ca7d | 2008-03-18 19:32:00 +0000 | [diff] [blame^] | 137 | skip(prot, list.elemType, maxDepth - 1); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 138 | } |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 139 | prot.readListEnd(); |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 140 | break; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 141 | } |
| 142 | default: |
Mark Slee | 780e4ec | 2007-10-02 04:43:31 +0000 | [diff] [blame] | 143 | break; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |