aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2013-05-07 15:56:50 -0500
committerrogerk <devnull@localhost>2013-05-07 15:56:50 -0500
commit92fcf3537f42475148feaf9a85f800603811ede2 (patch)
treeac6fc36ebabd270bb8d2954b5fec1b0e3be716a7
parent71e22f8b7f08116d7ed021b59fe1f154110a2a6a (diff)
downloadprotobuf-92fcf3537f42475148feaf9a85f800603811ede2.tar.gz
protobuf-92fcf3537f42475148feaf9a85f800603811ede2.tar.bz2
protobuf-92fcf3537f42475148feaf9a85f800603811ede2.zip
Added the ability to instantiate a coded input with a pre-existing buffer
Added CodedInputStream.Position to return the current input stream position
-rw-r--r--src/ProtocolBuffers/CodedInputStream.cs32
-rw-r--r--src/ProtocolBuffersLite.Test/AbstractBuilderLiteTest.cs4
2 files changed, 34 insertions, 2 deletions
diff --git a/src/ProtocolBuffers/CodedInputStream.cs b/src/ProtocolBuffers/CodedInputStream.cs
index 3ee7d55f..bc443839 100644
--- a/src/ProtocolBuffers/CodedInputStream.cs
+++ b/src/ProtocolBuffers/CodedInputStream.cs
@@ -108,6 +108,14 @@ namespace Google.ProtocolBuffers
{
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
@@ -142,8 +150,28 @@ namespace Google.ProtocolBuffers
this.input = input;
}
+ private CodedInputStream(Stream input, byte[] buffer)
+ {
+ this.buffer = buffer;
+ this.bufferSize = 0;
+ this.input = input;
+ }
#endregion
+ /// <summary>
+ /// Returns the current position in the input stream, or the position in the input buffer
+ /// </summary>
+ public long Position
+ {
+ get
+ {
+ if (input != null)
+ return input.Position - (bufferSize - bufferPos);
+ return bufferPos;
+ }
+ }
+
+
void ICodedInputStream.ReadMessageStart() { }
void ICodedInputStream.ReadMessageEnd() { }
@@ -1608,7 +1636,7 @@ namespace Google.ProtocolBuffers
bufferPos += size;
return bytes;
}
- else if (size < BufferSize)
+ else if (size < buffer.Length)
{
// Reading more bytes than are in the buffer, but not an excessive number
// of bytes. We can safely allocate the resulting array ahead of time.
@@ -1663,7 +1691,7 @@ namespace Google.ProtocolBuffers
while (sizeLeft > 0)
{
- byte[] chunk = new byte[Math.Min(sizeLeft, BufferSize)];
+ byte[] chunk = new byte[Math.Min(sizeLeft, buffer.Length)];
int pos = 0;
while (pos < chunk.Length)
{
diff --git a/src/ProtocolBuffersLite.Test/AbstractBuilderLiteTest.cs b/src/ProtocolBuffersLite.Test/AbstractBuilderLiteTest.cs
index 06fe6e49..66791afb 100644
--- a/src/ProtocolBuffersLite.Test/AbstractBuilderLiteTest.cs
+++ b/src/ProtocolBuffersLite.Test/AbstractBuilderLiteTest.cs
@@ -326,10 +326,14 @@ namespace Google.ProtocolBuffers
var input = CodedInputStream.CreateInstance(ms);
var builder = BucketOfBytes.CreateBuilder();
input.ReadMessage(builder, ExtensionRegistry.Empty);
+ Assert.AreEqual(3005L, input.Position);
Assert.AreEqual(3000, builder.Value.Length);
input.ReadMessage(builder, ExtensionRegistry.Empty);
+ Assert.AreEqual(5114, input.Position);
Assert.AreEqual(1000, builder.Value.Length);
input.ReadMessage(builder, ExtensionRegistry.Empty);
+ Assert.AreEqual(5217L, input.Position);
+ Assert.AreEqual(input.Position, ms.Length);
Assert.AreEqual(100, builder.Value.Length);
}
}