This repository was archived by the owner on Jun 5, 2019. It is now read-only.
This repository was archived by the owner on Jun 5, 2019. It is now read-only.
StreamReader requires a Stream to implement Stream.Length on NETMF #231
Open
Description
http://netmf.codeplex.com/workitem/2239
The System.IO.StreamReader implementation on NETMF requires an underlying stream to implement the Length function in order for the ReadLine function to work.
This reduces the usefulness of the ReadLine function in network stream environments where the length of a stream is unknown.
The following code will work on .NET:
using System;
using System.IO;
public class Program
{
public static void Main()
{
using (var stream = new TestStream())
using (var reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadLine());
}
}
}
but not on NETMF:
using Microsoft.SPOT;
using System.IO;
public class Program
{
public static void Main()
{
using (var stream = new TestStream())
using (var reader = new StreamReader(stream))
{
Debug.Print(reader.ReadLine());
}
}
}
And will throw a System.NotImplemented exception in our Length function. This is due to StreamReader using StreamReader.Read(). Which in turn uses the Length function on the underlying Stream if there is data to be read.
Teststream class for reference:
using System;
using System.Text;
using System.IO;
public class TestStream : Stream
{
// Imitate a network stream by just allocating a constant buffer.
byte[] readBuffer = UTF8Encoding.UTF8.GetBytes("Hello World!\n");
int currentPos;
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()
{
throw new NotImplementedException();
}
public override long Length
{
get { throw new NotImplementedException(); }
}
public override long Position
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override int Read(byte[] buffer, int offset, int count)
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
if (count < 0 || offset < 0 || offset > buffer.Length || buffer.Length - offset > count)
{
throw new ArgumentOutOfRangeException();
}
if (count <= 0 || currentPos > readBuffer.Length)
{
return 0;
}
int toRead = readBuffer.Length - currentPos;
int len = count > toRead ? toRead : count;
Array.Copy(readBuffer, currentPos, buffer, offset, len);
currentPos += len;
return len;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
}