diff options
author | Jon Skeet <skeet@pobox.com> | 2015-06-09 19:30:44 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2015-06-09 19:30:44 +0100 |
commit | e38294a62d7f37c0661273a9a26fda16d557423f (patch) | |
tree | 316989251907553408e7b32a12792f496333e075 /csharp/src/ProtocolBuffers/LimitedInputStream.cs | |
parent | f52426827e4d5e8da7d205af538799740b5199b9 (diff) | |
download | protobuf-e38294a62d7f37c0661273a9a26fda16d557423f.tar.gz protobuf-e38294a62d7f37c0661273a9a26fda16d557423f.tar.bz2 protobuf-e38294a62d7f37c0661273a9a26fda16d557423f.zip |
First pass at the mutable API. Quite a bit more to do - in particular, it's pretty slow right now.
Diffstat (limited to 'csharp/src/ProtocolBuffers/LimitedInputStream.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers/LimitedInputStream.cs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers/LimitedInputStream.cs b/csharp/src/ProtocolBuffers/LimitedInputStream.cs new file mode 100644 index 00000000..cfbf47de --- /dev/null +++ b/csharp/src/ProtocolBuffers/LimitedInputStream.cs @@ -0,0 +1,78 @@ +using System; +using System.IO; + +namespace Google.Protobuf +{ + /// <summary> + /// Stream implementation which proxies another stream, only allowing a certain amount + /// of data to be read. Note that this is only used to read delimited streams, so it + /// doesn't attempt to implement everything. + /// </summary> + internal sealed class LimitedInputStream : Stream + { + private readonly Stream proxied; + private int bytesLeft; + + internal LimitedInputStream(Stream proxied, int size) + { + this.proxied = proxied; + bytesLeft = size; + } + + public override bool CanRead + { + get { return true; } + } + + public override bool CanSeek + { + get { return false; } + } + + public override bool CanWrite + { + get { return false; } + } + + public override void Flush() + { + } + + public override long Length + { + get { throw new NotSupportedException(); } + } + + public override long Position + { + get { throw new NotSupportedException(); } + set { throw new NotSupportedException(); } + } + + public override int Read(byte[] buffer, int offset, int count) + { + if (bytesLeft > 0) + { + int bytesRead = proxied.Read(buffer, offset, Math.Min(bytesLeft, count)); + bytesLeft -= bytesRead; + return bytesRead; + } + return 0; + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotSupportedException(); + } + + public override void SetLength(long value) + { + throw new NotSupportedException(); + } + + public override void Write(byte[] buffer, int offset, int count) + { + throw new NotSupportedException(); + } + } +} |