Commit db9f47a3 authored by Jon Skeet's avatar Jon Skeet

Merge pull request #678 from jskeet/stream-ctor

Expose Coded*Stream constructors directly.
parents bf1cc921 0e0e0c97
...@@ -59,20 +59,20 @@ namespace Google.Protobuf ...@@ -59,20 +59,20 @@ namespace Google.Protobuf
/// </summary> /// </summary>
private static void AssertReadVarint(byte[] data, ulong value) private static void AssertReadVarint(byte[] data, ulong value)
{ {
CodedInputStream input = CodedInputStream.CreateInstance(data); CodedInputStream input = new CodedInputStream(data);
Assert.AreEqual((uint) value, input.ReadRawVarint32()); Assert.AreEqual((uint) value, input.ReadRawVarint32());
input = CodedInputStream.CreateInstance(data); input = new CodedInputStream(data);
Assert.AreEqual(value, input.ReadRawVarint64()); Assert.AreEqual(value, input.ReadRawVarint64());
Assert.IsTrue(input.IsAtEnd); Assert.IsTrue(input.IsAtEnd);
// Try different block sizes. // Try different block sizes.
for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) 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()); 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.AreEqual(value, input.ReadRawVarint64());
Assert.IsTrue(input.IsAtEnd); Assert.IsTrue(input.IsAtEnd);
} }
...@@ -95,11 +95,11 @@ namespace Google.Protobuf ...@@ -95,11 +95,11 @@ namespace Google.Protobuf
/// </summary> /// </summary>
private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data) 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()); var exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint32());
Assert.AreEqual(expected.Message, exception.Message); Assert.AreEqual(expected.Message, exception.Message);
input = CodedInputStream.CreateInstance(data); input = new CodedInputStream(data);
exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint64()); exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint64());
Assert.AreEqual(expected.Message, exception.Message); Assert.AreEqual(expected.Message, exception.Message);
...@@ -152,14 +152,14 @@ namespace Google.Protobuf ...@@ -152,14 +152,14 @@ namespace Google.Protobuf
/// </summary> /// </summary>
private static void AssertReadLittleEndian32(byte[] data, uint value) private static void AssertReadLittleEndian32(byte[] data, uint value)
{ {
CodedInputStream input = CodedInputStream.CreateInstance(data); CodedInputStream input = new CodedInputStream(data);
Assert.AreEqual(value, input.ReadRawLittleEndian32()); Assert.AreEqual(value, input.ReadRawLittleEndian32());
Assert.IsTrue(input.IsAtEnd); Assert.IsTrue(input.IsAtEnd);
// Try different block sizes. // Try different block sizes.
for (int blockSize = 1; blockSize <= 16; blockSize *= 2) for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
{ {
input = CodedInputStream.CreateInstance( input = new CodedInputStream(
new SmallBlockInputStream(data, blockSize)); new SmallBlockInputStream(data, blockSize));
Assert.AreEqual(value, input.ReadRawLittleEndian32()); Assert.AreEqual(value, input.ReadRawLittleEndian32());
Assert.IsTrue(input.IsAtEnd); Assert.IsTrue(input.IsAtEnd);
...@@ -172,14 +172,14 @@ namespace Google.Protobuf ...@@ -172,14 +172,14 @@ namespace Google.Protobuf
/// </summary> /// </summary>
private static void AssertReadLittleEndian64(byte[] data, ulong value) private static void AssertReadLittleEndian64(byte[] data, ulong value)
{ {
CodedInputStream input = CodedInputStream.CreateInstance(data); CodedInputStream input = new CodedInputStream(data);
Assert.AreEqual(value, input.ReadRawLittleEndian64()); Assert.AreEqual(value, input.ReadRawLittleEndian64());
Assert.IsTrue(input.IsAtEnd); Assert.IsTrue(input.IsAtEnd);
// Try different block sizes. // Try different block sizes.
for (int blockSize = 1; blockSize <= 16; blockSize *= 2) for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
{ {
input = CodedInputStream.CreateInstance( input = new CodedInputStream(
new SmallBlockInputStream(data, blockSize)); new SmallBlockInputStream(data, blockSize));
Assert.AreEqual(value, input.ReadRawLittleEndian64()); Assert.AreEqual(value, input.ReadRawLittleEndian64());
Assert.IsTrue(input.IsAtEnd); Assert.IsTrue(input.IsAtEnd);
...@@ -269,7 +269,7 @@ namespace Google.Protobuf ...@@ -269,7 +269,7 @@ namespace Google.Protobuf
public void ReadMaliciouslyLargeBlob() public void ReadMaliciouslyLargeBlob()
{ {
MemoryStream ms = new MemoryStream(); MemoryStream ms = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(ms); CodedOutputStream output = new CodedOutputStream(ms);
uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
output.WriteRawVarint32(tag); output.WriteRawVarint32(tag);
...@@ -278,7 +278,7 @@ namespace Google.Protobuf ...@@ -278,7 +278,7 @@ namespace Google.Protobuf
output.Flush(); output.Flush();
ms.Position = 0; ms.Position = 0;
CodedInputStream input = CodedInputStream.CreateInstance(ms); CodedInputStream input = new CodedInputStream(ms);
uint testtag; uint testtag;
Assert.IsTrue(input.ReadTag(out testtag)); Assert.IsTrue(input.ReadTag(out testtag));
Assert.AreEqual(tag, testtag); Assert.AreEqual(tag, testtag);
...@@ -335,7 +335,7 @@ namespace Google.Protobuf ...@@ -335,7 +335,7 @@ namespace Google.Protobuf
// Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't
// apply to the latter case. // apply to the latter case.
MemoryStream ms = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray()); MemoryStream ms = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray());
CodedInputStream input = CodedInputStream.CreateInstance(ms); CodedInputStream input = new CodedInputStream(ms);
input.SetSizeLimit(16); input.SetSizeLimit(16);
Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.ParseFrom(input)); Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.ParseFrom(input));
...@@ -344,7 +344,7 @@ namespace Google.Protobuf ...@@ -344,7 +344,7 @@ namespace Google.Protobuf
[Test] [Test]
public void ResetSizeCounter() public void ResetSizeCounter()
{ {
CodedInputStream input = CodedInputStream.CreateInstance( CodedInputStream input = new CodedInputStream(
new SmallBlockInputStream(new byte[256], 8)); new SmallBlockInputStream(new byte[256], 8));
input.SetSizeLimit(16); input.SetSizeLimit(16);
input.ReadRawBytes(16); input.ReadRawBytes(16);
...@@ -366,7 +366,7 @@ namespace Google.Protobuf ...@@ -366,7 +366,7 @@ namespace Google.Protobuf
public void ReadInvalidUtf8() public void ReadInvalidUtf8()
{ {
MemoryStream ms = new MemoryStream(); MemoryStream ms = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(ms); CodedOutputStream output = new CodedOutputStream(ms);
uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
output.WriteRawVarint32(tag); output.WriteRawVarint32(tag);
...@@ -375,7 +375,7 @@ namespace Google.Protobuf ...@@ -375,7 +375,7 @@ namespace Google.Protobuf
output.Flush(); output.Flush();
ms.Position = 0; ms.Position = 0;
CodedInputStream input = CodedInputStream.CreateInstance(ms); CodedInputStream input = new CodedInputStream(ms);
uint actualTag; uint actualTag;
Assert.IsTrue(input.ReadTag(out actualTag)); Assert.IsTrue(input.ReadTag(out actualTag));
...@@ -409,7 +409,7 @@ namespace Google.Protobuf ...@@ -409,7 +409,7 @@ namespace Google.Protobuf
public void TestNegativeEnum() public void TestNegativeEnum()
{ {
byte[] bytes = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 }; 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.AreEqual((int)SampleEnum.NegativeValue, input.ReadEnum());
Assert.IsTrue(input.IsAtEnd); Assert.IsTrue(input.IsAtEnd);
} }
...@@ -420,7 +420,7 @@ namespace Google.Protobuf ...@@ -420,7 +420,7 @@ namespace Google.Protobuf
{ {
using (var ms = new MemoryStream()) using (var ms = new MemoryStream())
{ {
CodedOutputStream output = CodedOutputStream.CreateInstance(ms); CodedOutputStream output = new CodedOutputStream(ms);
output.WriteTag(1, WireFormat.WireType.LengthDelimited); output.WriteTag(1, WireFormat.WireType.LengthDelimited);
output.WriteBytes(ByteString.CopyFrom(new byte[100])); output.WriteBytes(ByteString.CopyFrom(new byte[100]));
output.WriteTag(2, WireFormat.WireType.LengthDelimited); output.WriteTag(2, WireFormat.WireType.LengthDelimited);
...@@ -428,7 +428,7 @@ namespace Google.Protobuf ...@@ -428,7 +428,7 @@ namespace Google.Protobuf
output.Flush(); output.Flush();
ms.Position = 0; 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; uint tag;
Assert.IsTrue(input.ReadTag(out tag)); Assert.IsTrue(input.ReadTag(out tag));
......
...@@ -49,7 +49,7 @@ namespace Google.Protobuf ...@@ -49,7 +49,7 @@ namespace Google.Protobuf
if ((value >> 32) == 0) if ((value >> 32) == 0)
{ {
MemoryStream rawOutput = new MemoryStream(); MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput); CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawVarint32((uint) value); output.WriteRawVarint32((uint) value);
output.Flush(); output.Flush();
Assert.AreEqual(data, rawOutput.ToArray()); Assert.AreEqual(data, rawOutput.ToArray());
...@@ -59,7 +59,7 @@ namespace Google.Protobuf ...@@ -59,7 +59,7 @@ namespace Google.Protobuf
{ {
MemoryStream rawOutput = new MemoryStream(); MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput); CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawVarint64(value); output.WriteRawVarint64(value);
output.Flush(); output.Flush();
Assert.AreEqual(data, rawOutput.ToArray()); Assert.AreEqual(data, rawOutput.ToArray());
...@@ -76,7 +76,7 @@ namespace Google.Protobuf ...@@ -76,7 +76,7 @@ namespace Google.Protobuf
{ {
MemoryStream rawOutput = new MemoryStream(); MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream output =
CodedOutputStream.CreateInstance(rawOutput, bufferSize); new CodedOutputStream(rawOutput, bufferSize);
output.WriteRawVarint32((uint) value); output.WriteRawVarint32((uint) value);
output.Flush(); output.Flush();
Assert.AreEqual(data, rawOutput.ToArray()); Assert.AreEqual(data, rawOutput.ToArray());
...@@ -84,7 +84,7 @@ namespace Google.Protobuf ...@@ -84,7 +84,7 @@ namespace Google.Protobuf
{ {
MemoryStream rawOutput = new MemoryStream(); MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput, bufferSize); CodedOutputStream output = new CodedOutputStream(rawOutput, bufferSize);
output.WriteRawVarint64(value); output.WriteRawVarint64(value);
output.Flush(); output.Flush();
Assert.AreEqual(data, rawOutput.ToArray()); Assert.AreEqual(data, rawOutput.ToArray());
...@@ -134,7 +134,7 @@ namespace Google.Protobuf ...@@ -134,7 +134,7 @@ namespace Google.Protobuf
private static void AssertWriteLittleEndian32(byte[] data, uint value) private static void AssertWriteLittleEndian32(byte[] data, uint value)
{ {
MemoryStream rawOutput = new MemoryStream(); MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput); CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawLittleEndian32(value); output.WriteRawLittleEndian32(value);
output.Flush(); output.Flush();
Assert.AreEqual(data, rawOutput.ToArray()); Assert.AreEqual(data, rawOutput.ToArray());
...@@ -143,7 +143,7 @@ namespace Google.Protobuf ...@@ -143,7 +143,7 @@ namespace Google.Protobuf
for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
{ {
rawOutput = new MemoryStream(); rawOutput = new MemoryStream();
output = CodedOutputStream.CreateInstance(rawOutput, bufferSize); output = new CodedOutputStream(rawOutput, bufferSize);
output.WriteRawLittleEndian32(value); output.WriteRawLittleEndian32(value);
output.Flush(); output.Flush();
Assert.AreEqual(data, rawOutput.ToArray()); Assert.AreEqual(data, rawOutput.ToArray());
...@@ -157,7 +157,7 @@ namespace Google.Protobuf ...@@ -157,7 +157,7 @@ namespace Google.Protobuf
private static void AssertWriteLittleEndian64(byte[] data, ulong value) private static void AssertWriteLittleEndian64(byte[] data, ulong value)
{ {
MemoryStream rawOutput = new MemoryStream(); MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput); CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawLittleEndian64(value); output.WriteRawLittleEndian64(value);
output.Flush(); output.Flush();
Assert.AreEqual(data, rawOutput.ToArray()); Assert.AreEqual(data, rawOutput.ToArray());
...@@ -166,7 +166,7 @@ namespace Google.Protobuf ...@@ -166,7 +166,7 @@ namespace Google.Protobuf
for (int blockSize = 1; blockSize <= 16; blockSize *= 2) for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
{ {
rawOutput = new MemoryStream(); rawOutput = new MemoryStream();
output = CodedOutputStream.CreateInstance(rawOutput, blockSize); output = new CodedOutputStream(rawOutput, blockSize);
output.WriteRawLittleEndian64(value); output.WriteRawLittleEndian64(value);
output.Flush(); output.Flush();
Assert.AreEqual(data, rawOutput.ToArray()); Assert.AreEqual(data, rawOutput.ToArray());
...@@ -201,7 +201,7 @@ namespace Google.Protobuf ...@@ -201,7 +201,7 @@ namespace Google.Protobuf
for (int blockSize = 1; blockSize < 256; blockSize *= 2) for (int blockSize = 1; blockSize < 256; blockSize *= 2)
{ {
MemoryStream rawOutput = new MemoryStream(); MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput, blockSize); CodedOutputStream output = new CodedOutputStream(rawOutput, blockSize);
message.WriteTo(output); message.WriteTo(output);
output.Flush(); output.Flush();
Assert.AreEqual(rawBytes, rawOutput.ToArray()); Assert.AreEqual(rawBytes, rawOutput.ToArray());
...@@ -276,7 +276,7 @@ namespace Google.Protobuf ...@@ -276,7 +276,7 @@ namespace Google.Protobuf
Assert.AreEqual(10, CodedOutputStream.ComputeEnumSize((int) SampleEnum.NegativeValue)); Assert.AreEqual(10, CodedOutputStream.ComputeEnumSize((int) SampleEnum.NegativeValue));
byte[] bytes = new byte[10]; byte[] bytes = new byte[10];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes); CodedOutputStream output = new CodedOutputStream(bytes);
output.WriteEnum((int) SampleEnum.NegativeValue); output.WriteEnum((int) SampleEnum.NegativeValue);
Assert.AreEqual(0, output.SpaceLeft); Assert.AreEqual(0, output.SpaceLeft);
...@@ -293,7 +293,7 @@ namespace Google.Protobuf ...@@ -293,7 +293,7 @@ namespace Google.Protobuf
byte[] child = new byte[120]; byte[] child = new byte[120];
{ {
MemoryStream ms = new MemoryStream(child); MemoryStream ms = new MemoryStream(child);
CodedOutputStream cout = CodedOutputStream.CreateInstance(ms, 20); CodedOutputStream cout = new CodedOutputStream(ms, 20);
// Field 11: numeric value: 500 // Field 11: numeric value: 500
cout.WriteTag(11, WireFormat.WireType.Varint); cout.WriteTag(11, WireFormat.WireType.Varint);
Assert.AreEqual(1, cout.Position); Assert.AreEqual(1, cout.Position);
...@@ -314,7 +314,7 @@ namespace Google.Protobuf ...@@ -314,7 +314,7 @@ namespace Google.Protobuf
byte[] bytes = new byte[130]; byte[] bytes = new byte[130];
{ {
CodedOutputStream cout = CodedOutputStream.CreateInstance(bytes); CodedOutputStream cout = new CodedOutputStream(bytes);
// Field 1: numeric value: 500 // Field 1: numeric value: 500
cout.WriteTag(1, WireFormat.WireType.Varint); cout.WriteTag(1, WireFormat.WireType.Varint);
Assert.AreEqual(1, cout.Position); Assert.AreEqual(1, cout.Position);
...@@ -334,7 +334,7 @@ namespace Google.Protobuf ...@@ -334,7 +334,7 @@ namespace Google.Protobuf
} }
// Now test Input stream: // 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; uint tag;
Assert.AreEqual(0, cin.Position); Assert.AreEqual(0, cin.Position);
// Field 1: // Field 1:
......
...@@ -221,7 +221,7 @@ namespace Google.Protobuf.Collections ...@@ -221,7 +221,7 @@ namespace Google.Protobuf.Collections
{ {
uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var stream = new MemoryStream(); var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream); var output = new CodedOutputStream(stream);
var length = CodedOutputStream.ComputeInt32Size(10) var length = CodedOutputStream.ComputeInt32Size(10)
+ CodedOutputStream.ComputeInt32Size(999) + CodedOutputStream.ComputeInt32Size(999)
+ CodedOutputStream.ComputeInt32Size(-1000); + CodedOutputStream.ComputeInt32Size(-1000);
...@@ -237,7 +237,7 @@ namespace Google.Protobuf.Collections ...@@ -237,7 +237,7 @@ namespace Google.Protobuf.Collections
// actually packed. // actually packed.
uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<int>(); var field = new RepeatedField<int>();
var input = CodedInputStream.CreateInstance(stream); var input = new CodedInputStream(stream);
input.AssertNextTag(packedTag); input.AssertNextTag(packedTag);
field.AddEntriesFrom(input, FieldCodec.ForInt32(nonPackedTag)); field.AddEntriesFrom(input, FieldCodec.ForInt32(nonPackedTag));
CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field); CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
...@@ -249,7 +249,7 @@ namespace Google.Protobuf.Collections ...@@ -249,7 +249,7 @@ namespace Google.Protobuf.Collections
{ {
uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint); uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
var stream = new MemoryStream(); var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream); var output = new CodedOutputStream(stream);
output.WriteTag(nonPackedTag); output.WriteTag(nonPackedTag);
output.WriteInt32(10); output.WriteInt32(10);
output.WriteTag(nonPackedTag); output.WriteTag(nonPackedTag);
...@@ -263,7 +263,7 @@ namespace Google.Protobuf.Collections ...@@ -263,7 +263,7 @@ namespace Google.Protobuf.Collections
// actually not packed. // actually not packed.
uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<int>(); var field = new RepeatedField<int>();
var input = CodedInputStream.CreateInstance(stream); var input = new CodedInputStream(stream);
input.AssertNextTag(nonPackedTag); input.AssertNextTag(nonPackedTag);
field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag)); field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag));
CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field); CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
...@@ -275,7 +275,7 @@ namespace Google.Protobuf.Collections ...@@ -275,7 +275,7 @@ namespace Google.Protobuf.Collections
{ {
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var stream = new MemoryStream(); var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream); var output = new CodedOutputStream(stream);
output.WriteTag(tag); output.WriteTag(tag);
output.WriteString("Foo"); output.WriteString("Foo");
output.WriteTag(tag); output.WriteTag(tag);
...@@ -286,7 +286,7 @@ namespace Google.Protobuf.Collections ...@@ -286,7 +286,7 @@ namespace Google.Protobuf.Collections
stream.Position = 0; stream.Position = 0;
var field = new RepeatedField<string>(); var field = new RepeatedField<string>();
var input = CodedInputStream.CreateInstance(stream); var input = new CodedInputStream(stream);
input.AssertNextTag(tag); input.AssertNextTag(tag);
field.AddEntriesFrom(input, FieldCodec.ForString(tag)); field.AddEntriesFrom(input, FieldCodec.ForString(tag));
CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field); CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field);
...@@ -301,7 +301,7 @@ namespace Google.Protobuf.Collections ...@@ -301,7 +301,7 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var stream = new MemoryStream(); var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream); var output = new CodedOutputStream(stream);
output.WriteTag(tag); output.WriteTag(tag);
output.WriteMessage(message1); output.WriteMessage(message1);
output.WriteTag(tag); output.WriteTag(tag);
...@@ -310,7 +310,7 @@ namespace Google.Protobuf.Collections ...@@ -310,7 +310,7 @@ namespace Google.Protobuf.Collections
stream.Position = 0; stream.Position = 0;
var field = new RepeatedField<ForeignMessage>(); var field = new RepeatedField<ForeignMessage>();
var input = CodedInputStream.CreateInstance(stream); var input = new CodedInputStream(stream);
input.AssertNextTag(tag); input.AssertNextTag(tag);
field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser)); field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
CollectionAssert.AreEqual(new[] { message1, message2}, field); CollectionAssert.AreEqual(new[] { message1, message2}, field);
...@@ -323,12 +323,12 @@ namespace Google.Protobuf.Collections ...@@ -323,12 +323,12 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<int> { 10, 1000, 1000000 }; var field = new RepeatedField<int> { 10, 1000, 1000000 };
var stream = new MemoryStream(); var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream); var output = new CodedOutputStream(stream);
field.WriteTo(output, FieldCodec.ForInt32(tag)); field.WriteTo(output, FieldCodec.ForInt32(tag));
output.Flush(); output.Flush();
stream.Position = 0; stream.Position = 0;
var input = CodedInputStream.CreateInstance(stream); var input = new CodedInputStream(stream);
input.AssertNextTag(tag); input.AssertNextTag(tag);
var length = input.ReadLength(); var length = input.ReadLength();
Assert.AreEqual(10, input.ReadInt32()); Assert.AreEqual(10, input.ReadInt32());
...@@ -344,12 +344,12 @@ namespace Google.Protobuf.Collections ...@@ -344,12 +344,12 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.Varint); uint tag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
var field = new RepeatedField<int> { 10, 1000, 1000000}; var field = new RepeatedField<int> { 10, 1000, 1000000};
var stream = new MemoryStream(); var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream); var output = new CodedOutputStream(stream);
field.WriteTo(output, FieldCodec.ForInt32(tag)); field.WriteTo(output, FieldCodec.ForInt32(tag));
output.Flush(); output.Flush();
stream.Position = 0; stream.Position = 0;
var input = CodedInputStream.CreateInstance(stream); var input = new CodedInputStream(stream);
input.AssertNextTag(tag); input.AssertNextTag(tag);
Assert.AreEqual(10, input.ReadInt32()); Assert.AreEqual(10, input.ReadInt32());
input.AssertNextTag(tag); input.AssertNextTag(tag);
...@@ -365,12 +365,12 @@ namespace Google.Protobuf.Collections ...@@ -365,12 +365,12 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<string> { "Foo", "", "Bar" }; var field = new RepeatedField<string> { "Foo", "", "Bar" };
var stream = new MemoryStream(); var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream); var output = new CodedOutputStream(stream);
field.WriteTo(output, FieldCodec.ForString(tag)); field.WriteTo(output, FieldCodec.ForString(tag));
output.Flush(); output.Flush();
stream.Position = 0; stream.Position = 0;
var input = CodedInputStream.CreateInstance(stream); var input = new CodedInputStream(stream);
input.AssertNextTag(tag); input.AssertNextTag(tag);
Assert.AreEqual("Foo", input.ReadString()); Assert.AreEqual("Foo", input.ReadString());
input.AssertNextTag(tag); input.AssertNextTag(tag);
...@@ -388,12 +388,12 @@ namespace Google.Protobuf.Collections ...@@ -388,12 +388,12 @@ namespace Google.Protobuf.Collections
uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
var field = new RepeatedField<ForeignMessage> { message1, message2 }; var field = new RepeatedField<ForeignMessage> { message1, message2 };
var stream = new MemoryStream(); var stream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(stream); var output = new CodedOutputStream(stream);
field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Parser)); field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
output.Flush(); output.Flush();
stream.Position = 0; stream.Position = 0;
var input = CodedInputStream.CreateInstance(stream); var input = new CodedInputStream(stream);
input.AssertNextTag(tag); input.AssertNextTag(tag);
Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser)); Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser));
input.AssertNextTag(tag); input.AssertNextTag(tag);
...@@ -444,7 +444,7 @@ namespace Google.Protobuf.Collections ...@@ -444,7 +444,7 @@ namespace Google.Protobuf.Collections
int arraySize = 1 + 1 + (11 * 5); int arraySize = 1 + 1 + (11 * 5);
int msgSize = arraySize; int msgSize = arraySize;
byte[] bytes = new byte[msgSize]; byte[] bytes = new byte[msgSize];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes); CodedOutputStream output = new CodedOutputStream(bytes);
uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint); uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint);
for (int i = 0; i >= -5; i--) for (int i = 0; i >= -5; i--)
{ {
...@@ -454,7 +454,7 @@ namespace Google.Protobuf.Collections ...@@ -454,7 +454,7 @@ namespace Google.Protobuf.Collections
Assert.AreEqual(0, output.SpaceLeft); Assert.AreEqual(0, output.SpaceLeft);
CodedInputStream input = CodedInputStream.CreateInstance(bytes); CodedInputStream input = new CodedInputStream(bytes);
Assert.IsTrue(input.ReadTag(out tag)); Assert.IsTrue(input.ReadTag(out tag));
RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>(); RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
...@@ -476,7 +476,7 @@ namespace Google.Protobuf.Collections ...@@ -476,7 +476,7 @@ namespace Google.Protobuf.Collections
int arraySize = 1 + (10 * 5); int arraySize = 1 + (10 * 5);
int msgSize = 1 + 1 + arraySize; int msgSize = 1 + 1 + arraySize;
byte[] bytes = new byte[msgSize]; byte[] bytes = new byte[msgSize];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes); CodedOutputStream output = new CodedOutputStream(bytes);
// Length-delimited to show we want the packed representation // Length-delimited to show we want the packed representation
uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited); uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited);
output.WriteTag(tag); output.WriteTag(tag);
...@@ -492,7 +492,7 @@ namespace Google.Protobuf.Collections ...@@ -492,7 +492,7 @@ namespace Google.Protobuf.Collections
} }
Assert.AreEqual(0, output.SpaceLeft); Assert.AreEqual(0, output.SpaceLeft);
CodedInputStream input = CodedInputStream.CreateInstance(bytes); CodedInputStream input = new CodedInputStream(bytes);
Assert.IsTrue(input.ReadTag(out tag)); Assert.IsTrue(input.ReadTag(out tag));
RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>(); RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
......
...@@ -120,11 +120,11 @@ namespace Google.Protobuf ...@@ -120,11 +120,11 @@ namespace Google.Protobuf
public void TestRoundTripRaw() public void TestRoundTripRaw()
{ {
var stream = new MemoryStream(); var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream); var codedOutput = new CodedOutputStream(stream);
codec.ValueWriter(codedOutput, sampleValue); codec.ValueWriter(codedOutput, sampleValue);
codedOutput.Flush(); codedOutput.Flush();
stream.Position = 0; stream.Position = 0;
var codedInput = CodedInputStream.CreateInstance(stream); var codedInput = new CodedInputStream(stream);
Assert.AreEqual(sampleValue, codec.ValueReader(codedInput)); Assert.AreEqual(sampleValue, codec.ValueReader(codedInput));
Assert.IsTrue(codedInput.IsAtEnd); Assert.IsTrue(codedInput.IsAtEnd);
} }
...@@ -132,11 +132,11 @@ namespace Google.Protobuf ...@@ -132,11 +132,11 @@ namespace Google.Protobuf
public void TestRoundTripWithTag() public void TestRoundTripWithTag()
{ {
var stream = new MemoryStream(); var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream); var codedOutput = new CodedOutputStream(stream);
codec.WriteTagAndValue(codedOutput, sampleValue); codec.WriteTagAndValue(codedOutput, sampleValue);
codedOutput.Flush(); codedOutput.Flush();
stream.Position = 0; stream.Position = 0;
var codedInput = CodedInputStream.CreateInstance(stream); var codedInput = new CodedInputStream(stream);
codedInput.AssertNextTag(codec.Tag); codedInput.AssertNextTag(codec.Tag);
Assert.AreEqual(sampleValue, codec.Read(codedInput)); Assert.AreEqual(sampleValue, codec.Read(codedInput));
Assert.IsTrue(codedInput.IsAtEnd); Assert.IsTrue(codedInput.IsAtEnd);
...@@ -145,7 +145,7 @@ namespace Google.Protobuf ...@@ -145,7 +145,7 @@ namespace Google.Protobuf
public void TestCalculateSizeWithTag() public void TestCalculateSizeWithTag()
{ {
var stream = new MemoryStream(); var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream); var codedOutput = new CodedOutputStream(stream);
codec.WriteTagAndValue(codedOutput, sampleValue); codec.WriteTagAndValue(codedOutput, sampleValue);
codedOutput.Flush(); codedOutput.Flush();
Assert.AreEqual(stream.Position, codec.CalculateSizeWithTag(sampleValue)); Assert.AreEqual(stream.Position, codec.CalculateSizeWithTag(sampleValue));
...@@ -155,7 +155,7 @@ namespace Google.Protobuf ...@@ -155,7 +155,7 @@ namespace Google.Protobuf
{ {
// WriteTagAndValue ignores default values // WriteTagAndValue ignores default values
var stream = new MemoryStream(); var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream); var codedOutput = new CodedOutputStream(stream);
codec.WriteTagAndValue(codedOutput, codec.DefaultValue); codec.WriteTagAndValue(codedOutput, codec.DefaultValue);
codedOutput.Flush(); codedOutput.Flush();
Assert.AreEqual(0, stream.Position); Assert.AreEqual(0, stream.Position);
...@@ -168,13 +168,13 @@ namespace Google.Protobuf ...@@ -168,13 +168,13 @@ namespace Google.Protobuf
// The plain ValueWriter/ValueReader delegates don't. // The plain ValueWriter/ValueReader delegates don't.
if (codec.DefaultValue != null) // This part isn't appropriate for message types. 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); codec.ValueWriter(codedOutput, codec.DefaultValue);
codedOutput.Flush(); codedOutput.Flush();
Assert.AreNotEqual(0, stream.Position); Assert.AreNotEqual(0, stream.Position);
Assert.AreEqual(stream.Position, codec.ValueSizeCalculator(codec.DefaultValue)); Assert.AreEqual(stream.Position, codec.ValueSizeCalculator(codec.DefaultValue));
stream.Position = 0; stream.Position = 0;
var codedInput = CodedInputStream.CreateInstance(stream); var codedInput = new CodedInputStream(stream);
Assert.AreEqual(codec.DefaultValue, codec.ValueReader(codedInput)); Assert.AreEqual(codec.DefaultValue, codec.ValueReader(codedInput));
} }
} }
......
...@@ -253,7 +253,7 @@ namespace Google.Protobuf ...@@ -253,7 +253,7 @@ namespace Google.Protobuf
{ {
// Hand-craft the stream to contain a single entry with just a value. // Hand-craft the stream to contain a single entry with just a value.
var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream); var output = new CodedOutputStream(memoryStream);
output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited); output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited);
var nestedMessage = new ForeignMessage { C = 20 }; var nestedMessage = new ForeignMessage { C = 20 };
// Size of the entry (tag, size written by WriteMessage, data written by WriteMessage) // Size of the entry (tag, size written by WriteMessage, data written by WriteMessage)
...@@ -271,7 +271,7 @@ namespace Google.Protobuf ...@@ -271,7 +271,7 @@ namespace Google.Protobuf
{ {
// Hand-craft the stream to contain a single entry with three fields // Hand-craft the stream to contain a single entry with three fields
var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream); var output = new CodedOutputStream(memoryStream);
output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
...@@ -298,7 +298,7 @@ namespace Google.Protobuf ...@@ -298,7 +298,7 @@ namespace Google.Protobuf
public void MapFieldOrderIsIrrelevant() public void MapFieldOrderIsIrrelevant()
{ {
var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream); var output = new CodedOutputStream(memoryStream);
output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited);
...@@ -322,7 +322,7 @@ namespace Google.Protobuf ...@@ -322,7 +322,7 @@ namespace Google.Protobuf
public void MapNonContiguousEntries() public void MapNonContiguousEntries()
{ {
var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream); var output = new CodedOutputStream(memoryStream);
// Message structure: // Message structure:
// Entry for MapInt32Int32 // Entry for MapInt32Int32
...@@ -373,7 +373,7 @@ namespace Google.Protobuf ...@@ -373,7 +373,7 @@ namespace Google.Protobuf
public void DuplicateKeys_LastEntryWins() public void DuplicateKeys_LastEntryWins()
{ {
var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
var output = CodedOutputStream.CreateInstance(memoryStream); var output = new CodedOutputStream(memoryStream);
var key = 10; var key = 10;
var value1 = 20; var value1 = 20;
......
...@@ -50,7 +50,7 @@ namespace Google.Protobuf ...@@ -50,7 +50,7 @@ namespace Google.Protobuf
Assert.AreEqual(58, msg.CalculateSize()); Assert.AreEqual(58, msg.CalculateSize());
byte[] bytes = new byte[58]; byte[] bytes = new byte[58];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes); CodedOutputStream output = new CodedOutputStream(bytes);
msg.WriteTo(output); msg.WriteTo(output);
Assert.AreEqual(0, output.SpaceLeft); Assert.AreEqual(0, output.SpaceLeft);
......
...@@ -309,7 +309,7 @@ namespace Google.Protobuf.WellKnownTypes ...@@ -309,7 +309,7 @@ namespace Google.Protobuf.WellKnownTypes
var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint); var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint);
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
{ {
var coded = CodedOutputStream.CreateInstance(stream); var coded = new CodedOutputStream(stream);
coded.WriteTag(wrapperTag); coded.WriteTag(wrapperTag);
coded.WriteLength(2); // valueTag + a value 0, each one byte coded.WriteLength(2); // valueTag + a value 0, each one byte
coded.WriteTag(valueTag); coded.WriteTag(valueTag);
......
...@@ -203,7 +203,7 @@ namespace Google.Protobuf ...@@ -203,7 +203,7 @@ namespace Google.Protobuf
public CodedInputStream CreateCodedInput() public CodedInputStream CreateCodedInput()
{ {
// We trust CodedInputStream not to reveal the provided byte array or modify it // 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) public static bool operator ==(ByteString lhs, ByteString rhs)
......
...@@ -93,43 +93,19 @@ namespace Google.Protobuf ...@@ -93,43 +93,19 @@ namespace Google.Protobuf
private int sizeLimit = DefaultSizeLimit; private int sizeLimit = DefaultSizeLimit;
#region Construction #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> /// <summary>
/// Creates a new CodedInputStream reading data from the given /// Creates a new CodedInputStream reading data from the given
/// byte array. /// byte array.
/// </summary> /// </summary>
public static CodedInputStream CreateInstance(byte[] buf) public CodedInputStream(byte[] buf) : this(buf, 0, buf.Length)
{ {
return new CodedInputStream(buf, 0, buf.Length);
} }
/// <summary> /// <summary>
/// Creates a new CodedInputStream that reads from the given /// Creates a new CodedInputStream that reads from the given
/// byte array slice. /// byte array slice.
/// </summary> /// </summary>
public static CodedInputStream CreateInstance(byte[] buf, int offset, int length) public CodedInputStream(byte[] buffer, int offset, int length)
{
return new CodedInputStream(buf, offset, length);
}
private CodedInputStream(byte[] buffer, int offset, int length)
{ {
this.buffer = buffer; this.buffer = buffer;
this.bufferPos = offset; this.bufferPos = offset;
...@@ -137,14 +113,21 @@ namespace Google.Protobuf ...@@ -137,14 +113,21 @@ namespace Google.Protobuf
this.input = null; 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.buffer = new byte[BufferSize];
this.bufferSize = 0; this.bufferSize = 0;
this.input = input; 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.buffer = buffer;
this.bufferSize = 0; this.bufferSize = 0;
......
...@@ -65,7 +65,20 @@ namespace Google.Protobuf ...@@ -65,7 +65,20 @@ namespace Google.Protobuf
private readonly Stream output; private readonly Stream output;
#region Construction #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) private CodedOutputStream(byte[] buffer, int offset, int length)
{ {
this.output = null; this.output = null;
...@@ -85,40 +98,17 @@ namespace Google.Protobuf ...@@ -85,40 +98,17 @@ namespace Google.Protobuf
/// <summary> /// <summary>
/// Creates a new CodedOutputStream which write to the given stream. /// Creates a new CodedOutputStream which write to the given stream.
/// </summary> /// </summary>
public static CodedOutputStream CreateInstance(Stream output) public CodedOutputStream(Stream output) : this(output, DefaultBufferSize)
{ {
return CreateInstance(output, DefaultBufferSize);
} }
/// <summary> /// <summary>
/// Creates a new CodedOutputStream which write to the given stream and uses /// Creates a new CodedOutputStream which write to the given stream and uses
/// the specified buffer size. /// the specified buffer size.
/// </summary> /// </summary>
public static CodedOutputStream CreateInstance(Stream output, int bufferSize) public CodedOutputStream(Stream output, int bufferSize) : this(output, new byte[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 #endregion
/// <summary> /// <summary>
......
...@@ -43,7 +43,7 @@ namespace Google.Protobuf ...@@ -43,7 +43,7 @@ namespace Google.Protobuf
{ {
Preconditions.CheckNotNull(message, "message"); Preconditions.CheckNotNull(message, "message");
Preconditions.CheckNotNull(data, "data"); Preconditions.CheckNotNull(data, "data");
CodedInputStream input = CodedInputStream.CreateInstance(data); CodedInputStream input = new CodedInputStream(data);
message.MergeFrom(input); message.MergeFrom(input);
input.CheckLastTagWas(0); input.CheckLastTagWas(0);
} }
...@@ -61,7 +61,7 @@ namespace Google.Protobuf ...@@ -61,7 +61,7 @@ namespace Google.Protobuf
{ {
Preconditions.CheckNotNull(message, "message"); Preconditions.CheckNotNull(message, "message");
Preconditions.CheckNotNull(input, "input"); Preconditions.CheckNotNull(input, "input");
CodedInputStream codedInput = CodedInputStream.CreateInstance(input); CodedInputStream codedInput = new CodedInputStream(input);
message.MergeFrom(codedInput); message.MergeFrom(codedInput);
codedInput.CheckLastTagWas(0); codedInput.CheckLastTagWas(0);
} }
...@@ -79,7 +79,7 @@ namespace Google.Protobuf ...@@ -79,7 +79,7 @@ namespace Google.Protobuf
{ {
Preconditions.CheckNotNull(message, "message"); Preconditions.CheckNotNull(message, "message");
byte[] result = new byte[message.CalculateSize()]; byte[] result = new byte[message.CalculateSize()];
CodedOutputStream output = CodedOutputStream.CreateInstance(result); CodedOutputStream output = new CodedOutputStream(result);
message.WriteTo(output); message.WriteTo(output);
output.CheckNoSpaceLeft(); output.CheckNoSpaceLeft();
return result; return result;
...@@ -89,7 +89,7 @@ namespace Google.Protobuf ...@@ -89,7 +89,7 @@ namespace Google.Protobuf
{ {
Preconditions.CheckNotNull(message, "message"); Preconditions.CheckNotNull(message, "message");
Preconditions.CheckNotNull(output, "output"); Preconditions.CheckNotNull(output, "output");
CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); CodedOutputStream codedOutput = new CodedOutputStream(output);
message.WriteTo(codedOutput); message.WriteTo(codedOutput);
codedOutput.Flush(); codedOutput.Flush();
} }
...@@ -98,7 +98,7 @@ namespace Google.Protobuf ...@@ -98,7 +98,7 @@ namespace Google.Protobuf
{ {
Preconditions.CheckNotNull(message, "message"); Preconditions.CheckNotNull(message, "message");
Preconditions.CheckNotNull(output, "output"); Preconditions.CheckNotNull(output, "output");
CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); CodedOutputStream codedOutput = new CodedOutputStream(output);
codedOutput.WriteRawVarint32((uint)message.CalculateSize()); codedOutput.WriteRawVarint32((uint)message.CalculateSize());
message.WriteTo(codedOutput); message.WriteTo(codedOutput);
codedOutput.Flush(); codedOutput.Flush();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment