123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- #region Copyright notice and license
- #endregion
- using Google.Protobuf.Collections;
- using System;
- using System.IO;
- using System.Text;
- namespace Google.Protobuf
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public sealed partial class CodedOutputStream : IDisposable
- {
-
- internal static readonly Encoding Utf8Encoding = Encoding.UTF8;
-
-
-
- public static readonly int DefaultBufferSize = 4096;
- private readonly bool leaveOpen;
- private readonly byte[] buffer;
- private readonly int limit;
- private int position;
- private readonly Stream output;
- #region Construction
-
-
-
-
-
- public CodedOutputStream(byte[] flatArray) : this(flatArray, 0, flatArray.Length)
- {
- }
-
-
-
-
-
- private CodedOutputStream(byte[] buffer, int offset, int length)
- {
- this.output = null;
- this.buffer = buffer;
- this.position = offset;
- this.limit = offset + length;
- leaveOpen = true;
- }
- private CodedOutputStream(Stream output, byte[] buffer, bool leaveOpen)
- {
- this.output = ProtoPreconditions.CheckNotNull(output, "output");
- this.buffer = buffer;
- this.position = 0;
- this.limit = buffer.Length;
- this.leaveOpen = leaveOpen;
- }
-
-
-
-
-
- public CodedOutputStream(Stream output) : this(output, DefaultBufferSize, false)
- {
- }
-
-
-
-
-
-
- public CodedOutputStream(Stream output, int bufferSize) : this(output, new byte[bufferSize], false)
- {
- }
-
-
-
-
-
-
- public CodedOutputStream(Stream output, bool leaveOpen) : this(output, DefaultBufferSize, leaveOpen)
- {
- }
-
-
-
-
-
-
-
-
- public CodedOutputStream(Stream output, int bufferSize, bool leaveOpen) : this(output, new byte[bufferSize], leaveOpen)
- {
- }
- #endregion
-
-
-
- public long Position
- {
- get
- {
- if (output != null)
- {
- return output.Position + position;
- }
- return position;
- }
- }
- #region Writing of values (not including tags)
-
-
-
-
- public void WriteDouble(double value)
- {
- WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value));
- }
-
-
-
-
- public void WriteFloat(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);
- }
- }
-
-
-
-
- public void WriteUInt64(ulong value)
- {
- WriteRawVarint64(value);
- }
-
-
-
-
- public void WriteInt64(long value)
- {
- WriteRawVarint64((ulong) value);
- }
-
-
-
-
- public void WriteInt32(int value)
- {
- if (value >= 0)
- {
- WriteRawVarint32((uint) value);
- }
- else
- {
-
- WriteRawVarint64((ulong) value);
- }
- }
-
-
-
-
- public void WriteFixed64(ulong value)
- {
- WriteRawLittleEndian64(value);
- }
-
-
-
-
- public void WriteFixed32(uint value)
- {
- WriteRawLittleEndian32(value);
- }
-
-
-
-
- public void WriteBool(bool value)
- {
- WriteRawByte(value ? (byte) 1 : (byte) 0);
- }
-
-
-
-
-
- public void WriteString(string value)
- {
-
-
- int length = Utf8Encoding.GetByteCount(value);
- WriteLength(length);
- if (limit - position >= length)
- {
- if (length == value.Length)
- {
- for (int i = 0; i < length; i++)
- {
- buffer[position + i] = (byte)value[i];
- }
- }
- else
- {
- Utf8Encoding.GetBytes(value, 0, value.Length, buffer, position);
- }
- position += length;
- }
- else
- {
- byte[] bytes = Utf8Encoding.GetBytes(value);
- WriteRawBytes(bytes);
- }
- }
-
-
-
-
-
- public void WriteMessage(IMessage value)
- {
- WriteLength(value.CalculateSize());
- value.WriteTo(this);
- }
-
-
-
-
-
- public void WriteBytes(ByteString value)
- {
- WriteLength(value.Length);
- value.WriteRawBytesTo(this);
- }
-
-
-
-
- public void WriteUInt32(uint value)
- {
- WriteRawVarint32(value);
- }
-
-
-
-
- public void WriteEnum(int value)
- {
- WriteInt32(value);
- }
-
-
-
-
- public void WriteSFixed32(int value)
- {
- WriteRawLittleEndian32((uint) value);
- }
-
-
-
-
- public void WriteSFixed64(long value)
- {
- WriteRawLittleEndian64((ulong) value);
- }
-
-
-
-
- public void WriteSInt32(int value)
- {
- WriteRawVarint32(EncodeZigZag32(value));
- }
-
-
-
-
- public void WriteSInt64(long value)
- {
- WriteRawVarint64(EncodeZigZag64(value));
- }
-
-
-
-
-
-
-
- public void WriteLength(int length)
- {
- WriteRawVarint32((uint) length);
- }
- #endregion
- #region Raw tag writing
-
-
-
-
-
- public void WriteTag(int fieldNumber, WireFormat.WireType type)
- {
- WriteRawVarint32(WireFormat.MakeTag(fieldNumber, type));
- }
-
-
-
-
- public void WriteTag(uint tag)
- {
- WriteRawVarint32(tag);
- }
-
-
-
-
- public void WriteRawTag(byte b1)
- {
- WriteRawByte(b1);
- }
-
-
-
-
-
- public void WriteRawTag(byte b1, byte b2)
- {
- WriteRawByte(b1);
- WriteRawByte(b2);
- }
-
-
-
-
-
-
- public void WriteRawTag(byte b1, byte b2, byte b3)
- {
- WriteRawByte(b1);
- WriteRawByte(b2);
- WriteRawByte(b3);
- }
-
-
-
-
-
-
-
- public void WriteRawTag(byte b1, byte b2, byte b3, byte b4)
- {
- WriteRawByte(b1);
- WriteRawByte(b2);
- WriteRawByte(b3);
- WriteRawByte(b4);
- }
-
-
-
-
-
-
-
-
- public void WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5)
- {
- WriteRawByte(b1);
- WriteRawByte(b2);
- WriteRawByte(b3);
- WriteRawByte(b4);
- WriteRawByte(b5);
- }
- #endregion
- #region Underlying writing primitives
-
-
-
-
-
- internal void WriteRawVarint32(uint 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);
- }
- }
- internal 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);
- }
- }
- internal 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));
- }
- }
- internal 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));
- }
- }
- internal void WriteRawByte(byte value)
- {
- if (position == limit)
- {
- RefreshBuffer();
- }
- buffer[position++] = value;
- }
- internal void WriteRawByte(uint value)
- {
- WriteRawByte((byte) value);
- }
-
-
-
- internal void WriteRawBytes(byte[] value)
- {
- WriteRawBytes(value, 0, value.Length);
- }
-
-
-
- internal void WriteRawBytes(byte[] value, int offset, int length)
- {
- if (limit - position >= length)
- {
- ByteArray.Copy(value, offset, buffer, position, length);
-
- position += length;
- }
- else
- {
-
-
- int bytesWritten = limit - position;
- ByteArray.Copy(value, offset, buffer, position, bytesWritten);
- offset += bytesWritten;
- length -= bytesWritten;
- position = limit;
- RefreshBuffer();
-
-
-
- if (length <= limit)
- {
-
- ByteArray.Copy(value, offset, buffer, 0, length);
- position = length;
- }
- else
- {
-
- output.Write(value, offset, length);
- }
- }
- }
- #endregion
-
-
-
-
-
-
-
-
-
- internal static uint EncodeZigZag32(int n)
- {
-
- return (uint) ((n << 1) ^ (n >> 31));
- }
-
-
-
-
-
-
-
-
-
- internal static ulong EncodeZigZag64(long n)
- {
- return (ulong) ((n << 1) ^ (n >> 63));
- }
- private void RefreshBuffer()
- {
- if (output == null)
- {
-
- throw new OutOfSpaceException();
- }
-
-
- output.Write(buffer, 0, position);
- position = 0;
- }
-
-
-
-
- public sealed class OutOfSpaceException : IOException
- {
- internal OutOfSpaceException()
- : base("CodedOutputStream was writing to a flat byte array and ran out of space.")
- {
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void Dispose()
- {
- Flush();
- if (!leaveOpen)
- {
- output.Dispose();
- }
- }
-
-
-
- public void Flush()
- {
- if (output != null)
- {
- RefreshBuffer();
- }
- }
-
-
-
-
-
-
- public void CheckNoSpaceLeft()
- {
- if (SpaceLeft != 0)
- {
- throw new InvalidOperationException("Did not write as much data as expected.");
- }
- }
-
-
-
-
- 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.");
- }
- }
- }
- }
- }
|