aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/Google.Protobuf
diff options
context:
space:
mode:
authorGravatar Jon Skeet <skeet@pobox.com>2015-08-03 14:02:44 +0100
committerGravatar Jon Skeet <skeet@pobox.com>2015-08-03 14:02:44 +0100
commitdb9f47a3ed78f55bb4eb8b8b8582a88eb64402d0 (patch)
tree00f2d3938d3efd08057024143a7afea36786a008 /csharp/src/Google.Protobuf
parentbf1cc9217a486cf68c2705d4f307fddb73291004 (diff)
parent0e0e0c97e7d5a70b3307c2fcea8793983b6e0680 (diff)
Merge pull request #678 from jskeet/stream-ctor
Expose Coded*Stream constructors directly.
Diffstat (limited to 'csharp/src/Google.Protobuf')
-rw-r--r--csharp/src/Google.Protobuf/ByteString.cs2
-rw-r--r--csharp/src/Google.Protobuf/CodedInputStream.cs39
-rw-r--r--csharp/src/Google.Protobuf/CodedOutputStream.cs42
-rw-r--r--csharp/src/Google.Protobuf/MessageExtensions.cs10
4 files changed, 33 insertions, 60 deletions
diff --git a/csharp/src/Google.Protobuf/ByteString.cs b/csharp/src/Google.Protobuf/ByteString.cs
index 329f47f6..cf5468d2 100644
--- a/csharp/src/Google.Protobuf/ByteString.cs
+++ b/csharp/src/Google.Protobuf/ByteString.cs
@@ -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)
diff --git a/csharp/src/Google.Protobuf/CodedInputStream.cs b/csharp/src/Google.Protobuf/CodedInputStream.cs
index ef7cf114..f2e1d661 100644
--- a/csharp/src/Google.Protobuf/CodedInputStream.cs
+++ b/csharp/src/Google.Protobuf/CodedInputStream.cs
@@ -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;
diff --git a/csharp/src/Google.Protobuf/CodedOutputStream.cs b/csharp/src/Google.Protobuf/CodedOutputStream.cs
index b91d6d70..b084c14f 100644
--- a/csharp/src/Google.Protobuf/CodedOutputStream.cs
+++ b/csharp/src/Google.Protobuf/CodedOutputStream.cs
@@ -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>
diff --git a/csharp/src/Google.Protobuf/MessageExtensions.cs b/csharp/src/Google.Protobuf/MessageExtensions.cs
index 7bd79930..6e7d47bc 100644
--- a/csharp/src/Google.Protobuf/MessageExtensions.cs
+++ b/csharp/src/Google.Protobuf/MessageExtensions.cs
@@ -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();