| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419 |
- #region Copyright notice and license
- // Protocol Buffers - Google's data interchange format
- // Copyright 2008 Google Inc. All rights reserved.
- // http://github.com/jskeet/dotnet-protobufs/
- // Original C++/Java/Python code:
- // http://code.google.com/p/protobuf/
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are
- // met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following disclaimer
- // in the documentation and/or other materials provided with the
- // distribution.
- // * Neither the name of Google Inc. nor the names of its
- // contributors may be used to endorse or promote products derived from
- // this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- #endregion
- using System;
- using System.IO;
- using System.Text;
- using Google.ProtocolBuffers.Descriptors;
- namespace Google.ProtocolBuffers
- {
- /// <summary>
- /// Encodes and writes protocol message fields.
- /// </summary>
- /// <remarks>
- /// This class contains two kinds of methods: methods that write specific
- /// protocol message constructs and field types (e.g. WriteTag and
- /// WriteInt32) and methods that write low-level values (e.g.
- /// WriteRawVarint32 and WriteRawBytes). If you are writing encoded protocol
- /// messages, you should use the former methods, but if you are writing some
- /// other format of your own design, use the latter. The names of the former
- /// methods are taken from the protocol buffer type names, not .NET types.
- /// (Hence WriteFloat instead of WriteSingle, and WriteBool instead of WriteBoolean.)
- /// </remarks>
- public sealed partial class CodedOutputStream
- {
- /// <summary>
- /// The buffer size used by CreateInstance(Stream).
- /// </summary>
- public static readonly int DefaultBufferSize = 4096;
- private readonly byte[] buffer;
- private readonly int limit;
- private int position;
- private readonly Stream output;
- #region Construction
- private CodedOutputStream(byte[] buffer, int offset, int length)
- {
- this.output = null;
- this.buffer = buffer;
- this.position = offset;
- this.limit = offset + length;
- }
- private CodedOutputStream(Stream output, byte[] buffer)
- {
- this.output = output;
- this.buffer = buffer;
- this.position = 0;
- this.limit = buffer.Length;
- }
- /// <summary>
- /// Creates a new CodedOutputStream which write to the given stream.
- /// </summary>
- public static CodedOutputStream CreateInstance(Stream output)
- {
- return CreateInstance(output, DefaultBufferSize);
- }
- /// <summary>
- /// Creates a new CodedOutputStream which write to the given stream and uses
- /// the specified buffer size.
- /// </summary>
- public static CodedOutputStream CreateInstance(Stream output, int bufferSize)
- {
- return new CodedOutputStream(output, new byte[bufferSize]);
- }
- /// <summary>
- /// Creates a new CodedOutputStream that writes directly to the given
- /// byte array. If more bytes are written than fit in the array,
- /// OutOfSpaceException will be thrown.
- /// </summary>
- public static CodedOutputStream CreateInstance(byte[] flatArray)
- {
- return CreateInstance(flatArray, 0, flatArray.Length);
- }
- /// <summary>
- /// Creates a new CodedOutputStream that writes directly to the given
- /// byte array slice. If more bytes are written than fit in the array,
- /// OutOfSpaceException will be thrown.
- /// </summary>
- public static CodedOutputStream CreateInstance(byte[] flatArray, int offset, int length)
- {
- return new CodedOutputStream(flatArray, offset, length);
- }
- #endregion
- #region Writing of tags etc
- /// <summary>
- /// Writes a double field value, including tag, to the stream.
- /// </summary>
- public void WriteDouble(int fieldNumber, double value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
- WriteDoubleNoTag(value);
- }
- /// <summary>
- /// Writes a float field value, including tag, to the stream.
- /// </summary>
- public void WriteFloat(int fieldNumber, float value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
- WriteFloatNoTag(value);
- }
- /// <summary>
- /// Writes a uint64 field value, including tag, to the stream.
- /// </summary>
- [CLSCompliant(false)]
- public void WriteUInt64(int fieldNumber, ulong value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawVarint64(value);
- }
- /// <summary>
- /// Writes an int64 field value, including tag, to the stream.
- /// </summary>
- public void WriteInt64(int fieldNumber, long value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawVarint64((ulong) value);
- }
- /// <summary>
- /// Writes an int32 field value, including tag, to the stream.
- /// </summary>
- public void WriteInt32(int fieldNumber, int value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- if (value >= 0)
- {
- WriteRawVarint32((uint) value);
- }
- else
- {
- // Must sign-extend.
- WriteRawVarint64((ulong) value);
- }
- }
- /// <summary>
- /// Writes a fixed64 field value, including tag, to the stream.
- /// </summary>
- [CLSCompliant(false)]
- public void WriteFixed64(int fieldNumber, ulong value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
- WriteRawLittleEndian64(value);
- }
- /// <summary>
- /// Writes a fixed32 field value, including tag, to the stream.
- /// </summary>
- [CLSCompliant(false)]
- public void WriteFixed32(int fieldNumber, uint value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
- WriteRawLittleEndian32(value);
- }
- /// <summary>
- /// Writes a bool field value, including tag, to the stream.
- /// </summary>
- public void WriteBool(int fieldNumber, bool value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawByte(value ? (byte) 1 : (byte) 0);
- }
- /// <summary>
- /// Writes a string field value, including tag, to the stream.
- /// </summary>
- public void WriteString(int fieldNumber, string value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- // Optimise the case where we have enough space to write
- // the string directly to the buffer, which should be common.
- int length = Encoding.UTF8.GetByteCount(value);
- WriteRawVarint32((uint) length);
- if (limit - position >= length)
- {
- Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, position);
- position += length;
- }
- else
- {
- byte[] bytes = Encoding.UTF8.GetBytes(value);
- WriteRawBytes(bytes);
- }
- }
- /// <summary>
- /// Writes a group field value, including tag, to the stream.
- /// </summary>
- public void WriteGroup(int fieldNumber, IMessageLite value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.StartGroup);
- value.WriteTo(this);
- WriteTag(fieldNumber, WireFormat.WireType.EndGroup);
- }
- [Obsolete]
- public void WriteUnknownGroup(int fieldNumber, IMessageLite value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.StartGroup);
- value.WriteTo(this);
- WriteTag(fieldNumber, WireFormat.WireType.EndGroup);
- }
- public void WriteMessage(int fieldNumber, IMessageLite value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32((uint) value.SerializedSize);
- value.WriteTo(this);
- }
- public void WriteBytes(int fieldNumber, ByteString value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32((uint)value.Length);
- value.WriteTo(this);
- }
- [CLSCompliant(false)]
- public void WriteUInt32(int fieldNumber, uint value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawVarint32(value);
- }
- public void WriteEnum(int fieldNumber, int value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawVarint32((uint) value);
- }
- public void WriteSFixed32(int fieldNumber, int value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
- WriteRawLittleEndian32((uint) value);
- }
- public void WriteSFixed64(int fieldNumber, long value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
- WriteRawLittleEndian64((ulong) value);
- }
- public void WriteSInt32(int fieldNumber, int value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawVarint32(EncodeZigZag32(value));
- }
- public void WriteSInt64(int fieldNumber, long value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawVarint64(EncodeZigZag64(value));
- }
- public void WriteMessageSetExtension(int fieldNumber, IMessageLite value)
- {
- WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.StartGroup);
- WriteUInt32(WireFormat.MessageSetField.TypeID, (uint) fieldNumber);
- WriteMessage(WireFormat.MessageSetField.Message, value);
- WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.EndGroup);
- }
- public void WriteRawMessageSetExtension(int fieldNumber, ByteString value)
- {
- WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.StartGroup);
- WriteUInt32(WireFormat.MessageSetField.TypeID, (uint) fieldNumber);
- WriteBytes(WireFormat.MessageSetField.Message, value);
- WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.EndGroup);
- }
- public void WriteField(FieldType fieldType, int fieldNumber, object value)
- {
- switch (fieldType)
- {
- case FieldType.Double:
- WriteDouble(fieldNumber, (double) value);
- break;
- case FieldType.Float:
- WriteFloat(fieldNumber, (float) value);
- break;
- case FieldType.Int64:
- WriteInt64(fieldNumber, (long) value);
- break;
- case FieldType.UInt64:
- WriteUInt64(fieldNumber, (ulong) value);
- break;
- case FieldType.Int32:
- WriteInt32(fieldNumber, (int) value);
- break;
- case FieldType.Fixed64:
- WriteFixed64(fieldNumber, (ulong) value);
- break;
- case FieldType.Fixed32:
- WriteFixed32(fieldNumber, (uint) value);
- break;
- case FieldType.Bool:
- WriteBool(fieldNumber, (bool) value);
- break;
- case FieldType.String:
- WriteString(fieldNumber, (string) value);
- break;
- case FieldType.Group:
- WriteGroup(fieldNumber, (IMessageLite) value);
- break;
- case FieldType.Message:
- WriteMessage(fieldNumber, (IMessageLite) value);
- break;
- case FieldType.Bytes:
- WriteBytes(fieldNumber, (ByteString) value);
- break;
- case FieldType.UInt32:
- WriteUInt32(fieldNumber, (uint) value);
- break;
- case FieldType.SFixed32:
- WriteSFixed32(fieldNumber, (int) value);
- break;
- case FieldType.SFixed64:
- WriteSFixed64(fieldNumber, (long) value);
- break;
- case FieldType.SInt32:
- WriteSInt32(fieldNumber, (int) value);
- break;
- case FieldType.SInt64:
- WriteSInt64(fieldNumber, (long) value);
- break;
- case FieldType.Enum:
- WriteEnum(fieldNumber, ((IEnumLite) value).Number);
- break;
- }
- }
- public void WriteFieldNoTag(FieldType fieldType, object value)
- {
- switch (fieldType)
- {
- case FieldType.Double:
- WriteDoubleNoTag((double) value);
- break;
- case FieldType.Float:
- WriteFloatNoTag((float) value);
- break;
- case FieldType.Int64:
- WriteInt64NoTag((long) value);
- break;
- case FieldType.UInt64:
- WriteUInt64NoTag((ulong) value);
- break;
- case FieldType.Int32:
- WriteInt32NoTag((int) value);
- break;
- case FieldType.Fixed64:
- WriteFixed64NoTag((ulong) value);
- break;
- case FieldType.Fixed32:
- WriteFixed32NoTag((uint) value);
- break;
- case FieldType.Bool:
- WriteBoolNoTag((bool) value);
- break;
- case FieldType.String:
- WriteStringNoTag((string) value);
- break;
- case FieldType.Group:
- WriteGroupNoTag((IMessageLite) value);
- break;
- case FieldType.Message:
- WriteMessageNoTag((IMessageLite) value);
- break;
- case FieldType.Bytes:
- WriteBytesNoTag((ByteString) value);
- break;
- case FieldType.UInt32:
- WriteUInt32NoTag((uint) value);
- break;
- case FieldType.SFixed32:
- WriteSFixed32NoTag((int) value);
- break;
- case FieldType.SFixed64:
- WriteSFixed64NoTag((long) value);
- break;
- case FieldType.SInt32:
- WriteSInt32NoTag((int) value);
- break;
- case FieldType.SInt64:
- WriteSInt64NoTag((long) value);
- break;
- case FieldType.Enum:
- WriteEnumNoTag(((IEnumLite) value).Number);
- break;
- }
- }
- #endregion
- #region Writing of values without tags
- /// <summary>
- /// Writes a double field value, including tag, to the stream.
- /// </summary>
- public void WriteDoubleNoTag(double value)
- {
- // TODO(jonskeet): Test this on different endiannesses
- #if SILVERLIGHT2 || COMPACT_FRAMEWORK_35
- byte[] bytes = BitConverter.GetBytes(value);
- WriteRawBytes(bytes, 0, 8);
- #else
- WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value));
- #endif
- }
- /// <summary>
- /// Writes a float field value, without a tag, to the stream.
- /// </summary>
- public void WriteFloatNoTag(float value)
- {
- // TODO(jonskeet): Test this on different endiannesses
- byte[] rawBytes = BitConverter.GetBytes(value);
- uint asInteger = BitConverter.ToUInt32(rawBytes, 0);
- WriteRawLittleEndian32(asInteger);
- }
- /// <summary>
- /// Writes a uint64 field value, without a tag, to the stream.
- /// </summary>
- [CLSCompliant(false)]
- public void WriteUInt64NoTag(ulong value)
- {
- WriteRawVarint64(value);
- }
- /// <summary>
- /// Writes an int64 field value, without a tag, to the stream.
- /// </summary>
- public void WriteInt64NoTag(long value)
- {
- WriteRawVarint64((ulong) value);
- }
- /// <summary>
- /// Writes an int32 field value, without a tag, to the stream.
- /// </summary>
- public void WriteInt32NoTag(int value)
- {
- if (value >= 0)
- {
- WriteRawVarint32((uint) value);
- }
- else
- {
- // Must sign-extend.
- WriteRawVarint64((ulong) value);
- }
- }
- /// <summary>
- /// Writes a fixed64 field value, without a tag, to the stream.
- /// </summary>
- [CLSCompliant(false)]
- public void WriteFixed64NoTag(ulong value)
- {
- WriteRawLittleEndian64(value);
- }
- /// <summary>
- /// Writes a fixed32 field value, without a tag, to the stream.
- /// </summary>
- [CLSCompliant(false)]
- public void WriteFixed32NoTag(uint value)
- {
- WriteRawLittleEndian32(value);
- }
- /// <summary>
- /// Writes a bool field value, without a tag, to the stream.
- /// </summary>
- public void WriteBoolNoTag(bool value)
- {
- WriteRawByte(value ? (byte) 1 : (byte) 0);
- }
- /// <summary>
- /// Writes a string field value, without a tag, to the stream.
- /// </summary>
- public void WriteStringNoTag(string value)
- {
- // Optimise the case where we have enough space to write
- // the string directly to the buffer, which should be common.
- int length = Encoding.UTF8.GetByteCount(value);
- WriteRawVarint32((uint) length);
- if (limit - position >= length)
- {
- Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, position);
- position += length;
- }
- else
- {
- byte[] bytes = Encoding.UTF8.GetBytes(value);
- WriteRawBytes(bytes);
- }
- }
- /// <summary>
- /// Writes a group field value, without a tag, to the stream.
- /// </summary>
- public void WriteGroupNoTag(IMessageLite value)
- {
- value.WriteTo(this);
- }
- public void WriteMessageNoTag(IMessageLite value)
- {
- WriteRawVarint32((uint) value.SerializedSize);
- value.WriteTo(this);
- }
- public void WriteBytesNoTag(ByteString value)
- {
- WriteRawVarint32((uint)value.Length);
- value.WriteTo(this);
- }
- [CLSCompliant(false)]
- public void WriteUInt32NoTag(uint value)
- {
- WriteRawVarint32(value);
- }
- public void WriteEnumNoTag(int value)
- {
- WriteRawVarint32((uint) value);
- }
- public void WriteSFixed32NoTag(int value)
- {
- WriteRawLittleEndian32((uint) value);
- }
- public void WriteSFixed64NoTag(long value)
- {
- WriteRawLittleEndian64((ulong) value);
- }
- public void WriteSInt32NoTag(int value)
- {
- WriteRawVarint32(EncodeZigZag32(value));
- }
- public void WriteSInt64NoTag(long value)
- {
- WriteRawVarint64(EncodeZigZag64(value));
- }
- #endregion
- #region Underlying writing primitives
- /// <summary>
- /// Encodes and writes a tag.
- /// </summary>
- [CLSCompliant(false)]
- public void WriteTag(int fieldNumber, WireFormat.WireType type)
- {
- WriteRawVarint32(WireFormat.MakeTag(fieldNumber, type));
- }
- private void SlowWriteRawVarint32(uint value)
- {
- while (true)
- {
- if ((value & ~0x7F) == 0)
- {
- WriteRawByte(value);
- return;
- }
- else
- {
- WriteRawByte((value & 0x7F) | 0x80);
- value >>= 7;
- }
- }
- }
- /// <summary>
- /// Writes a 32 bit value as a varint. The fast route is taken when
- /// there's enough buffer space left to whizz through without checking
- /// for each byte; otherwise, we resort to calling WriteRawByte each time.
- /// </summary>
- [CLSCompliant(false)]
- public void WriteRawVarint32(uint value)
- {
- if (position + 5 > limit)
- {
- SlowWriteRawVarint32(value);
- return;
- }
- while (true)
- {
- if ((value & ~0x7F) == 0)
- {
- buffer[position++] = (byte) value;
- return;
- }
- else
- {
- buffer[position++] = (byte) ((value & 0x7F) | 0x80);
- value >>= 7;
- }
- }
- }
- [CLSCompliant(false)]
- public void WriteRawVarint64(ulong value)
- {
- while (true)
- {
- if ((value & ~0x7FUL) == 0)
- {
- WriteRawByte((uint) value);
- return;
- }
- else
- {
- WriteRawByte(((uint) value & 0x7F) | 0x80);
- value >>= 7;
- }
- }
- }
- [CLSCompliant(false)]
- public void WriteRawLittleEndian32(uint value)
- {
- WriteRawByte((byte) value);
- WriteRawByte((byte) (value >> 8));
- WriteRawByte((byte) (value >> 16));
- WriteRawByte((byte) (value >> 24));
- }
- [CLSCompliant(false)]
- public void WriteRawLittleEndian64(ulong value)
- {
- WriteRawByte((byte) value);
- WriteRawByte((byte) (value >> 8));
- WriteRawByte((byte) (value >> 16));
- WriteRawByte((byte) (value >> 24));
- WriteRawByte((byte) (value >> 32));
- WriteRawByte((byte) (value >> 40));
- WriteRawByte((byte) (value >> 48));
- WriteRawByte((byte) (value >> 56));
- }
- public void WriteRawByte(byte value)
- {
- if (position == limit)
- {
- RefreshBuffer();
- }
- buffer[position++] = value;
- }
- [CLSCompliant(false)]
- public void WriteRawByte(uint value)
- {
- WriteRawByte((byte) value);
- }
- /// <summary>
- /// Writes out an array of bytes.
- /// </summary>
- public void WriteRawBytes(byte[] value)
- {
- WriteRawBytes(value, 0, value.Length);
- }
- /// <summary>
- /// Writes out part of an array of bytes.
- /// </summary>
- public void WriteRawBytes(byte[] value, int offset, int length)
- {
- if (limit - position >= length)
- {
- Array.Copy(value, offset, buffer, position, length);
- // We have room in the current buffer.
- position += length;
- }
- else
- {
- // Write extends past current buffer. Fill the rest of this buffer and
- // flush.
- int bytesWritten = limit - position;
- Array.Copy(value, offset, buffer, position, bytesWritten);
- offset += bytesWritten;
- length -= bytesWritten;
- position = limit;
- RefreshBuffer();
- // Now deal with the rest.
- // Since we have an output stream, this is our buffer
- // and buffer offset == 0
- if (length <= limit)
- {
- // Fits in new buffer.
- Array.Copy(value, offset, buffer, 0, length);
- position = length;
- }
- else
- {
- // Write is very big. Let's do it all at once.
- output.Write(value, offset, length);
- }
- }
- }
- #endregion
- #region Size computations
- private const int LittleEndian64Size = 8;
- private const int LittleEndian32Size = 4;
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// double field, including the tag.
- /// </summary>
- public static int ComputeDoubleSize(int fieldNumber, double value)
- {
- return ComputeTagSize(fieldNumber) + LittleEndian64Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// float field, including the tag.
- /// </summary>
- public static int ComputeFloatSize(int fieldNumber, float value)
- {
- return ComputeTagSize(fieldNumber) + LittleEndian32Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// uint64 field, including the tag.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeUInt64Size(int fieldNumber, ulong value)
- {
- return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size(value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// int64 field, including the tag.
- /// </summary>
- public static int ComputeInt64Size(int fieldNumber, long value)
- {
- return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size((ulong) value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// int32 field, including the tag.
- /// </summary>
- public static int ComputeInt32Size(int fieldNumber, int value)
- {
- if (value >= 0)
- {
- return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint) value);
- }
- else
- {
- // Must sign-extend.
- return ComputeTagSize(fieldNumber) + 10;
- }
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// fixed64 field, including the tag.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeFixed64Size(int fieldNumber, ulong value)
- {
- return ComputeTagSize(fieldNumber) + LittleEndian64Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// fixed32 field, including the tag.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeFixed32Size(int fieldNumber, uint value)
- {
- return ComputeTagSize(fieldNumber) + LittleEndian32Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// bool field, including the tag.
- /// </summary>
- public static int ComputeBoolSize(int fieldNumber, bool value)
- {
- return ComputeTagSize(fieldNumber) + 1;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// string field, including the tag.
- /// </summary>
- public static int ComputeStringSize(int fieldNumber, String value)
- {
- int byteArraySize = Encoding.UTF8.GetByteCount(value);
- return ComputeTagSize(fieldNumber) +
- ComputeRawVarint32Size((uint) byteArraySize) +
- byteArraySize;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// group field, including the tag.
- /// </summary>
- public static int ComputeGroupSize(int fieldNumber, IMessageLite value)
- {
- return ComputeTagSize(fieldNumber)*2 + value.SerializedSize;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// group field represented by an UnknownFieldSet, including the tag.
- /// </summary>
- [Obsolete]
- public static int ComputeUnknownGroupSize(int fieldNumber,
- IMessageLite value)
- {
- return ComputeTagSize(fieldNumber)*2 + value.SerializedSize;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// embedded message field, including the tag.
- /// </summary>
- public static int ComputeMessageSize(int fieldNumber, IMessageLite value)
- {
- int size = value.SerializedSize;
- return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint) size) + size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// bytes field, including the tag.
- /// </summary>
- public static int ComputeBytesSize(int fieldNumber, ByteString value)
- {
- return ComputeTagSize(fieldNumber) +
- ComputeRawVarint32Size((uint) value.Length) +
- value.Length;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// uint32 field, including the tag.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeUInt32Size(int fieldNumber, uint value)
- {
- return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size(value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// enum field, including the tag. The caller is responsible for
- /// converting the enum value to its numeric value.
- /// </summary>
- public static int ComputeEnumSize(int fieldNumber, int value)
- {
- return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint) value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// sfixed32 field, including the tag.
- /// </summary>
- public static int ComputeSFixed32Size(int fieldNumber, int value)
- {
- return ComputeTagSize(fieldNumber) + LittleEndian32Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// sfixed64 field, including the tag.
- /// </summary>
- public static int ComputeSFixed64Size(int fieldNumber, long value)
- {
- return ComputeTagSize(fieldNumber) + LittleEndian64Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// sint32 field, including the tag.
- /// </summary>
- public static int ComputeSInt32Size(int fieldNumber, int value)
- {
- return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size(EncodeZigZag32(value));
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// sint64 field, including the tag.
- /// </summary>
- public static int ComputeSInt64Size(int fieldNumber, long value)
- {
- return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size(EncodeZigZag64(value));
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// double field, including the tag.
- /// </summary>
- public static int ComputeDoubleSizeNoTag(double value)
- {
- return LittleEndian64Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// float field, including the tag.
- /// </summary>
- public static int ComputeFloatSizeNoTag(float value)
- {
- return LittleEndian32Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// uint64 field, including the tag.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeUInt64SizeNoTag(ulong value)
- {
- return ComputeRawVarint64Size(value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// int64 field, including the tag.
- /// </summary>
- public static int ComputeInt64SizeNoTag(long value)
- {
- return ComputeRawVarint64Size((ulong) value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// int32 field, including the tag.
- /// </summary>
- public static int ComputeInt32SizeNoTag(int value)
- {
- if (value >= 0)
- {
- return ComputeRawVarint32Size((uint) value);
- }
- else
- {
- // Must sign-extend.
- return 10;
- }
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// fixed64 field, including the tag.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeFixed64SizeNoTag(ulong value)
- {
- return LittleEndian64Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// fixed32 field, including the tag.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeFixed32SizeNoTag(uint value)
- {
- return LittleEndian32Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// bool field, including the tag.
- /// </summary>
- public static int ComputeBoolSizeNoTag(bool value)
- {
- return 1;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// string field, including the tag.
- /// </summary>
- public static int ComputeStringSizeNoTag(String value)
- {
- int byteArraySize = Encoding.UTF8.GetByteCount(value);
- return ComputeRawVarint32Size((uint) byteArraySize) +
- byteArraySize;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// group field, including the tag.
- /// </summary>
- public static int ComputeGroupSizeNoTag(IMessageLite value)
- {
- return value.SerializedSize;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// group field represented by an UnknownFieldSet, including the tag.
- /// </summary>
- [Obsolete]
- public static int ComputeUnknownGroupSizeNoTag(IMessageLite value)
- {
- return value.SerializedSize;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// embedded message field, including the tag.
- /// </summary>
- public static int ComputeMessageSizeNoTag(IMessageLite value)
- {
- int size = value.SerializedSize;
- return ComputeRawVarint32Size((uint) size) + size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// bytes field, including the tag.
- /// </summary>
- public static int ComputeBytesSizeNoTag(ByteString value)
- {
- return ComputeRawVarint32Size((uint) value.Length) +
- value.Length;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// uint32 field, including the tag.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeUInt32SizeNoTag(uint value)
- {
- return ComputeRawVarint32Size(value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// enum field, including the tag. The caller is responsible for
- /// converting the enum value to its numeric value.
- /// </summary>
- public static int ComputeEnumSizeNoTag(int value)
- {
- return ComputeRawVarint32Size((uint) value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// sfixed32 field, including the tag.
- /// </summary>
- public static int ComputeSFixed32SizeNoTag(int value)
- {
- return LittleEndian32Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// sfixed64 field, including the tag.
- /// </summary>
- public static int ComputeSFixed64SizeNoTag(long value)
- {
- return LittleEndian64Size;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// sint32 field, including the tag.
- /// </summary>
- public static int ComputeSInt32SizeNoTag(int value)
- {
- return ComputeRawVarint32Size(EncodeZigZag32(value));
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// sint64 field, including the tag.
- /// </summary>
- public static int ComputeSInt64SizeNoTag(long value)
- {
- return ComputeRawVarint64Size(EncodeZigZag64(value));
- }
- /*
- * Compute the number of bytes that would be needed to encode a
- * MessageSet extension to the stream. For historical reasons,
- * the wire format differs from normal fields.
- */
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// MessageSet extension to the stream. For historical reasons,
- /// the wire format differs from normal fields.
- /// </summary>
- public static int ComputeMessageSetExtensionSize(int fieldNumber, IMessageLite value)
- {
- return ComputeTagSize(WireFormat.MessageSetField.Item)*2 +
- ComputeUInt32Size(WireFormat.MessageSetField.TypeID, (uint) fieldNumber) +
- ComputeMessageSize(WireFormat.MessageSetField.Message, value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode an
- /// unparsed MessageSet extension field to the stream. For
- /// historical reasons, the wire format differs from normal fields.
- /// </summary>
- public static int ComputeRawMessageSetExtensionSize(int fieldNumber, ByteString value)
- {
- return ComputeTagSize(WireFormat.MessageSetField.Item)*2 +
- ComputeUInt32Size(WireFormat.MessageSetField.TypeID, (uint) fieldNumber) +
- ComputeBytesSize(WireFormat.MessageSetField.Message, value);
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a varint.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeRawVarint32Size(uint value)
- {
- if ((value & (0xffffffff << 7)) == 0) return 1;
- if ((value & (0xffffffff << 14)) == 0) return 2;
- if ((value & (0xffffffff << 21)) == 0) return 3;
- if ((value & (0xffffffff << 28)) == 0) return 4;
- return 5;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a varint.
- /// </summary>
- [CLSCompliant(false)]
- public static int ComputeRawVarint64Size(ulong value)
- {
- if ((value & (0xffffffffffffffffL << 7)) == 0) return 1;
- if ((value & (0xffffffffffffffffL << 14)) == 0) return 2;
- if ((value & (0xffffffffffffffffL << 21)) == 0) return 3;
- if ((value & (0xffffffffffffffffL << 28)) == 0) return 4;
- if ((value & (0xffffffffffffffffL << 35)) == 0) return 5;
- if ((value & (0xffffffffffffffffL << 42)) == 0) return 6;
- if ((value & (0xffffffffffffffffL << 49)) == 0) return 7;
- if ((value & (0xffffffffffffffffL << 56)) == 0) return 8;
- if ((value & (0xffffffffffffffffL << 63)) == 0) return 9;
- return 10;
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// field of arbitrary type, including the tag, to the stream.
- /// </summary>
- public static int ComputeFieldSize(FieldType fieldType, int fieldNumber, Object value)
- {
- switch (fieldType)
- {
- case FieldType.Double:
- return ComputeDoubleSize(fieldNumber, (double) value);
- case FieldType.Float:
- return ComputeFloatSize(fieldNumber, (float) value);
- case FieldType.Int64:
- return ComputeInt64Size(fieldNumber, (long) value);
- case FieldType.UInt64:
- return ComputeUInt64Size(fieldNumber, (ulong) value);
- case FieldType.Int32:
- return ComputeInt32Size(fieldNumber, (int) value);
- case FieldType.Fixed64:
- return ComputeFixed64Size(fieldNumber, (ulong) value);
- case FieldType.Fixed32:
- return ComputeFixed32Size(fieldNumber, (uint) value);
- case FieldType.Bool:
- return ComputeBoolSize(fieldNumber, (bool) value);
- case FieldType.String:
- return ComputeStringSize(fieldNumber, (string) value);
- case FieldType.Group:
- return ComputeGroupSize(fieldNumber, (IMessageLite) value);
- case FieldType.Message:
- return ComputeMessageSize(fieldNumber, (IMessageLite) value);
- case FieldType.Bytes:
- return ComputeBytesSize(fieldNumber, (ByteString) value);
- case FieldType.UInt32:
- return ComputeUInt32Size(fieldNumber, (uint) value);
- case FieldType.SFixed32:
- return ComputeSFixed32Size(fieldNumber, (int) value);
- case FieldType.SFixed64:
- return ComputeSFixed64Size(fieldNumber, (long) value);
- case FieldType.SInt32:
- return ComputeSInt32Size(fieldNumber, (int) value);
- case FieldType.SInt64:
- return ComputeSInt64Size(fieldNumber, (long) value);
- case FieldType.Enum:
- return ComputeEnumSize(fieldNumber, ((IEnumLite) value).Number);
- default:
- throw new ArgumentOutOfRangeException("Invalid field type " + fieldType);
- }
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a
- /// field of arbitrary type, excluding the tag, to the stream.
- /// </summary>
- public static int ComputeFieldSizeNoTag(FieldType fieldType, Object value)
- {
- switch (fieldType)
- {
- case FieldType.Double:
- return ComputeDoubleSizeNoTag((double) value);
- case FieldType.Float:
- return ComputeFloatSizeNoTag((float) value);
- case FieldType.Int64:
- return ComputeInt64SizeNoTag((long) value);
- case FieldType.UInt64:
- return ComputeUInt64SizeNoTag((ulong) value);
- case FieldType.Int32:
- return ComputeInt32SizeNoTag((int) value);
- case FieldType.Fixed64:
- return ComputeFixed64SizeNoTag((ulong) value);
- case FieldType.Fixed32:
- return ComputeFixed32SizeNoTag((uint) value);
- case FieldType.Bool:
- return ComputeBoolSizeNoTag((bool) value);
- case FieldType.String:
- return ComputeStringSizeNoTag((string) value);
- case FieldType.Group:
- return ComputeGroupSizeNoTag((IMessageLite) value);
- case FieldType.Message:
- return ComputeMessageSizeNoTag((IMessageLite) value);
- case FieldType.Bytes:
- return ComputeBytesSizeNoTag((ByteString) value);
- case FieldType.UInt32:
- return ComputeUInt32SizeNoTag((uint) value);
- case FieldType.SFixed32:
- return ComputeSFixed32SizeNoTag((int) value);
- case FieldType.SFixed64:
- return ComputeSFixed64SizeNoTag((long) value);
- case FieldType.SInt32:
- return ComputeSInt32SizeNoTag((int) value);
- case FieldType.SInt64:
- return ComputeSInt64SizeNoTag((long) value);
- case FieldType.Enum:
- return ComputeEnumSizeNoTag(((IEnumLite) value).Number);
- default:
- throw new ArgumentOutOfRangeException("Invalid field type " + fieldType);
- }
- }
- /// <summary>
- /// Compute the number of bytes that would be needed to encode a tag.
- /// </summary>
- public static int ComputeTagSize(int fieldNumber)
- {
- return ComputeRawVarint32Size(WireFormat.MakeTag(fieldNumber, 0));
- }
- #endregion
- /// <summary>
- /// Encode a 32-bit value with ZigZag encoding.
- /// </summary>
- /// <remarks>
- /// ZigZag encodes signed integers into values that can be efficiently
- /// encoded with varint. (Otherwise, negative values must be
- /// sign-extended to 64 bits to be varint encoded, thus always taking
- /// 10 bytes on the wire.)
- /// </remarks>
- [CLSCompliant(false)]
- public static uint EncodeZigZag32(int n)
- {
- // Note: the right-shift must be arithmetic
- return (uint) ((n << 1) ^ (n >> 31));
- }
- /// <summary>
- /// Encode a 64-bit value with ZigZag encoding.
- /// </summary>
- /// <remarks>
- /// ZigZag encodes signed integers into values that can be efficiently
- /// encoded with varint. (Otherwise, negative values must be
- /// sign-extended to 64 bits to be varint encoded, thus always taking
- /// 10 bytes on the wire.)
- /// </remarks>
- [CLSCompliant(false)]
- public static ulong EncodeZigZag64(long n)
- {
- return (ulong) ((n << 1) ^ (n >> 63));
- }
- private void RefreshBuffer()
- {
- if (output == null)
- {
- // We're writing to a single buffer.
- throw new OutOfSpaceException();
- }
- // Since we have an output stream, this is our buffer
- // and buffer offset == 0
- output.Write(buffer, 0, position);
- position = 0;
- }
- /// <summary>
- /// Indicates that a CodedOutputStream wrapping a flat byte array
- /// ran out of space.
- /// </summary>
- public sealed class OutOfSpaceException : IOException
- {
- internal OutOfSpaceException()
- : base("CodedOutputStream was writing to a flat byte array and ran out of space.")
- {
- }
- }
- public void Flush()
- {
- if (output != null)
- {
- RefreshBuffer();
- }
- }
- /// <summary>
- /// Verifies that SpaceLeft returns zero. It's common to create a byte array
- /// that is exactly big enough to hold a message, then write to it with
- /// a CodedOutputStream. Calling CheckNoSpaceLeft after writing verifies that
- /// the message was actually as big as expected, which can help bugs.
- /// </summary>
- public void CheckNoSpaceLeft()
- {
- if (SpaceLeft != 0)
- {
- throw new InvalidOperationException("Did not write as much data as expected.");
- }
- }
- /// <summary>
- /// If writing to a flat array, returns the space left in the array. Otherwise,
- /// throws an InvalidOperationException.
- /// </summary>
- public int SpaceLeft
- {
- get
- {
- if (output == null)
- {
- return limit - position;
- }
- else
- {
- throw new InvalidOperationException(
- "SpaceLeft can only be called on CodedOutputStreams that are " +
- "writing to a flat array.");
- }
- }
- }
- }
- }
|