1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291 |
- #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.Collections;
- using System.IO;
- using System.Text;
- using Google.Protobuf.Collections;
- using Google.Protobuf.Descriptors;
- namespace Google.Protobuf
- {
- /// <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 : ICodedOutputStream
- {
- private static readonly Encoding UTF8 = Encoding.UTF8;
- /// <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
- /// <summary>
- /// Returns the current position in the stream, or the position in the output buffer
- /// </summary>
- public long Position
- {
- get
- {
- if (output != null)
- {
- return output.Position + position;
- }
- return position;
- }
- }
- void ICodedOutputStream.WriteMessageStart() { }
- void ICodedOutputStream.WriteMessageEnd() { Flush(); }
- #region Writing of tags and fields
- // TODO(jonskeet): Do we need this?
- public void WriteField(FieldType fieldType, int fieldNumber, string fieldName, object value)
- {
- switch (fieldType)
- {
- case FieldType.String:
- WriteString(fieldNumber, fieldName, (string) value);
- break;
- case FieldType.Message:
- WriteMessage(fieldNumber, fieldName, (IMessage) value);
- break;
- case FieldType.Group:
- WriteGroup(fieldNumber, fieldName, (IMessage) value);
- break;
- case FieldType.Bytes:
- WriteBytes(fieldNumber, fieldName, (ByteString) value);
- break;
- case FieldType.Bool:
- WriteBool(fieldNumber, fieldName, (bool) value);
- break;
- case FieldType.Enum:
- throw new NotImplementedException();
- case FieldType.Int32:
- WriteInt32(fieldNumber, fieldName, (int) value);
- break;
- case FieldType.Int64:
- WriteInt64(fieldNumber, fieldName, (long) value);
- break;
- case FieldType.UInt32:
- WriteUInt32(fieldNumber, fieldName, (uint) value);
- break;
- case FieldType.UInt64:
- WriteUInt64(fieldNumber, fieldName, (ulong) value);
- break;
- case FieldType.SInt32:
- WriteSInt32(fieldNumber, fieldName, (int) value);
- break;
- case FieldType.SInt64:
- WriteSInt64(fieldNumber, fieldName, (long) value);
- break;
- case FieldType.Fixed32:
- WriteFixed32(fieldNumber, fieldName, (uint) value);
- break;
- case FieldType.Fixed64:
- WriteFixed64(fieldNumber, fieldName, (ulong) value);
- break;
- case FieldType.SFixed32:
- WriteSFixed32(fieldNumber, fieldName, (int) value);
- break;
- case FieldType.SFixed64:
- WriteSFixed64(fieldNumber, fieldName, (long) value);
- break;
- case FieldType.Double:
- WriteDouble(fieldNumber, fieldName, (double) value);
- break;
- case FieldType.Float:
- WriteFloat(fieldNumber, fieldName, (float) value);
- break;
- }
- }
- /// <summary>
- /// Writes a double field value, including tag, to the stream.
- /// </summary>
- public void WriteDouble(int fieldNumber, string fieldName, 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, string fieldName, float value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
- WriteFloatNoTag(value);
- }
- /// <summary>
- /// Writes a uint64 field value, including tag, to the stream.
- /// </summary>
- public void WriteUInt64(int fieldNumber, string fieldName, 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, string fieldName, 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, string fieldName, 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>
- public void WriteFixed64(int fieldNumber, string fieldName, ulong value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
- WriteRawLittleEndian64(value);
- }
- /// <summary>
- /// Writes a fixed32 field value, including tag, to the stream.
- /// </summary>
- public void WriteFixed32(int fieldNumber, string fieldName, 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, string fieldName, 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 fieldName, 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 = UTF8.GetByteCount(value);
- WriteRawVarint32((uint) length);
- if (limit - position >= length)
- {
- if (length == value.Length) // Must be all ASCII...
- {
- for (int i = 0; i < length; i++)
- {
- buffer[position + i] = (byte)value[i];
- }
- }
- else
- {
- UTF8.GetBytes(value, 0, value.Length, buffer, position);
- }
- position += length;
- }
- else
- {
- byte[] bytes = UTF8.GetBytes(value);
- WriteRawBytes(bytes);
- }
- }
- /// <summary>
- /// Writes a group field value, including tag, to the stream.
- /// </summary>
- public void WriteGroup(int fieldNumber, string fieldName, IMessage value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.StartGroup);
- value.WriteTo(this);
- WriteTag(fieldNumber, WireFormat.WireType.EndGroup);
- }
- public void WriteMessage(int fieldNumber, string fieldName, IMessage value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32((uint) value.CalculateSize());
- value.WriteTo(this);
- }
- public void WriteBytes(int fieldNumber, string fieldName, ByteString value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32((uint) value.Length);
- value.WriteRawBytesTo(this);
- }
- public void WriteUInt32(int fieldNumber, string fieldName, uint value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawVarint32(value);
- }
- public void WriteEnum<T>(int fieldNumber, string fieldName, T value) where T : struct, IComparable, IFormattable
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteInt32NoTag(EnumHelper<T>.ToInt32(value));
- }
- public void WriteSFixed32(int fieldNumber, string fieldName, int value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
- WriteRawLittleEndian32((uint) value);
- }
- public void WriteSFixed64(int fieldNumber, string fieldName, long value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
- WriteRawLittleEndian64((ulong) value);
- }
- public void WriteSInt32(int fieldNumber, string fieldName, int value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawVarint32(EncodeZigZag32(value));
- }
- public void WriteSInt64(int fieldNumber, string fieldName, long value)
- {
- WriteTag(fieldNumber, WireFormat.WireType.Varint);
- WriteRawVarint64(EncodeZigZag64(value));
- }
- public void WriteMessageSetExtension(int fieldNumber, string fieldName, IMessage value)
- {
- WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.StartGroup);
- WriteUInt32(WireFormat.MessageSetField.TypeID, "type_id", (uint) fieldNumber);
- WriteMessage(WireFormat.MessageSetField.Message, "message", value);
- WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.EndGroup);
- }
- public void WriteMessageSetExtension(int fieldNumber, string fieldName, ByteString value)
- {
- WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.StartGroup);
- WriteUInt32(WireFormat.MessageSetField.TypeID, "type_id", (uint) fieldNumber);
- WriteBytes(WireFormat.MessageSetField.Message, "message", value);
- WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.EndGroup);
- }
- #endregion
- #region Writing of values without tags
- // TODO(jonskeet): Remove this?
- public void WriteFieldNoTag(FieldType fieldType, object value)
- {
- switch (fieldType)
- {
- case FieldType.String:
- WriteStringNoTag((string) value);
- break;
- case FieldType.Message:
- WriteMessageNoTag((IMessage) value);
- break;
- case FieldType.Group:
- WriteGroupNoTag((IMessage) value);
- break;
- case FieldType.Bytes:
- WriteBytesNoTag((ByteString) value);
- break;
- case FieldType.Bool:
- WriteBoolNoTag((bool) value);
- break;
- case FieldType.Enum:
- WriteEnumNoTag((int) value);
- break;
- case FieldType.Int32:
- WriteInt32NoTag((int) value);
- break;
- case FieldType.Int64:
- WriteInt64NoTag((long) value);
- break;
- case FieldType.UInt32:
- WriteUInt32NoTag((uint) value);
- break;
- case FieldType.UInt64:
- WriteUInt64NoTag((ulong) value);
- break;
- case FieldType.SInt32:
- WriteSInt32NoTag((int) value);
- break;
- case FieldType.SInt64:
- WriteSInt64NoTag((long) value);
- break;
- case FieldType.Fixed32:
- WriteFixed32NoTag((uint) value);
- break;
- case FieldType.Fixed64:
- WriteFixed64NoTag((ulong) value);
- break;
- case FieldType.SFixed32:
- WriteSFixed32NoTag((int) value);
- break;
- case FieldType.SFixed64:
- WriteSFixed64NoTag((long) value);
- break;
- case FieldType.Double:
- WriteDoubleNoTag((double) value);
- break;
- case FieldType.Float:
- WriteFloatNoTag((float) value);
- break;
- }
- }
- /// <summary>
- /// Writes a double field value, including tag, to the stream.
- /// </summary>
- public void WriteDoubleNoTag(double value)
- {
- WriteRawLittleEndian64((ulong)FrameworkPortability.DoubleToInt64(value));
- }
- /// <summary>
- /// Writes a float field value, without a tag, to the stream.
- /// </summary>
- public void WriteFloatNoTag(float value)
- {
- byte[] rawBytes = BitConverter.GetBytes(value);
- if (!BitConverter.IsLittleEndian)
- {
- ByteArray.Reverse(rawBytes);
- }
- if (limit - position >= 4)
- {
- buffer[position++] = rawBytes[0];
- buffer[position++] = rawBytes[1];
- buffer[position++] = rawBytes[2];
- buffer[position++] = rawBytes[3];
- }
- else
- {
- WriteRawBytes(rawBytes, 0, 4);
- }
- }
- /// <summary>
- /// Writes a uint64 field value, without a tag, to the stream.
- /// </summary>
- 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>
- public void WriteFixed64NoTag(ulong value)
- {
- WriteRawLittleEndian64(value);
- }
- /// <summary>
- /// Writes a fixed32 field value, without a tag, to the stream.
- /// </summary>
- 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(IMessage value)
- {
- value.WriteTo(this);
- }
- public void WriteMessageNoTag(IMessage value)
- {
- WriteRawVarint32((uint) value.CalculateSize());
- value.WriteTo(this);
- }
- public void WriteBytesNoTag(ByteString value)
- {
- WriteRawVarint32((uint) value.Length);
- value.WriteRawBytesTo(this);
- }
- public void WriteUInt32NoTag(uint value)
- {
- WriteRawVarint32(value);
- }
- public void WriteEnumNoTag<T>(T value) where T : struct, IComparable, IFormattable
- {
- WriteInt32NoTag(EnumHelper<T>.ToInt32(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 Write array members
- // TODO(jonskeet): Remove?
- public void WriteArray(FieldType fieldType, int fieldNumber, string fieldName, IEnumerable list)
- {
- foreach (object element in list)
- {
- WriteField(fieldType, fieldNumber, fieldName, element);
- }
- }
- public void WriteGroupArray<T>(int fieldNumber, string fieldName, RepeatedField<T> list)
- where T : IMessage
- {
- foreach (IMessage value in list)
- {
- WriteGroup(fieldNumber, fieldName, value);
- }
- }
- public void WriteMessageArray<T>(int fieldNumber, string fieldName, RepeatedField<T> list)
- where T : IMessage
- {
- foreach (IMessage value in list)
- {
- WriteMessage(fieldNumber, fieldName, value);
- }
- }
- public void WriteStringArray(int fieldNumber, string fieldName, RepeatedField<string> list)
- {
- foreach (var value in list)
- {
- WriteString(fieldNumber, fieldName, value);
- }
- }
- public void WriteBytesArray(int fieldNumber, string fieldName, RepeatedField<ByteString> list)
- {
- foreach (var value in list)
- {
- WriteBytes(fieldNumber, fieldName, value);
- }
- }
- public void WriteBoolArray(int fieldNumber, string fieldName, RepeatedField<bool> list)
- {
- foreach (var value in list)
- {
- WriteBool(fieldNumber, fieldName, value);
- }
- }
- public void WriteInt32Array(int fieldNumber, string fieldName, RepeatedField<int> list)
- {
- foreach (var value in list)
- {
- WriteInt32(fieldNumber, fieldName, value);
- }
- }
- public void WriteSInt32Array(int fieldNumber, string fieldName, RepeatedField<int> list)
- {
- foreach (var value in list)
- {
- WriteSInt32(fieldNumber, fieldName, value);
- }
- }
- public void WriteUInt32Array(int fieldNumber, string fieldName, RepeatedField<uint> list)
- {
- foreach (var value in list)
- {
- WriteUInt32(fieldNumber, fieldName, value);
- }
- }
- public void WriteFixed32Array(int fieldNumber, string fieldName, RepeatedField<uint> list)
- {
- foreach (var value in list)
- {
- WriteFixed32(fieldNumber, fieldName, value);
- }
- }
- public void WriteSFixed32Array(int fieldNumber, string fieldName, RepeatedField<int> list)
- {
- foreach (var value in list)
- {
- WriteSFixed32(fieldNumber, fieldName, value);
- }
- }
- public void WriteInt64Array(int fieldNumber, string fieldName, RepeatedField<long> list)
- {
- foreach (var value in list)
- {
- WriteInt64(fieldNumber, fieldName, value);
- }
- }
- public void WriteSInt64Array(int fieldNumber, string fieldName, RepeatedField<long> list)
- {
- foreach (var value in list)
- {
- WriteSInt64(fieldNumber, fieldName, value);
- }
- }
- public void WriteUInt64Array(int fieldNumber, string fieldName, RepeatedField<ulong> list)
- {
- foreach (var value in list)
- {
- WriteUInt64(fieldNumber, fieldName, value);
- }
- }
- public void WriteFixed64Array(int fieldNumber, string fieldName, RepeatedField<ulong> list)
- {
- foreach (var value in list)
- {
- WriteFixed64(fieldNumber, fieldName, value);
- }
- }
- public void WriteSFixed64Array(int fieldNumber, string fieldName, RepeatedField<long> list)
- {
- foreach (var value in list)
- {
- WriteSFixed64(fieldNumber, fieldName, value);
- }
- }
- public void WriteDoubleArray(int fieldNumber, string fieldName, RepeatedField<double> list)
- {
- foreach (var value in list)
- {
- WriteDouble(fieldNumber, fieldName, value);
- }
- }
- public void WriteFloatArray(int fieldNumber, string fieldName, RepeatedField<float> list)
- {
- foreach (var value in list)
- {
- WriteFloat(fieldNumber, fieldName, value);
- }
- }
- public void WriteEnumArray<T>(int fieldNumber, string fieldName, RepeatedField<T> list)
- where T : struct, IComparable, IFormattable
- {
- foreach (T value in list)
- {
- WriteEnum(fieldNumber, fieldName, value);
- }
- }
- #endregion
- #region Write packed array members
- // TODO(jonskeet): Remove?
- public void WritePackedArray(FieldType fieldType, int fieldNumber, string fieldName, IEnumerable list)
- {
- int calculatedSize = 0;
- foreach (object element in list)
- {
- calculatedSize += ComputeFieldSizeNoTag(fieldType, element);
- }
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32((uint) calculatedSize);
- foreach (object element in list)
- {
- WriteFieldNoTag(fieldType, element);
- }
- }
- // TODO(jonskeet): A lot of these are really inefficient, due to method group conversions. Fix!
- public void WritePackedBoolArray(int fieldNumber, string fieldName, RepeatedField<bool> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = (uint)list.Count;
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteBoolNoTag(value);
- }
- }
- public void WritePackedInt32Array(int fieldNumber, string fieldName, RepeatedField<int> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = list.CalculateSize(ComputeInt32SizeNoTag);
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteInt32NoTag(value);
- }
- }
- public void WritePackedSInt32Array(int fieldNumber, string fieldName, RepeatedField<int> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = list.CalculateSize(ComputeSInt32SizeNoTag);
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteSInt32NoTag(value);
- }
- }
- public void WritePackedUInt32Array(int fieldNumber, string fieldName, RepeatedField<uint> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = list.CalculateSize(ComputeUInt32SizeNoTag);
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteUInt32NoTag(value);
- }
- }
- public void WritePackedFixed32Array(int fieldNumber, string fieldName, RepeatedField<uint> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = (uint) list.Count * 4;
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteFixed32NoTag(value);
- }
- }
- public void WritePackedSFixed32Array(int fieldNumber, string fieldName, RepeatedField<int> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = (uint) list.Count * 4;
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteSFixed32NoTag(value);
- }
- }
- public void WritePackedInt64Array(int fieldNumber, string fieldName, RepeatedField<long> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = list.CalculateSize(ComputeInt64SizeNoTag);
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteInt64NoTag(value);
- }
- }
- public void WritePackedSInt64Array(int fieldNumber, string fieldName, RepeatedField<long> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = list.CalculateSize(ComputeSInt64SizeNoTag);
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteSInt64NoTag(value);
- }
- }
- public void WritePackedUInt64Array(int fieldNumber, string fieldName, RepeatedField<ulong> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = list.CalculateSize(ComputeUInt64SizeNoTag);
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteUInt64NoTag(value);
- }
- }
- public void WritePackedFixed64Array(int fieldNumber, string fieldName, RepeatedField<ulong> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = (uint) list.Count * 8;
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteFixed64NoTag(value);
- }
- }
- public void WritePackedSFixed64Array(int fieldNumber, string fieldName, RepeatedField<long> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = (uint) list.Count * 8;
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteSFixed64NoTag(value);
- }
- }
- public void WritePackedDoubleArray(int fieldNumber, string fieldName, RepeatedField<double> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = (uint) list.Count * 8;
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteDoubleNoTag(value);
- }
- }
- public void WritePackedFloatArray(int fieldNumber, string fieldName, RepeatedField<float> list)
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = (uint) list.Count * 4;
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (var value in list)
- {
- WriteFloatNoTag(value);
- }
- }
- public void WritePackedEnumArray<T>(int fieldNumber, string fieldName, RepeatedField<T> list)
- where T : struct, IComparable, IFormattable
- {
- if (list.Count == 0)
- {
- return;
- }
- uint size = list.CalculateSize(ComputeEnumSizeNoTag);
- WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- WriteRawVarint32(size);
- foreach (T value in list)
- {
- WriteEnumNoTag(value);
- }
- }
- #endregion
- #region Underlying writing primitives
- /// <summary>
- /// Encodes and writes a tag.
- /// </summary>
- public void WriteTag(int fieldNumber, WireFormat.WireType type)
- {
- WriteRawVarint32(WireFormat.MakeTag(fieldNumber, type));
- }
- /// <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>
- public void WriteRawVarint32(uint value)
- {
- // Optimize for the common case of a single byte value
- if (value < 128 && position < limit)
- {
- buffer[position++] = (byte)value;
- return;
- }
- while (value > 127 && position < limit)
- {
- buffer[position++] = (byte) ((value & 0x7F) | 0x80);
- value >>= 7;
- }
- while (value > 127)
- {
- WriteRawByte((byte) ((value & 0x7F) | 0x80));
- value >>= 7;
- }
- if (position < limit)
- {
- buffer[position++] = (byte) value;
- }
- else
- {
- WriteRawByte((byte) value);
- }
- }
- public void WriteRawVarint64(ulong value)
- {
- while (value > 127 && position < limit)
- {
- buffer[position++] = (byte) ((value & 0x7F) | 0x80);
- value >>= 7;
- }
- while (value > 127)
- {
- WriteRawByte((byte) ((value & 0x7F) | 0x80));
- value >>= 7;
- }
- if (position < limit)
- {
- buffer[position++] = (byte) value;
- }
- else
- {
- WriteRawByte((byte) value);
- }
- }
- public void WriteRawLittleEndian32(uint value)
- {
- if (position + 4 > limit)
- {
- WriteRawByte((byte) value);
- WriteRawByte((byte) (value >> 8));
- WriteRawByte((byte) (value >> 16));
- WriteRawByte((byte) (value >> 24));
- }
- else
- {
- buffer[position++] = ((byte) value);
- buffer[position++] = ((byte) (value >> 8));
- buffer[position++] = ((byte) (value >> 16));
- buffer[position++] = ((byte) (value >> 24));
- }
- }
- public void WriteRawLittleEndian64(ulong value)
- {
- if (position + 8 > limit)
- {
- 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));
- }
- else
- {
- buffer[position++] = ((byte) value);
- buffer[position++] = ((byte) (value >> 8));
- buffer[position++] = ((byte) (value >> 16));
- buffer[position++] = ((byte) (value >> 24));
- buffer[position++] = ((byte) (value >> 32));
- buffer[position++] = ((byte) (value >> 40));
- buffer[position++] = ((byte) (value >> 48));
- buffer[position++] = ((byte) (value >> 56));
- }
- }
- public void WriteRawByte(byte value)
- {
- if (position == limit)
- {
- RefreshBuffer();
- }
- buffer[position++] = value;
- }
- 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)
- {
- ByteArray.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;
- ByteArray.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.
- ByteArray.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
- /// <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>
- 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>
- 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.");
- }
- }
- }
- }
- }
|