Merge pull request #678 from jskeet/stream-ctor

Expose Coded*Stream constructors directly.
pull/681/head
Jon Skeet 9 years ago
commit db9f47a3ed
  1. 38
      csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs
  2. 26
      csharp/src/Google.Protobuf.Test/CodedOutputStreamTest.cs
  3. 40
      csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs
  4. 16
      csharp/src/Google.Protobuf.Test/FieldCodecTest.cs
  5. 10
      csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs
  6. 2
      csharp/src/Google.Protobuf.Test/TestCornerCases.cs
  7. 2
      csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs
  8. 2
      csharp/src/Google.Protobuf/ByteString.cs
  9. 39
      csharp/src/Google.Protobuf/CodedInputStream.cs
  10. 42
      csharp/src/Google.Protobuf/CodedOutputStream.cs
  11. 10
      csharp/src/Google.Protobuf/MessageExtensions.cs

@ -59,20 +59,20 @@ namespace Google.Protobuf
/// </summary>
private static void AssertReadVarint(byte[] data, ulong value)
{
CodedInputStream input = CodedInputStream.CreateInstance(data);
CodedInputStream input = new CodedInputStream(data);
Assert.AreEqual((uint) value, input.ReadRawVarint32());
input = CodedInputStream.CreateInstance(data);
input = new CodedInputStream(data);
Assert.AreEqual(value, input.ReadRawVarint64());
Assert.IsTrue(input.IsAtEnd);
// Try different block sizes.
for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
{
input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize));
input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize));
Assert.AreEqual((uint) value, input.ReadRawVarint32());
input = CodedInputStream.CreateInstance(new SmallBlockInputStream(data, bufferSize));
input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize));
Assert.AreEqual(value, input.ReadRawVarint64());
Assert.IsTrue(input.IsAtEnd);
}
@ -95,11 +95,11 @@ namespace Google.Protobuf
/// </summary>
private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data)
{
CodedInputStream input = CodedInputStream.CreateInstance(data);
CodedInputStream input = new CodedInputStream(data);
var exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint32());
Assert.AreEqual(expected.Message, exception.Message);
input = CodedInputStream.CreateInstance(data);
input = new CodedInputStream(data);
exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint64());
Assert.AreEqual(expected.Message, exception.Message);
@ -152,14 +152,14 @@ namespace Google.Protobuf
/// </summary>
private static void AssertReadLittleEndian32(byte[] data, uint value)
{
CodedInputStream input = CodedInputStream.CreateInstance(data);
CodedInputStream input = new CodedInputStream(data);
Assert.AreEqual(value, input.ReadRawLittleEndian32());
Assert.IsTrue(input.IsAtEnd);
// Try different block sizes.
for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
{
input = CodedInputStream.CreateInstance(
input = new CodedInputStream(
new SmallBlockInputStream(data, blockSize));
Assert.AreEqual(value, input.ReadRawLittleEndian32());
Assert.IsTrue(input.IsAtEnd);
@ -172,14 +172,14 @@ namespace Google.Protobuf
/// </summary>
private static void AssertReadLittleEndian64(byte[] data, ulong value)
{
CodedInputStream input = CodedInputStream.CreateInstance(data);
CodedInputStream input = new CodedInputStream(data);
Assert.AreEqual(value, input.ReadRawLittleEndian64());
Assert.IsTrue(input.IsAtEnd);
// Try different block sizes.
for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
{
input = CodedInputStream.CreateInstance(
input = new CodedInputStream(
new SmallBlockInputStream(data, blockSize));
Assert.AreEqual(value, input.ReadRawLittleEndian64());
Assert.IsTrue(input.IsAtEnd);
@ -269,7 +269,7 @@ namespace Google.Protobuf
public void ReadMaliciouslyLargeBlob()
{
MemoryStream ms = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(ms);
CodedOutputStream output = new CodedOutputStream(ms);
uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
output.WriteRawVarint32(tag);
@ -278,7 +278,7 @@ namespace Google.Protobuf
output.Flush();
ms.Position = 0;
CodedInputStream input = CodedInputStream.CreateInstance(ms);
CodedInputStream input = new CodedInputStream(ms);
uint testtag;
Assert.IsTrue(input.ReadTag(out testtag));
Assert.AreEqual(tag, testtag);
@ -335,7 +335,7 @@ namespace Google.Protobuf
// Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't
// apply to the latter case.
MemoryStream ms = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray());
CodedInputStream input = CodedInputStream.CreateInstance(ms);
CodedInputStream input = new CodedInputStream(ms);
input.SetSizeLimit(16);
Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.ParseFrom(input));
@ -344,7 +344,7 @@ namespace Google.Protobuf
[Test]
public void ResetSizeCounter()
{
CodedInputStream input = CodedInputStream.CreateInstance(
CodedInputStream input = new CodedInputStream(
new SmallBlockInputStream(new byte[256], 8));
input.SetSizeLimit(16);
input.ReadRawBytes(16);
@ -366,7 +366,7 @@ namespace Google.Protobuf
public void ReadInvalidUtf8()
{
MemoryStream ms = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(ms);
CodedOutputStream output = new CodedOutputStream(ms);
uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
output.WriteRawVarint32(tag);
@ -375,7 +375,7 @@ namespace Google.Protobuf
output.Flush();
ms.Position = 0;
CodedInputStream input = CodedInputStream.CreateInstance(ms);
CodedInputStream input = new CodedInputStream(ms);
uint actualTag;
Assert.IsTrue(input.ReadTag(out actualTag));
@ -409,7 +409,7 @@ namespace Google.Protobuf
public void TestNegativeEnum()
{
byte[] bytes = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 };
CodedInputStream input = CodedInputStream.CreateInstance(bytes);
CodedInputStream input = new CodedInputStream(bytes);
Assert.AreEqual((int)SampleEnum.NegativeValue, input.ReadEnum());
Assert.IsTrue(input.IsAtEnd);
}
@ -420,7 +420,7 @@ namespace Google.Protobuf
{
using (var ms = new MemoryStream())
{
CodedOutputStream output = CodedOutputStream.CreateInstance(ms);
CodedOutputStream output = new CodedOutputStream(ms);
output.WriteTag(1, WireFormat.WireType.LengthDelimited);
output.WriteBytes(ByteString.CopyFrom(new byte[100]));
output.WriteTag(2, WireFormat.WireType.LengthDelimited);
@ -428,7 +428,7 @@ namespace Google.Protobuf
output.Flush();
ms.Position = 0;
CodedInputStream input = CodedInputStream.CreateInstance(ms, new byte[ms.Length / 2]);
CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2]);
uint tag;
Assert.IsTrue(input.ReadTag(out tag));

@ -49,7 +49,7 @@ namespace Google.Protobuf
if ((value >> 32) == 0)
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawVarint32((uint) value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
@ -59,7 +59,7 @@ namespace Google.Protobuf
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawVarint64(value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
@ -76,7 +76,7 @@ namespace Google.Protobuf
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output =
CodedOutputStream.CreateInstance(rawOutput, bufferSize);
new CodedOutputStream(rawOutput, bufferSize);
output.WriteRawVarint32((uint) value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
@ -84,7 +84,7 @@ namespace Google.Protobuf
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput, bufferSize);
CodedOutputStream output = new CodedOutputStream(rawOutput, bufferSize);
output.WriteRawVarint64(value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
@ -134,7 +134,7 @@ namespace Google.Protobuf
private static void AssertWriteLittleEndian32(byte[] data, uint value)
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawLittleEndian32(value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
@ -143,7 +143,7 @@ namespace Google.Protobuf
for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
{
rawOutput = new MemoryStream();
output = CodedOutputStream.CreateInstance(rawOutput, bufferSize);
output = new CodedOutputStream(rawOutput, bufferSize);
output.WriteRawLittleEndian32(value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
@ -157,7 +157,7 @@ namespace Google.Protobuf
private static void AssertWriteLittleEndian64(byte[] data, ulong value)
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawLittleEndian64(value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
@ -166,7 +166,7 @@ namespace Google.Protobuf
for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
{
rawOutput = new MemoryStream();
output = CodedOutputStream.CreateInstance(rawOutput, blockSize);
output = new CodedOutputStream(rawOutput, blockSize);
output.WriteRawLittleEndian64(value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
@ -201,7 +201,7 @@ namespace Google.Protobuf
for (int blockSize = 1; blockSize < 256; blockSize *= 2)
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput, blockSize);
CodedOutputStream output = new CodedOutputStream(rawOutput, blockSize);
message.WriteTo(output);
output.Flush();
Assert.AreEqual(rawBytes, rawOutput.ToArray());
@ -276,7 +276,7 @@ namespace Google.Protobuf
Assert.AreEqual(10, CodedOutputStream.ComputeEnumSize((int) SampleEnum.NegativeValue));
byte[] bytes = new byte[10];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
CodedOutputStream output = new CodedOutputStream(bytes);
output.WriteEnum((int) SampleEnum.NegativeValue);
Assert.AreEqual(0, output.SpaceLeft);
@ -293,7 +293,7 @@ namespace Google.Protobuf
byte[] child = new byte[120];
{
MemoryStream ms = new MemoryStream(child);
CodedOutputStream cout = CodedOutputStream.CreateInstance(ms, 20);
CodedOutputStream cout = new CodedOutputStream(ms, 20);
// Field 11: numeric value: 500
cout.WriteTag(11, WireFormat.WireType.Varint);
Assert.AreEqual(1, cout.Position);
@ -314,7 +314,7 @@ namespace Google.Protobuf
byte[] bytes = new byte[130];
{
CodedOutputStream cout = CodedOutputStream.CreateInstance(bytes);
CodedOutputStream cout = new CodedOutputStream(bytes);
// Field 1: numeric value: 500
cout.WriteTag(1, WireFormat.WireType.Varint);
Assert.AreEqual(1, cout.Position);
@ -334,7 +334,7 @@ namespace Google.Protobuf
}
// Now test Input stream:
{
CodedInputStream cin = CodedInputStream.CreateInstance(new MemoryStream(bytes), new byte[50]);
CodedInputStream cin = new CodedInputStream(new MemoryStream(bytes), new byte[50]);
uint tag;
Assert.AreEqual(0, cin.Position);
// Field 1:

@ -221,7 +221,7 @@ namespace Google.Protobuf.Collections
{
uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream);
var output = new CodedOutputStream(stream);
var length = CodedOutputStream.ComputeInt32Size(10)
+ CodedOutputStream.ComputeInt32Size(999)
+ CodedOutputStream.ComputeInt32Size(-1000);
@ -237,7 +237,7 @@ namespace Google.Protobuf.Collections
// actually packed.
uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<int>();
var input = CodedInputStream.CreateInstance(stream);
var input = new CodedInputStream(stream);
input.AssertNextTag(packedTag);
field.AddEntriesFrom(input, FieldCodec.ForInt32(nonPackedTag));
CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
@ -249,7 +249,7 @@ namespace Google.Protobuf.Collections
{
uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream);
var output = new CodedOutputStream(stream);
output.WriteTag(nonPackedTag);
output.WriteInt32(10);
output.WriteTag(nonPackedTag);
@ -263,7 +263,7 @@ namespace Google.Protobuf.Collections
// actually not packed.
uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<int>();
var input = CodedInputStream.CreateInstance(stream);
var input = new CodedInputStream(stream);
input.AssertNextTag(nonPackedTag);
field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag));
CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
@ -275,7 +275,7 @@ namespace Google.Protobuf.Collections
{
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream);
var output = new CodedOutputStream(stream);
output.WriteTag(tag);
output.WriteString("Foo");
output.WriteTag(tag);
@ -286,7 +286,7 @@ namespace Google.Protobuf.Collections
stream.Position = 0;
var field = new RepeatedField<string>();
var input = CodedInputStream.CreateInstance(stream);
var input = new CodedInputStream(stream);
input.AssertNextTag(tag);
field.AddEntriesFrom(input, FieldCodec.ForString(tag));
CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field);
@ -301,7 +301,7 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream);
var output = new CodedOutputStream(stream);
output.WriteTag(tag);
output.WriteMessage(message1);
output.WriteTag(tag);
@ -310,7 +310,7 @@ namespace Google.Protobuf.Collections
stream.Position = 0;
var field = new RepeatedField<ForeignMessage>();
var input = CodedInputStream.CreateInstance(stream);
var input = new CodedInputStream(stream);
input.AssertNextTag(tag);
field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
CollectionAssert.AreEqual(new[] { message1, message2}, field);
@ -323,12 +323,12 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<int> { 10, 1000, 1000000 };
var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream);
var output = new CodedOutputStream(stream);
field.WriteTo(output, FieldCodec.ForInt32(tag));
output.Flush();
stream.Position = 0;
var input = CodedInputStream.CreateInstance(stream);
var input = new CodedInputStream(stream);
input.AssertNextTag(tag);
var length = input.ReadLength();
Assert.AreEqual(10, input.ReadInt32());
@ -344,12 +344,12 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
var field = new RepeatedField<int> { 10, 1000, 1000000};
var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream);
var output = new CodedOutputStream(stream);
field.WriteTo(output, FieldCodec.ForInt32(tag));
output.Flush();
stream.Position = 0;
var input = CodedInputStream.CreateInstance(stream);
var input = new CodedInputStream(stream);
input.AssertNextTag(tag);
Assert.AreEqual(10, input.ReadInt32());
input.AssertNextTag(tag);
@ -365,12 +365,12 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<string> { "Foo", "", "Bar" };
var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream);
var output = new CodedOutputStream(stream);
field.WriteTo(output, FieldCodec.ForString(tag));
output.Flush();
stream.Position = 0;
var input = CodedInputStream.CreateInstance(stream);
var input = new CodedInputStream(stream);
input.AssertNextTag(tag);
Assert.AreEqual("Foo", input.ReadString());
input.AssertNextTag(tag);
@ -388,12 +388,12 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<ForeignMessage> { message1, message2 };
var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream);
var output = new CodedOutputStream(stream);
field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
output.Flush();
stream.Position = 0;
var input = CodedInputStream.CreateInstance(stream);
var input = new CodedInputStream(stream);
input.AssertNextTag(tag);
Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser));
input.AssertNextTag(tag);
@ -444,7 +444,7 @@ namespace Google.Protobuf.Collections
int arraySize = 1 + 1 + (11 * 5);
int msgSize = arraySize;
byte[] bytes = new byte[msgSize];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
CodedOutputStream output = new CodedOutputStream(bytes);
uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint);
for (int i = 0; i >= -5; i--)
{
@ -454,7 +454,7 @@ namespace Google.Protobuf.Collections
Assert.AreEqual(0, output.SpaceLeft);
CodedInputStream input = CodedInputStream.CreateInstance(bytes);
CodedInputStream input = new CodedInputStream(bytes);
Assert.IsTrue(input.ReadTag(out tag));
RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
@ -476,7 +476,7 @@ namespace Google.Protobuf.Collections
int arraySize = 1 + (10 * 5);
int msgSize = 1 + 1 + arraySize;
byte[] bytes = new byte[msgSize];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
CodedOutputStream output = new CodedOutputStream(bytes);
// Length-delimited to show we want the packed representation
uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited);
output.WriteTag(tag);
@ -492,7 +492,7 @@ namespace Google.Protobuf.Collections
}
Assert.AreEqual(0, output.SpaceLeft);
CodedInputStream input = CodedInputStream.CreateInstance(bytes);
CodedInputStream input = new CodedInputStream(bytes);
Assert.IsTrue(input.ReadTag(out tag));
RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();

@ -120,11 +120,11 @@ namespace Google.Protobuf
public void TestRoundTripRaw()
{
var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream);
var codedOutput = new CodedOutputStream(stream);
codec.ValueWriter(codedOutput, sampleValue);
codedOutput.Flush();
stream.Position = 0;
var codedInput = CodedInputStream.CreateInstance(stream);
var codedInput = new CodedInputStream(stream);
Assert.AreEqual(sampleValue, codec.ValueReader(codedInput));
Assert.IsTrue(codedInput.IsAtEnd);
}
@ -132,11 +132,11 @@ namespace Google.Protobuf
public void TestRoundTripWithTag()
{
var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream);
var codedOutput = new CodedOutputStream(stream);
codec.WriteTagAndValue(codedOutput, sampleValue);
codedOutput.Flush();
stream.Position = 0;
var codedInput = CodedInputStream.CreateInstance(stream);
var codedInput = new CodedInputStream(stream);
codedInput.AssertNextTag(codec.Tag);
Assert.AreEqual(sampleValue, codec.Read(codedInput));
Assert.IsTrue(codedInput.IsAtEnd);
@ -145,7 +145,7 @@ namespace Google.Protobuf
public void TestCalculateSizeWithTag()
{
var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream);
var codedOutput = new CodedOutputStream(stream);
codec.WriteTagAndValue(codedOutput, sampleValue);
codedOutput.Flush();
Assert.AreEqual(stream.Position, codec.CalculateSizeWithTag(sampleValue));
@ -155,7 +155,7 @@ namespace Google.Protobuf
{
// WriteTagAndValue ignores default values
var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream);
var codedOutput = new CodedOutputStream(stream);
codec.WriteTagAndValue(codedOutput, codec.DefaultValue);
codedOutput.Flush();
Assert.AreEqual(0, stream.Position);
@ -168,13 +168,13 @@ namespace Google.Protobuf
// The plain ValueWriter/ValueReader delegates don't.
if (codec.DefaultValue != null) // This part isn't appropriate for message types.
{
codedOutput = CodedOutputStream.CreateInstance(stream);
codedOutput = new CodedOutputStream(stream);
codec.ValueWriter(codedOutput, codec.DefaultValue);
codedOutput.Flush();
Assert.AreNotEqual(0, stream.Position);
Assert.AreEqual(stream.Position, codec.ValueSizeCalculator(codec.DefaultValue));
stream.Position = 0;
var codedInput = CodedInputStream.CreateInstance(stream);
var codedInput = new CodedInputStream(stream);
Assert.AreEqual(codec.DefaultValue, codec.ValueReader(codedInput));
}
}

@ -253,7 +253,7 @@ namespace Google.Protobuf
{
// Hand-craft the stream to contain a single entry with just a value.
var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream);
var output = new CodedOutputStream(memoryStream);
output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited);
var nestedMessage = new ForeignMessage { C = 20 };
// Size of the entry (tag, size written by WriteMessage, data written by WriteMessage)
@ -271,7 +271,7 @@ namespace Google.Protobuf
{
// Hand-craft the stream to contain a single entry with three fields
var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream);
var output = new CodedOutputStream(memoryStream);
output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
@ -298,7 +298,7 @@ namespace Google.Protobuf
public void MapFieldOrderIsIrrelevant()
{
var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream);
var output = new CodedOutputStream(memoryStream);
output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
@ -322,7 +322,7 @@ namespace Google.Protobuf
public void MapNonContiguousEntries()
{
var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream);
var output = new CodedOutputStream(memoryStream);
// Message structure:
// Entry for MapInt32Int32
@ -373,7 +373,7 @@ namespace Google.Protobuf
public void DuplicateKeys_LastEntryWins()
{
var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream);
var output = new CodedOutputStream(memoryStream);
var key = 10;
var value1 = 20;

@ -50,7 +50,7 @@ namespace Google.Protobuf
Assert.AreEqual(58, msg.CalculateSize());
byte[] bytes = new byte[58];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
CodedOutputStream output = new CodedOutputStream(bytes);
msg.WriteTo(output);
Assert.AreEqual(0, output.SpaceLeft);

@ -309,7 +309,7 @@ namespace Google.Protobuf.WellKnownTypes
var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint);
using (var stream = new MemoryStream())
{
var coded = CodedOutputStream.CreateInstance(stream);
var coded = new CodedOutputStream(stream);
coded.WriteTag(wrapperTag);
coded.WriteLength(2); // valueTag + a value 0, each one byte
coded.WriteTag(valueTag);

@ -203,7 +203,7 @@ namespace Google.Protobuf
public CodedInputStream CreateCodedInput()
{
// We trust CodedInputStream not to reveal the provided byte array or modify it
return CodedInputStream.CreateInstance(bytes);
return new CodedInputStream(bytes);
}
public static bool operator ==(ByteString lhs, ByteString rhs)

@ -93,43 +93,19 @@ namespace Google.Protobuf
private int sizeLimit = DefaultSizeLimit;
#region Construction
/// <summary>
/// Creates a new CodedInputStream reading data from the given
/// stream.
/// </summary>
public static CodedInputStream CreateInstance(Stream input)
{
return new CodedInputStream(input);
}
/// <summary>
/// Creates a new CodedInputStream reading data from the given
/// stream and a pre-allocated memory buffer.
/// </summary>
public static CodedInputStream CreateInstance(Stream input, byte[] buffer)
{
return new CodedInputStream(input, buffer);
}
/// <summary>
/// Creates a new CodedInputStream reading data from the given
/// byte array.
/// </summary>
public static CodedInputStream CreateInstance(byte[] buf)
public CodedInputStream(byte[] buf) : this(buf, 0, buf.Length)
{
return new CodedInputStream(buf, 0, buf.Length);
}
/// <summary>
/// Creates a new CodedInputStream that reads from the given
/// byte array slice.
/// </summary>
public static CodedInputStream CreateInstance(byte[] buf, int offset, int length)
{
return new CodedInputStream(buf, offset, length);
}
private CodedInputStream(byte[] buffer, int offset, int length)
public CodedInputStream(byte[] buffer, int offset, int length)
{
this.buffer = buffer;
this.bufferPos = offset;
@ -137,14 +113,21 @@ namespace Google.Protobuf
this.input = null;
}
private CodedInputStream(Stream input)
/// <summary>
/// Creates a new CodedInputStream reading data from the given stream.
/// </summary>
public CodedInputStream(Stream input)
{
this.buffer = new byte[BufferSize];
this.bufferSize = 0;
this.input = input;
}
private CodedInputStream(Stream input, byte[] buffer)
/// <summary>
/// Creates a new CodedInputStream reading data from the given
/// stream, with a pre-allocated buffer.
/// </summary>
internal CodedInputStream(Stream input, byte[] buffer)
{
this.buffer = buffer;
this.bufferSize = 0;

@ -65,7 +65,20 @@ namespace Google.Protobuf
private readonly Stream output;
#region Construction
/// <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 CodedOutputStream(byte[] flatArray) : this(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>
private CodedOutputStream(byte[] buffer, int offset, int length)
{
this.output = null;
@ -85,40 +98,17 @@ namespace Google.Protobuf
/// <summary>
/// Creates a new CodedOutputStream which write to the given stream.
/// </summary>
public static CodedOutputStream CreateInstance(Stream output)
public CodedOutputStream(Stream output) : this(output, DefaultBufferSize)
{
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)
public CodedOutputStream(Stream output, int bufferSize) : this(output, new byte[bufferSize])
{
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>

@ -43,7 +43,7 @@ namespace Google.Protobuf
{
Preconditions.CheckNotNull(message, "message");
Preconditions.CheckNotNull(data, "data");
CodedInputStream input = CodedInputStream.CreateInstance(data);
CodedInputStream input = new CodedInputStream(data);
message.MergeFrom(input);
input.CheckLastTagWas(0);
}
@ -61,7 +61,7 @@ namespace Google.Protobuf
{
Preconditions.CheckNotNull(message, "message");
Preconditions.CheckNotNull(input, "input");
CodedInputStream codedInput = CodedInputStream.CreateInstance(input);
CodedInputStream codedInput = new CodedInputStream(input);
message.MergeFrom(codedInput);
codedInput.CheckLastTagWas(0);
}
@ -79,7 +79,7 @@ namespace Google.Protobuf
{
Preconditions.CheckNotNull(message, "message");
byte[] result = new byte[message.CalculateSize()];
CodedOutputStream output = CodedOutputStream.CreateInstance(result);
CodedOutputStream output = new CodedOutputStream(result);
message.WriteTo(output);
output.CheckNoSpaceLeft();
return result;
@ -89,7 +89,7 @@ namespace Google.Protobuf
{
Preconditions.CheckNotNull(message, "message");
Preconditions.CheckNotNull(output, "output");
CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
CodedOutputStream codedOutput = new CodedOutputStream(output);
message.WriteTo(codedOutput);
codedOutput.Flush();
}
@ -98,7 +98,7 @@ namespace Google.Protobuf
{
Preconditions.CheckNotNull(message, "message");
Preconditions.CheckNotNull(output, "output");
CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
CodedOutputStream codedOutput = new CodedOutputStream(output);
codedOutput.WriteRawVarint32((uint)message.CalculateSize());
message.WriteTo(codedOutput);
codedOutput.Flush();

Loading…
Cancel
Save