Gokul's Blog

String Stream for .NET

Leave a comment

A good article for using a stream of string.

Article Link:http://www.canerten.com/string-stream-for-net/

public class StringStream : Stream
{
        private StringBuilder strBuilder;
        public StringStream()
        {            strBuilder = new StringBuilder();        }
 
        public StringStream(string str)
        {            strBuilder = new StringBuilder(str);        }
 
        public override int Read(byte[] buffer, int offset, int count)
        {
            int howMuchRead = 0;
            for (int i = offset; i < count; i++)
            {
                var actualIndex = i * 2;               
                howMuchRead = i+1;
                if (actualIndex >= strBuilder.Length)
                {
                    howMuchRead = count;
                    break;
                }                                  
                string s = strBuilder[actualIndex].ToString() + strBuilder[actualIndex + 1].ToString();
                buffer[i] = Convert.ToByte(s,16);
            }
            return howMuchRead;
        }
      public override void Write(byte[] buffer, int offset, int count)
        {           
            for (int i = offset; i < count; i++)
            {
                strBuilder.Append(buffer[i].ToString(“x2”));
            }
        }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s