CodedOutputStream.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  18. * Encodes and writes protocol message fields.
  19. *
  20. * <p>This class contains two kinds of methods: methods that write specific
  21. * protocol message constructs and field types (e.g. {@link #writeTag} and
  22. * {@link #writeInt32}) and methods that write low-level values (e.g.
  23. * {@link #writeRawVarint32} and {@link #writeRawBytes}). If you are
  24. * writing encoded protocol messages, you should use the former methods, but if
  25. * you are writing some other format of your own design, use the latter.
  26. *
  27. * <p>This class is totally unsynchronized.
  28. *
  29. * @author Cyrus Najmabadi
  30. */
  31. @class PBUnknownFieldSet;
  32. @class RingBuffer;
  33. @protocol PBMessage;
  34. @interface PBCodedOutputStream : NSObject {
  35. NSOutputStream *output;
  36. RingBuffer *buffer;
  37. }
  38. /** init stream with data. */
  39. + (PBCodedOutputStream*) streamWithData:(NSMutableData*) data;
  40. /** init stream with NSOutputStream. */
  41. + (PBCodedOutputStream*) streamWithOutputStream:(NSOutputStream*) output;
  42. /** init stream with NSOutputStream. */
  43. + (PBCodedOutputStream*) streamWithOutputStream:(NSOutputStream*) output bufferSize:(SInt32) bufferSize;
  44. /**
  45. * Flushes the stream and forces any buffered bytes to be written. This
  46. * does not flush the underlying NSOutputStream. Returns free space in buffer.
  47. */
  48. - (void) flush;
  49. /** Write a single byte. */
  50. - (void) writeRawByte:(uint8_t) value;
  51. /** Encode and write a tag. */
  52. - (void) writeTag:(SInt32) fieldNumber format:(SInt32) format;
  53. /** Write a little-endian 32-bit integer. */
  54. - (void) writeRawLittleEndian32:(SInt32) value;
  55. /** Write a little-endian 64-bit integer. */
  56. - (void) writeRawLittleEndian64:(SInt64) value;
  57. /**
  58. * Encode and write a varint. {@code value} is treated as
  59. * unsigned, so it won't be sign-extended if negative.
  60. */
  61. - (void) writeRawVarint32:(SInt32) value;
  62. /** Encode and write a varint. */
  63. - (void) writeRawVarint64:(SInt64) value;
  64. //- (void) writeRawLittleEndian32:(SInt32) value;
  65. //- (void) writeRawLittleEndian64:(SInt64) value;
  66. /** Write an array of bytes. */
  67. - (void) writeRawData:(const NSData*) data;
  68. /** Write an array of bytes. */
  69. - (void) writeRawData:(const NSData*) data offset:(SInt32) offset length:(SInt32) length;
  70. /** Write an array of bytes. */
  71. - (void) writeData:(SInt32) fieldNumber value:(const NSData*) value;
  72. /** write Float64 value. */
  73. - (void) writeDouble:(SInt32) fieldNumber value:(Float64) value;
  74. /** write Float32 value. */
  75. - (void) writeFloat:(SInt32) fieldNumber value:(Float32) value;
  76. /** write SInt64 value. */
  77. - (void) writeUInt64:(SInt32) fieldNumber value:(SInt64) value;
  78. /** write SInt64 value. */
  79. - (void) writeInt64:(SInt32) fieldNumber value:(SInt64) value;
  80. /** write SInt32 value. */
  81. - (void) writeInt32:(SInt32) fieldNumber value:(SInt32) value;
  82. /** write SInt64 value. */
  83. - (void) writeFixed64:(SInt32) fieldNumber value:(SInt64) value;
  84. /** write SInt32 value. */
  85. - (void) writeFixed32:(SInt32) fieldNumber value:(SInt32) value;
  86. /** write BOOL value. */
  87. - (void) writeBool:(SInt32) fieldNumber value:(BOOL) value;
  88. /** write NSString value. */
  89. - (void) writeString:(SInt32) fieldNumber value:(const NSString*) value;
  90. /** write PBMessage value. */
  91. - (void) writeGroup:(SInt32) fieldNumber value:(const id<PBMessage>) value;
  92. /** write PBUnknownFieldSet value. */
  93. - (void) writeUnknownGroup:(SInt32) fieldNumber value:(const PBUnknownFieldSet*) value;
  94. /** write PBMessage value. */
  95. - (void) writeMessage:(SInt32) fieldNumber value:(const id<PBMessage>) value;
  96. /** write SInt32 value. */
  97. - (void) writeUInt32:(SInt32) fieldNumber value:(SInt32) value;
  98. /** write SInt32 value. */
  99. - (void) writeSFixed32:(SInt32) fieldNumber value:(SInt32) value;
  100. /** write SInt64 value. */
  101. - (void) writeSFixed64:(SInt32) fieldNumber value:(SInt64) value;
  102. /** write SInt32 value. */
  103. - (void) writeSInt32:(SInt32) fieldNumber value:(SInt32) value;
  104. /** write SInt64 value. */
  105. - (void) writeSInt64:(SInt32) fieldNumber value:(SInt64) value;
  106. /** write Float64 value with no tag. */
  107. - (void) writeDoubleNoTag:(Float64) value;
  108. /** write Float32 value with no tag. */
  109. - (void) writeFloatNoTag:(Float32) value;
  110. /** write SInt64 value with no tag. */
  111. - (void) writeUInt64NoTag:(SInt64) value;
  112. /** write SInt64 value with no tag. */
  113. - (void) writeInt64NoTag:(SInt64) value;
  114. /** write SInt32 value with no tag. */
  115. - (void) writeInt32NoTag:(SInt32) value;
  116. /** write SInt64 value with no tag. */
  117. - (void) writeFixed64NoTag:(SInt64) value;
  118. /** write SInt32 value with no tag. */
  119. - (void) writeFixed32NoTag:(SInt32) value;
  120. /** write BOOL value with no tag. */
  121. - (void) writeBoolNoTag:(BOOL) value;
  122. /** write NSString value with no tag. */
  123. - (void) writeStringNoTag:(const NSString*) value;
  124. /** write PBMessage value with no tag. */
  125. - (void) writeGroupNoTag:(SInt32) fieldNumber value:(const id<PBMessage>) value;
  126. /** write PBUnknownFieldSet value with no tag. */
  127. - (void) writeUnknownGroupNoTag:(SInt32) fieldNumber value:(const PBUnknownFieldSet*) value;
  128. /** write PBMessage value with no tag. */
  129. - (void) writeMessageNoTag:(const id<PBMessage>) value;
  130. /** write NSData value with no tag. */
  131. - (void) writeDataNoTag:(const NSData*) value;
  132. /** write SInt32 value with no tag. */
  133. - (void) writeUInt32NoTag:(SInt32) value;
  134. /** write SInt32 value with no tag. */
  135. - (void) writeEnumNoTag:(SInt32) value;
  136. /** write SInt32 value with no tag. */
  137. - (void) writeSFixed32NoTag:(SInt32) value;
  138. /** write SInt64 value with no tag. */
  139. - (void) writeSFixed64NoTag:(SInt64) value;
  140. /** write SInt32 value with no tag. */
  141. - (void) writeSInt32NoTag:(SInt32) value;
  142. /** write SInt64 value with no tag. */
  143. - (void) writeSInt64NoTag:(SInt64) value;
  144. /**
  145. * Write a MessageSet extension field to the stream. For historical reasons,
  146. * the wire format differs from normal fields.
  147. */
  148. - (void) writeMessageSetExtension:(SInt32) fieldNumber value:(const id<PBMessage>) value;
  149. /**
  150. * Write an unparsed MessageSet extension field to the stream. For
  151. * historical reasons, the wire format differs from normal fields.
  152. */
  153. - (void) writeRawMessageSetExtension:(SInt32) fieldNumber value:(const NSData*) value;
  154. /**
  155. * Write an enum field, including tag, to the stream. Caller is responsible
  156. * for converting the enum value to its numeric value.
  157. */
  158. - (void) writeEnum:(SInt32) fieldNumber value:(SInt32) value;
  159. @end