using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IO
{
    public class EndianStream
    {
        // Private stream handles
        Endianness endian;
        MemoryStream ms;
        FileStream fs;
        EndianReader br, mbr;
        EndianWriter bw, mbw;

        // Accessors
        public MemoryStream Stream { get { return ms; } }
        public EndianReader StreamReader { get { return mbr; } }
        public EndianWriter StreamWriter { get { return mbw; } }
        public EndianReader Reader { get { return br; } }
        public EndianWriter Writer { get { return bw; } }

        public EndianStream(Endianness Endian, string FileName, FileMode Mode, FileAccess Access, FileShare Share)
        {
            // Initialize flushable stream
            ms = new MemoryStream();
            mbr = new EndianReader(Endian, ms);
            mbw = new EndianWriter(Endian, ms);

            // Initialize filestream
            fs = new FileStream(FileName, Mode, Access, Share);
            br = new EndianReader(Endian, fs);
            bw = new EndianWriter(Endian, fs);

            // Save fields
            this.endian = Endian;
        }

        public void Flush()
        {
            // Flush stream
            bw.Write(ms.ToArray());

            // Initialize flushable stream
            ms = new MemoryStream();
            mbr = new EndianReader(endian, ms);
            mbw = new EndianWriter(endian, ms);
        }
    }
}
