class CodedOutputStream
Defined at line 1036 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
Class which encodes and writes binary data which is composed of varint-
encoded integers and fixed-width pieces. Wraps a ZeroCopyOutputStream.
Most users will not need to deal with CodedOutputStream.
Most methods of CodedOutputStream which return a bool return false if an
underlying I/O error occurs. Once such a failure occurs, the
CodedOutputStream is broken and is no longer useful. The Write* methods do
not return the stream status, but will invalidate the stream if an error
occurs. The client can probe HadError() to determine the status.
Note that every method of CodedOutputStream which writes some data has
a corresponding static "ToArray" version. These versions write directly
to the provided buffer, returning a pointer past the last written byte.
They require that the buffer has sufficient capacity for the encoded data.
This allows an optimization where we check if an output stream has enough
space for an entire message before we start writing and, if there is, we
call only the ToArray methods to avoid doing bound checks for each
individual value.
i.e., in the example above:
CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
int magic_number = 1234;
char text[] = "Hello world!";
int coded_size = sizeof(magic_number) +
CodedOutputStream::VarintSize32(strlen(text)) +
strlen(text);
uint8_t* buffer =
coded_output->GetDirectBufferForNBytesAndAdvance(coded_size);
if (buffer != nullptr) {
// The output stream has enough space in the buffer: write directly to
// the array.
buffer = CodedOutputStream::WriteLittleEndian32ToArray(magic_number,
buffer);
buffer = CodedOutputStream::WriteVarint32ToArray(strlen(text), buffer);
buffer = CodedOutputStream::WriteRawToArray(text, strlen(text), buffer);
} else {
// Make bound-checked writes, which will ask the underlying stream for
// more space as needed.
coded_output->WriteLittleEndian32(magic_number);
coded_output->WriteVarint32(strlen(text));
coded_output->WriteRaw(text, strlen(text));
}
delete coded_output;
Public Methods
void CodedOutputStream (const CodedOutputStream & )
Defined at line 1050 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
CodedOutputStream & operator= (const CodedOutputStream & )
Defined at line 1051 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
bool HadError ()
Returns true if there was an underlying I/O error since this object was
created. On should call Trim before this function in order to catch all
errors.
Defined at line 1060 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void Trim ()
Trims any unused space in the underlying buffer so that its size matches
the number of bytes written by this stream. The underlying buffer will
automatically be trimmed when this stream is destroyed; this call is only
necessary if the underlying buffer is accessed *before* the stream is
destroyed.
Defined at line 1071 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
bool Skip (int count)
Skips a number of bytes, leaving the bytes unmodified in the underlying
buffer. Returns false if an underlying write error occurs. This is
mainly useful with GetDirectBufferPointer().
Note of caution, the skipped bytes may contain uninitialized data. The
caller must make sure that the skipped bytes are properly initialized,
otherwise you might leak bytes from your heap.
Defined at line 1079 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
bool GetDirectBufferPointer (void ** data, int * size)
Sets *data to point directly at the unwritten part of the
CodedOutputStream's underlying buffer, and *size to the size of that
buffer, but does not advance the stream's current position. This will
always either produce a non-empty buffer or return false. If the caller
writes any data to this buffer, it should then call Skip() to skip over
the consumed bytes. This may be useful for implementing external fast
serialization routines for types of data not covered by the
CodedOutputStream interface.
Defined at line 1089 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * GetDirectBufferForNBytesAndAdvance (int size)
If there are at least "size" bytes available in the current buffer,
returns a pointer directly into the buffer and advances over these bytes.
The caller may then write directly into this buffer (e.g. using the
*ToArray static methods) rather than go through CodedOutputStream. If
there are not enough bytes available, returns NULL. The return pointer is
invalidated as soon as any other non-const method of CodedOutputStream
is called.
Defined at line 1100 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteRaw (const void * buffer, int size)
Write raw bytes, copying them from the given buffer.
Defined at line 1105 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteCord (const absl::Cord & cord)
Like WriteString() but writes a Cord.
Defined at line 1128 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteLittleEndian16 (uint16_t value)
Write a 16-bit little-endian integer.
Defined at line 1135 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteLittleEndian32 (uint32_t value)
Write a 32-bit little-endian integer.
Defined at line 1142 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteLittleEndian64 (uint64_t value)
Write a 64-bit little-endian integer.
Defined at line 1149 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteVarint32ToArrayOutOfLine (uint32_t value, uint8_t * target)
Like WriteVarint32ToArray()
Defined at line 1163 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
int ByteCount ()
Returns the total number of bytes written since this object was created.
Defined at line 1213 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void EnableAliasing (bool enabled)
Instructs the CodedOutputStream to allow the underlying
ZeroCopyOutputStream to hold pointers to the original structure instead of
copying, if it supports it (i.e. output->AllowsAliasing() is true). If the
underlying stream does not support aliasing, then enabling it has no
affect. For now, this only affects the behavior of
WriteRawMaybeAliased().
NOTE: It is caller's responsibility to ensure that the chunk of memory
remains live until all of the data has been consumed from the stream.
Defined at line 1226 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void SetSerializationDeterministic (bool value)
Indicate to the serializer whether the user wants deterministic
serialization. The default when this is not called comes from the global
default, controlled by SetDefaultSerializationDeterministic.
What deterministic serialization means is entirely up to the driver of the
serialization process (i.e. the caller of methods like WriteVarint32). In
the case of serializing a proto buffer message using one of the methods of
MessageLite, this means that for a given binary equal messages will always
be serialized to the same bytes. This implies:
* Repeated serialization of a message will return the same bytes.
* Different processes running the same binary (including on different
machines) will serialize equal messages to the same bytes.
Note that this is *not* canonical across languages. It is also unstable
across different builds with intervening message definition changes, due to
unknown fields. Users who need canonical serialization (e.g. persistent
storage in a canonical form, fingerprinting) should define their own
canonicalization specification and implement the serializer using
reflection APIs rather than relying on this API.
Defined at line 1249 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
bool IsSerializationDeterministic ()
Return whether the user wants deterministic serialization. See above.
Defined at line 1254 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
bool IsDefaultSerializationDeterministic ()
Defined at line 1258 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * Cur ()
Defined at line 1266 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void SetCur (uint8_t * ptr)
Defined at line 1267 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
EpsCopyOutputStream * EpsCopy ()
Defined at line 1268 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
template <class Stream, class = typename std::enable_if<std::is_base_of<
ZeroCopyOutputStream, Stream>::value>::type>
void CodedOutputStream (Stream * stream)
Creates a CodedOutputStream that writes to the given `stream`.
The provided stream must publicly derive from `ZeroCopyOutputStream`.
Defined at line 1616 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
template <class Stream, class = typename std::enable_if<std::is_base_of<
ZeroCopyOutputStream, Stream>::value>::type>
void CodedOutputStream (Stream * stream, bool eager_init)
Creates a CodedOutputStream that writes to the given `stream`, and does
an 'eager initialization' of the internal state if `eager_init` is true.
The provided stream must publicly derive from `ZeroCopyOutputStream`.
Defined at line 1623 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void ~CodedOutputStream ()
Destroy the CodedOutputStream and position the underlying
ZeroCopyOutputStream immediately after the last byte written.
void WriteRawMaybeAliased (const void * data, int size)
Like WriteRaw() but will try to write aliased data if aliasing is
turned on.
Defined at line 1798 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteRawToArray (const void * buffer, int size, uint8_t * target)
Like WriteRaw() but writing directly to the target array.
This is _not_ inlined, as the compiler often optimizes memcpy into inline
copy loops. Since this gets called by every field with string or bytes
type, inlining may lead to a significant amount of code bloat, with only a
minor performance gain.
Defined at line 1803 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteString (const std::string & str)
Equivalent to WriteRaw(str.data(), str.size()).
Defined at line 1794 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteStringToArray (const std::string & str, uint8_t * target)
Like WriteString() but writing directly to the target array.
Defined at line 1809 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteStringWithSizeToArray (const std::string & str, uint8_t * target)
Write the varint-encoded size of str followed by str.
uint8_t * WriteCordToArray (const absl::Cord & cord, uint8_t * target)
Like WriteCord() but writing directly to the target array.
uint8_t * WriteLittleEndian16ToArray (uint16_t value, uint8_t * target)
Like WriteLittleEndian16() but writing directly to the target array.
Defined at line 1659 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteLittleEndian32ToArray (uint32_t value, uint8_t * target)
Like WriteLittleEndian32() but writing directly to the target array.
Defined at line 1666 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteLittleEndian64ToArray (uint64_t value, uint8_t * target)
Like WriteLittleEndian64() but writing directly to the target array.
Defined at line 1680 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteVarint32 (uint32_t value)
Write an unsigned integer with Varint encoding. Writing a 32-bit value
is equivalent to casting it to uint64_t and writing it as a 64-bit value,
but may be more efficient.
Defined at line 1701 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteVarint32ToArray (uint32_t value, uint8_t * target)
Like WriteVarint32() but writing directly to the target array.
Defined at line 1640 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteVarint64 (uint64_t value)
Write an unsigned integer with Varint encoding.
Defined at line 1706 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteVarint64ToArray (uint64_t value, uint8_t * target)
Like WriteVarint64() but writing directly to the target array.
Defined at line 1645 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteVarint32SignExtended (int32_t value)
Equivalent to WriteVarint32() except when the value is negative,
in which case it must be sign-extended to a full 10 bytes.
Defined at line 1650 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteVarint32SignExtendedToArray (int32_t value, uint8_t * target)
Like WriteVarint32SignExtended() but writing directly to the target array.
Defined at line 1654 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
void WriteTag (uint32_t value)
This is identical to WriteVarint32(), but optimized for writing tags.
In particular, if the input is a compile-time constant, this method
compiles down to a couple instructions.
Always inline because otherwise the aforementioned optimization can't work,
but GCC by default doesn't want to inline this.
Defined at line 1711 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
uint8_t * WriteTagToArray (uint32_t value, uint8_t * target)
Like WriteTag() but writing directly to the target array.
Defined at line 1715 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
size_t VarintSize32 (uint32_t value)
Returns the number of bytes needed to encode the given value as a varint.
Defined at line 1730 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
size_t VarintSize64 (uint64_t value)
Returns the number of bytes needed to encode the given value as a varint.
Defined at line 1757 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
size_t VarintSize32SignExtended (int32_t value)
If negative, 10 bytes. Otherwise, same as VarintSize32().
Defined at line 1784 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
size_t VarintSize32PlusOne (uint32_t value)
Same as above, plus one. The additional one comes at no compute cost.
Defined at line 1744 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
size_t VarintSize64PlusOne (uint64_t value)
Defined at line 1771 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
size_t VarintSize32SignExtendedPlusOne (int32_t value)
Defined at line 1788 of file ../../third_party/protobuf/src/google/protobuf/io/coded_stream.h
template <typename Func>
void Serialize (const Func & func)
Records
Friends
void CodedOutputStream ()