//  ==========================================================
//  The widening stream class acts as an output stream layer
//  that expands each line feed character to a carriage return
//  line feed combination. It is ment to be used on Windows.
//  It simplifies and standardizes text output for an upper
//  stream layer because line endings can be represented as a
//  bare line feed character on all platforms supported by the
//  Cminor.
//  ==========================================================

using System;

namespace System.IO
{
    public class WideningStream : Stream, Closable
    {
        public WideningStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("provided stream is null");
            }
            this.baseStream = stream;
        }
        public override void WriteByte(byte value)
        {
            if (value == cast<byte>('\n'))
            {
                baseStream.WriteByte(cast<byte>('\r'));
            }
            baseStream.WriteByte(value);
        }
        public override void Write(byte[] buffer, int count)
        {
            for (int i = 0; i < count; ++i)
            {
                WriteByte(buffer[i]);
            }
        }
        public override int ReadByte()
        {
            return baseStream.ReadByte();
        }
        public override int Read(byte[] buffer)
        {
            return baseStream.Read(buffer);
        }
        public override void Flush()
        {
            baseStream.Flush();
        }
        public override void Close()
        {
            baseStream.Close();
        }
        private Stream baseStream;
    }
}