CodedInputStream.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Protocol Buffers for Objective C
  2. //
  3. // Copyright 2010 Booyah Inc.
  4. // Copyright 2008 Cyrus Najmabadi
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // 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, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. @class PBExtensionRegistry;
  18. @class PBUnknownFieldSetBuilder;
  19. @protocol PBMessageBuilder;
  20. @class APPBGeneratedMessage;
  21. /**
  22. * Reads and decodes protocol message fields.
  23. *
  24. * This class contains two kinds of methods: methods that read specific
  25. * protocol message constructs and field types (e.g. {@link #readTag()} and
  26. * {@link #readInt32()}) and methods that read low-level values (e.g.
  27. * {@link #readRawVarint32()} and {@link #readRawBytes}). If you are reading
  28. * encoded protocol messages, you should use the former methods, but if you are
  29. * reading some other format of your own design, use the latter.
  30. *
  31. * @author Cyrus Najmabadi
  32. */
  33. @interface PBCodedInputStream : NSObject {
  34. @private
  35. NSMutableData* buffer;
  36. SInt32 bufferSize;
  37. SInt32 bufferSizeAfterLimit;
  38. SInt32 bufferPos;
  39. NSInputStream* input;
  40. SInt32 lastTag;
  41. /**
  42. * The total number of bytes read before the current buffer. The total
  43. * bytes read up to the current position can be computed as
  44. * {@code totalBytesRetired + bufferPos}.
  45. */
  46. SInt32 totalBytesRetired;
  47. /** The absolute position of the end of the current message. */
  48. SInt32 currentLimit;
  49. /** See setRecursionLimit() */
  50. SInt32 recursionDepth;
  51. SInt32 recursionLimit;
  52. /** See setSizeLimit() */
  53. SInt32 sizeLimit;
  54. }
  55. //用data初始化stream
  56. + (PBCodedInputStream*) streamWithData:(NSData*) data;
  57. //用NSInputStream初始化stream
  58. + (PBCodedInputStream*) streamWithInputStream:(NSInputStream*) input;
  59. /**
  60. * Attempt to read a field tag, returning zero if we have reached EOF.
  61. * Protocol message parsers use this to read tags, since a protocol message
  62. * may legally end wherever a tag occurs, and zero is not a valid tag number.
  63. */
  64. - (SInt32) readTag;
  65. //bf
  66. - (BOOL) refillBuffer:(BOOL) mustSucceed;
  67. //读取Float64类型
  68. - (Float64) readDouble;
  69. //读取Float32类型
  70. - (Float32) readFloat;
  71. //读取UInt64类型
  72. - (SInt64) readUInt64;
  73. //读取UInt32类型
  74. - (SInt32) readUInt32;
  75. //读取Int64类型
  76. - (SInt64) readInt64;
  77. //读取SInt32类型
  78. - (SInt32) readInt32;
  79. //读取SInt64类型
  80. - (SInt64) readFixed64;
  81. //读取SInt32类型
  82. - (SInt32) readFixed32;
  83. //读取SInt32类型
  84. - (SInt32) readEnum;
  85. //读取SInt32类型
  86. - (SInt32) readSFixed32;
  87. //读取SInt64类型
  88. - (SInt64) readSFixed64;
  89. //读取SInt32类型
  90. - (SInt32) readSInt32;
  91. //读取SInt64类型
  92. - (SInt64) readSInt64;
  93. /**
  94. * Read one byte from the input.
  95. *
  96. * @throws InvalidProtocolBuffer The end of the stream or the current
  97. * limit was reached.
  98. */
  99. - (int8_t) readRawByte;
  100. /**
  101. * Read a raw Varint from the stream. If larger than 32 bits, discard the
  102. * upper bits.
  103. */
  104. - (SInt32) readRawVarint32;
  105. //读取Varint64
  106. - (SInt64) readRawVarint64;
  107. //读取LittleEndian32
  108. - (SInt32) readRawLittleEndian32;
  109. //读取LittleEndian64
  110. - (SInt64) readRawLittleEndian64;
  111. /**
  112. * Read a fixed size of bytes from the input.
  113. *
  114. * @throws InvalidProtocolBuffer The end of the stream or the current
  115. * limit was reached.
  116. */
  117. - (NSData*) readRawData:(SInt32) size;
  118. /**
  119. * Reads and discards a single field, given its tag value.
  120. *
  121. * @return {@code false} if the tag is an endgroup tag, in which case
  122. * nothing is skipped. Otherwise, returns {@code true}.
  123. */
  124. - (BOOL) skipField:(SInt32) tag;
  125. /**
  126. * Reads and discards {@code size} bytes.
  127. *
  128. * @throws InvalidProtocolBuffer The end of the stream or the current
  129. * limit was reached.
  130. */
  131. - (void) skipRawData:(SInt32) size;
  132. /**
  133. * Reads and discards an entire message. This will read either until EOF
  134. * or until an endgroup tag, whichever comes first.
  135. */
  136. - (void) skipMessage;
  137. //是否结束
  138. - (BOOL) isAtEnd;
  139. //push限制
  140. - (SInt32) pushLimit:(SInt32) byteLimit;
  141. //recompute限制
  142. - (void) recomputeBufferSizeAfterLimit;
  143. //pop限制
  144. - (void) popLimit:(SInt32) oldLimit;
  145. //bytesUntilLimit
  146. - (SInt32) bytesUntilLimit;
  147. /** Read an embedded message field value from the stream. */
  148. - (void) readMessage:(id<PBMessageBuilder>) builder extensionRegistry:(PBExtensionRegistry*) extensionRegistry;
  149. /** Read an embedded message field value from the stream. */
  150. - (void) readAPMessage:(APPBGeneratedMessage*) message extensionRegistry:(PBExtensionRegistry*) extensionRegistry;
  151. //读取BOOL
  152. - (BOOL) readBool;
  153. //读取string
  154. - (NSString*) readString;
  155. //读取data
  156. - (NSData*) readData;
  157. //- (void) readGroup:(SInt32) fieldNumber builder:(id<PBMessageBuilder>) builder extensionRegistry:(PBExtensionRegistry*) extensionRegistry;
  158. /**
  159. * Reads a {@code group} field value from the stream and merges it into the
  160. * given {@link UnknownFieldSet}.
  161. */
  162. - (void) readUnknownGroup:(SInt32) fieldNumber builder:(PBUnknownFieldSetBuilder*) builder;
  163. /**
  164. * Verifies that the last call to readTag() returned the given tag value.
  165. * This is used to verify that a nested group ended with the correct
  166. * end tag.
  167. *
  168. * @throws InvalidProtocolBuffer {@code value} does not match the
  169. * last tag.
  170. */
  171. - (void) checkLastTagWas:(SInt32) value;
  172. @end