ProtoBufUtil.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2008 Google Inc. All Rights Reserved.
  2. package com.google.common.io.protocol;
  3. import java.io.*;
  4. /**
  5. * Utility functions for dealing with ProtoBuf objects consolidated from
  6. * previous spot implementations across the codebase.
  7. *
  8. */
  9. public final class ProtoBufUtil {
  10. private ProtoBufUtil() {
  11. }
  12. /** Convenience method to return a string value from of a proto or "". */
  13. public static String getProtoValueOrEmpty(ProtoBuf proto, int tag) {
  14. try {
  15. return (proto != null && proto.has(tag)) ? proto.getString(tag) : "";
  16. } catch (ClassCastException e) {
  17. return "";
  18. }
  19. }
  20. /** Convenience method to return a string value from of a sub-proto or "". */
  21. public static String getSubProtoValueOrEmpty(
  22. ProtoBuf proto, int sub, int tag) {
  23. try {
  24. return getProtoValueOrEmpty(getSubProtoOrNull(proto, sub), tag);
  25. } catch (ClassCastException e) {
  26. return "";
  27. }
  28. }
  29. /** Convenience method to get a subproto if the proto has it. */
  30. public static ProtoBuf getSubProtoOrNull(ProtoBuf proto, int sub) {
  31. return (proto != null && proto.has(sub)) ? proto.getProtoBuf(sub) : null;
  32. }
  33. /**
  34. * Get an int with "tag" from the proto buffer. If the given field can't be
  35. * retrieved, return the provided default value.
  36. *
  37. * @param proto The proto buffer.
  38. * @param tag The tag value that identifies which protocol buffer field to
  39. * retrieve.
  40. * @param defaultValue The value to return if the field can't be retrieved.
  41. * @return The result which should be an integer.
  42. */
  43. public static int getProtoValueOrDefault(ProtoBuf proto, int tag,
  44. int defaultValue) {
  45. try {
  46. return (proto != null && proto.has(tag))
  47. ? proto.getInt(tag) : defaultValue;
  48. } catch (IllegalArgumentException e) {
  49. return defaultValue;
  50. } catch (ClassCastException e) {
  51. return defaultValue;
  52. }
  53. }
  54. /**
  55. * Get an Int with "tag" from the proto buffer.
  56. * If the given field can't be retrieved, return 0.
  57. *
  58. * @param proto The proto buffer.
  59. * @param tag The tag value that identifies which protocol buffer field to
  60. * retrieve.
  61. * @return The result which should be an integer.
  62. */
  63. public static int getProtoValueOrZero(ProtoBuf proto, int tag) {
  64. return getProtoValueOrDefault(proto, tag, 0);
  65. }
  66. /**
  67. * Get an Long with "tag" from the proto buffer.
  68. * If the given field can't be retrieved, return 0.
  69. *
  70. * @param proto The proto buffer.
  71. * @param tag The tag value that identifies which protocol buffer field to
  72. * retrieve.
  73. * @return The result which should be an integer.
  74. */
  75. public static long getProtoLongValueOrZero(ProtoBuf proto, int tag) {
  76. try {
  77. return (proto != null && proto.has(tag)) ? proto.getLong(tag) : 0L;
  78. } catch (IllegalArgumentException e) {
  79. return 0L;
  80. } catch (ClassCastException e) {
  81. return 0L;
  82. }
  83. }
  84. /**
  85. * Get an Int with "tag" from the proto buffer.
  86. * If the given field can't be retrieved, return -1.
  87. *
  88. * @param proto The proto buffer.
  89. * @param tag The tag value that identifies which protocol buffer field to
  90. * retrieve.
  91. * @return The result which should be a long.
  92. */
  93. public static long getProtoValueOrNegativeOne(ProtoBuf proto, int tag) {
  94. try {
  95. return (proto != null && proto.has(tag)) ? proto.getLong(tag) : -1;
  96. } catch (IllegalArgumentException e) {
  97. return -1;
  98. } catch (ClassCastException e) {
  99. return -1;
  100. }
  101. }
  102. /**
  103. * Reads a single protocol buffer from the given input stream. This method is
  104. * provided where the client needs incremental access to the contents of a
  105. * protocol buffer which contains a sequence of protocol buffers.
  106. * <p />
  107. * Please use {@link #getInputStreamForProtoBufResponse} to obtain an input
  108. * stream suitable for this method.
  109. *
  110. * @param umbrellaType the type of the "outer" protocol buffer containing
  111. * the message to read
  112. * @param is the stream to read the protocol buffer from
  113. * @param result the result protocol buffer (must be empty, will be filled
  114. * with the data read and the type will be set)
  115. * @return the tag id of the message, -1 at the end of the stream
  116. */
  117. public static int readNextProtoBuf(ProtoBufType umbrellaType,
  118. InputStream is, ProtoBuf result) throws IOException {
  119. long tagAndType = ProtoBuf.readVarInt(is, true /* permits EOF */);
  120. if (tagAndType == -1) {
  121. return -1;
  122. }
  123. if ((tagAndType & 7) != ProtoBuf.WIRETYPE_LENGTH_DELIMITED) {
  124. throw new IOException("Message expected");
  125. }
  126. int tag = (int) (tagAndType >>> 3);
  127. result.setType((ProtoBufType) umbrellaType.getData(tag));
  128. int length = (int) ProtoBuf.readVarInt(is, false);
  129. result.parse(is, length);
  130. return tag;
  131. }
  132. /**
  133. * A wrapper for <code> getProtoValueOrNegativeOne </code> that drills into
  134. * a sub message returning the long value if it exists, returning -1 if it
  135. * does not.
  136. *
  137. * @param proto The proto buffer.
  138. * @param tag The tag value that identifies which protocol buffer field to
  139. * retrieve.
  140. * @param sub The sub tag value that identifies which protocol buffer
  141. * sub-field to retrieve.n
  142. * @return The result which should be a long.
  143. */
  144. public static long getSubProtoValueOrNegativeOne(
  145. ProtoBuf proto, int sub, int tag) {
  146. try {
  147. return getProtoValueOrNegativeOne(getSubProtoOrNull(proto, sub), tag);
  148. } catch (IllegalArgumentException e) {
  149. return -1;
  150. } catch (ClassCastException e) {
  151. return -1;
  152. }
  153. }
  154. /**
  155. * A wrapper for {@link #getProtoValueOrDefault(ProtoBuf, int, int)} that
  156. * drills into a sub message returning the int value if it exists, returning
  157. * the given default if it does not.
  158. *
  159. * @param proto The proto buffer.
  160. * @param tag The tag value that identifies which protocol buffer field to
  161. * retrieve.
  162. * @param sub The sub tag value that identifies which protocol buffer
  163. * sub-field to retrieve.
  164. * @param defaultValue The value to return if the field is not present.
  165. * @return The result which should be a long.
  166. */
  167. public static int getSubProtoValueOrDefault(ProtoBuf proto, int sub, int tag,
  168. int defaultValue) {
  169. try {
  170. return getProtoValueOrDefault(getSubProtoOrNull(proto, sub), tag,
  171. defaultValue);
  172. } catch (IllegalArgumentException e) {
  173. return defaultValue;
  174. } catch (ClassCastException e) {
  175. return defaultValue;
  176. }
  177. }
  178. /**
  179. * Creates a sub ProtoBuf of the given Protobuf and sets it.
  180. *
  181. * @param proto The proto buffer.
  182. * @param tag The tag value that identifies which protocol buffer field to
  183. * create.
  184. * @return the sub ProtoBuf generated.
  185. */
  186. public static ProtoBuf createProtoBuf(ProtoBuf proto, int tag) {
  187. ProtoBuf child = proto.createGroup(tag);
  188. proto.setProtoBuf(tag, child);
  189. return child;
  190. }
  191. /**
  192. * Creates a sub ProtoBuf of the given Protobuf and adds it.
  193. *
  194. * @param proto The proto buffer.
  195. * @param tag The tag value that identifies which protocol buffer field to
  196. * add.
  197. * @return the sub ProtoBuf generated.
  198. */
  199. public static ProtoBuf addProtoBuf(ProtoBuf proto, int tag) {
  200. ProtoBuf child = proto.createGroup(tag);
  201. proto.addProtoBuf(tag, child);
  202. return child;
  203. }
  204. /**
  205. * Writes the ProtoBuf to the given DataOutput. This is useful for unit
  206. * tests.
  207. *
  208. * @param output The data output to write to.
  209. * @param protoBuf The proto buffer.
  210. */
  211. public static void writeProtoBufToOutput(DataOutput output, ProtoBuf protoBuf)
  212. throws IOException {
  213. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  214. protoBuf.outputTo(baos);
  215. byte[] bytes = baos.toByteArray();
  216. output.writeInt(bytes.length);
  217. output.write(bytes);
  218. }
  219. }