From f34d37a3d4d64621bc87aa0a65a05cab64062399 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Tue, 30 Jun 2015 13:16:20 +0100 Subject: Tidying up and extra tests. This is mostly just making things internal instead of public, removing and reordering a bunch of code in CodedInputStream/CodedOutputStream, and generally tidying up. --- csharp/src/ProtocolBuffers/CodedOutputStream.cs | 97 +++++++++++++++++++++---- 1 file changed, 83 insertions(+), 14 deletions(-) (limited to 'csharp/src/ProtocolBuffers/CodedOutputStream.cs') diff --git a/csharp/src/ProtocolBuffers/CodedOutputStream.cs b/csharp/src/ProtocolBuffers/CodedOutputStream.cs index 99a99ae2..161f48f4 100644 --- a/csharp/src/ProtocolBuffers/CodedOutputStream.cs +++ b/csharp/src/ProtocolBuffers/CodedOutputStream.cs @@ -37,7 +37,6 @@ using System; using System.IO; using System.Text; -using Google.Protobuf.Collections; namespace Google.Protobuf { @@ -141,11 +140,12 @@ namespace Google.Protobuf } } - #region Writing of values without tags + #region Writing of values (not including tags) /// - /// Writes a double field value, including tag, to the stream. + /// Writes a double field value, without a tag, to the stream. /// + /// The value to write public void WriteDouble(double value) { WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value)); @@ -154,6 +154,7 @@ namespace Google.Protobuf /// /// Writes a float field value, without a tag, to the stream. /// + /// The value to write public void WriteFloat(float value) { byte[] rawBytes = BitConverter.GetBytes(value); @@ -178,6 +179,7 @@ namespace Google.Protobuf /// /// Writes a uint64 field value, without a tag, to the stream. /// + /// The value to write public void WriteUInt64(ulong value) { WriteRawVarint64(value); @@ -186,6 +188,7 @@ namespace Google.Protobuf /// /// Writes an int64 field value, without a tag, to the stream. /// + /// The value to write public void WriteInt64(long value) { WriteRawVarint64((ulong) value); @@ -194,6 +197,7 @@ namespace Google.Protobuf /// /// Writes an int32 field value, without a tag, to the stream. /// + /// The value to write public void WriteInt32(int value) { if (value >= 0) @@ -210,6 +214,7 @@ namespace Google.Protobuf /// /// Writes a fixed64 field value, without a tag, to the stream. /// + /// The value to write public void WriteFixed64(ulong value) { WriteRawLittleEndian64(value); @@ -218,6 +223,7 @@ namespace Google.Protobuf /// /// Writes a fixed32 field value, without a tag, to the stream. /// + /// The value to write public void WriteFixed32(uint value) { WriteRawLittleEndian32(value); @@ -226,6 +232,7 @@ namespace Google.Protobuf /// /// Writes a bool field value, without a tag, to the stream. /// + /// The value to write public void WriteBool(bool value) { WriteRawByte(value ? (byte) 1 : (byte) 0); @@ -233,13 +240,15 @@ namespace Google.Protobuf /// /// Writes a string field value, without a tag, to the stream. + /// The data is length-prefixed. /// + /// The value to write public void WriteString(string value) { // Optimise the case where we have enough space to write // the string directly to the buffer, which should be common. int length = Utf8Encoding.GetByteCount(value); - WriteRawVarint32((uint)length); + WriteLength(length); if (limit - position >= length) { if (length == value.Length) // Must be all ASCII... @@ -262,23 +271,41 @@ namespace Google.Protobuf } } + /// + /// Writes a message, without a tag, to the stream. + /// The data is length-prefixed. + /// + /// The value to write public void WriteMessage(IMessage value) { WriteRawVarint32((uint) value.CalculateSize()); value.WriteTo(this); } + /// + /// Write a byte string, without a tag, to the stream. + /// The data is length-prefixed. + /// + /// The value to write public void WriteBytes(ByteString value) { WriteRawVarint32((uint) value.Length); value.WriteRawBytesTo(this); } + /// + /// Writes a uint32 value, without a tag, to the stream. + /// + /// The value to write public void WriteUInt32(uint value) { WriteRawVarint32(value); } + /// + /// Writes an enum value, without a tag, to the stream. + /// + /// The value to write public void WriteEnum(int value) { WriteInt32(value); @@ -289,27 +316,53 @@ namespace Google.Protobuf WriteRawLittleEndian32((uint) value); } + /// + /// Writes an sfixed64 value, without a tag, to the stream. + /// + /// The value to write public void WriteSFixed64(long value) { WriteRawLittleEndian64((ulong) value); } + /// + /// Writes an sint32 value, without a tag, to the stream. + /// + /// The value to write public void WriteSInt32(int value) { WriteRawVarint32(EncodeZigZag32(value)); } + /// + /// Writes an sint64 value, without a tag, to the stream. + /// + /// The value to write public void WriteSInt64(long value) { WriteRawVarint64(EncodeZigZag64(value)); } + /// + /// Writes a length (in bytes) for length-delimited data. + /// + /// + /// This method simply writes a rawint, but exists for clarity in calling code. + /// + /// Length value, in bytes. + public void WriteLength(int length) + { + WriteRawVarint32((uint) length); + } + #endregion #region Raw tag writing /// /// Encodes and writes a tag. /// + /// The number of the field to write the tag for + /// The wire format type of the tag to write public void WriteTag(int fieldNumber, WireFormat.WireType type) { WriteRawVarint32(WireFormat.MakeTag(fieldNumber, type)); @@ -318,6 +371,7 @@ namespace Google.Protobuf /// /// Writes an already-encoded tag. /// + /// The encoded tag public void WriteTag(uint tag) { WriteRawVarint32(tag); @@ -326,6 +380,7 @@ namespace Google.Protobuf /// /// Writes the given single-byte tag directly to the stream. /// + /// The encoded tag public void WriteRawTag(byte b1) { WriteRawByte(b1); @@ -334,6 +389,8 @@ namespace Google.Protobuf /// /// Writes the given two-byte tag directly to the stream. /// + /// The first byte of the encoded tag + /// The second byte of the encoded tag public void WriteRawTag(byte b1, byte b2) { WriteRawByte(b1); @@ -343,6 +400,9 @@ namespace Google.Protobuf /// /// Writes the given three-byte tag directly to the stream. /// + /// The first byte of the encoded tag + /// The second byte of the encoded tag + /// The third byte of the encoded tag public void WriteRawTag(byte b1, byte b2, byte b3) { WriteRawByte(b1); @@ -353,6 +413,10 @@ namespace Google.Protobuf /// /// Writes the given four-byte tag directly to the stream. /// + /// The first byte of the encoded tag + /// The second byte of the encoded tag + /// The third byte of the encoded tag + /// The fourth byte of the encoded tag public void WriteRawTag(byte b1, byte b2, byte b3, byte b4) { WriteRawByte(b1); @@ -364,6 +428,11 @@ namespace Google.Protobuf /// /// Writes the given five-byte tag directly to the stream. /// + /// The first byte of the encoded tag + /// The second byte of the encoded tag + /// The third byte of the encoded tag + /// The fourth byte of the encoded tag + /// The fifth byte of the encoded tag public void WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5) { WriteRawByte(b1); @@ -380,7 +449,7 @@ namespace Google.Protobuf /// there's enough buffer space left to whizz through without checking /// for each byte; otherwise, we resort to calling WriteRawByte each time. /// - public void WriteRawVarint32(uint value) + internal void WriteRawVarint32(uint value) { // Optimize for the common case of a single byte value if (value < 128 && position < limit) @@ -409,7 +478,7 @@ namespace Google.Protobuf } } - public void WriteRawVarint64(ulong value) + internal void WriteRawVarint64(ulong value) { while (value > 127 && position < limit) { @@ -431,7 +500,7 @@ namespace Google.Protobuf } } - public void WriteRawLittleEndian32(uint value) + internal void WriteRawLittleEndian32(uint value) { if (position + 4 > limit) { @@ -449,7 +518,7 @@ namespace Google.Protobuf } } - public void WriteRawLittleEndian64(ulong value) + internal void WriteRawLittleEndian64(ulong value) { if (position + 8 > limit) { @@ -475,7 +544,7 @@ namespace Google.Protobuf } } - public void WriteRawByte(byte value) + internal void WriteRawByte(byte value) { if (position == limit) { @@ -485,7 +554,7 @@ namespace Google.Protobuf buffer[position++] = value; } - public void WriteRawByte(uint value) + internal void WriteRawByte(uint value) { WriteRawByte((byte) value); } @@ -493,7 +562,7 @@ namespace Google.Protobuf /// /// Writes out an array of bytes. /// - public void WriteRawBytes(byte[] value) + internal void WriteRawBytes(byte[] value) { WriteRawBytes(value, 0, value.Length); } @@ -501,7 +570,7 @@ namespace Google.Protobuf /// /// Writes out part of an array of bytes. /// - public void WriteRawBytes(byte[] value, int offset, int length) + internal void WriteRawBytes(byte[] value, int offset, int length) { if (limit - position >= length) { @@ -548,7 +617,7 @@ namespace Google.Protobuf /// sign-extended to 64 bits to be varint encoded, thus always taking /// 10 bytes on the wire.) /// - public static uint EncodeZigZag32(int n) + internal static uint EncodeZigZag32(int n) { // Note: the right-shift must be arithmetic return (uint) ((n << 1) ^ (n >> 31)); @@ -563,7 +632,7 @@ namespace Google.Protobuf /// sign-extended to 64 bits to be varint encoded, thus always taking /// 10 bytes on the wire.) /// - public static ulong EncodeZigZag64(long n) + internal static ulong EncodeZigZag64(long n) { return (ulong) ((n << 1) ^ (n >> 63)); } -- cgit v1.2.3