abujehad139 / google-api-dotnet-client

Automatically exported from code.google.com/p/google-api-dotnet-client
Apache License 2.0
0 stars 0 forks source link

Create a binary stream reader (similar to StreamReader) #313

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
That stream reader should support read and peak (similar to 
http://msdn.microsoft.com/en-us/library/system.io.streamreader.peek.aspx).

It will be useful in resumable upload where we read bytes into a buffer and 
then want to figure out if is end of stream, so we read an additional cached 
byte

BEFORE:
int len = 0;
// read bytes form the stream to lastMediaRequest byte array
while (true)
{
 len = stream.Read(lastMediaRequest, lastMediaLength(int)Math.Min(BufferSize, ChunkSize - lastMediaLength));
 lastMediaLength += len;
 if (len == 0) break;
}

// check if there is still data to read from stream, and cache the first byte 
in catchedByte
if (0 == stream.Read(cachedByte, 0, 1))
...

AFTER:
int bytesRead;
bool eos = false;
using (var reader = new BinaryStreamReader(stream, ChunkSize))
{
 bytesRead = reader.Read();
 eos = reader.Peek() == -1;
}

Original issue reported on code.google.com by pele...@google.com on 17 Apr 2013 at 3:18