using System; using System.IO; class FileReader { public FileStream m_File; bool m_Open; public FileReader(string Filename) { m_File = File.OpenRead(Filename); m_Open = true; } ~FileReader() { if (m_Open) m_File.Close(); } public byte[] Read() { return Read((int)m_File.Length); } public byte[] Read(int Length) { if (Length < 0) throw new Exception("Invalid length " + Length); byte[] tmp_Buffer, Buffer = new byte[Length]; int Index = 0; int ByteCount, BytesRemaining = Length; do { tmp_Buffer = new byte[BytesRemaining]; ByteCount = m_File.Read(tmp_Buffer, 0, BytesRemaining); for (int i = 0; i < ByteCount; i++) Buffer[Index + i] = tmp_Buffer[i]; Index += ByteCount; BytesRemaining -= ByteCount; } while (ByteCount > 0 && BytesRemaining > 0); return Buffer; } public void Close() { if (!m_Open) throw new Exception("Trying to close an unopened file"); m_File.Close(); m_Open = false; } } class FileWriter { public FileStream m_File; bool m_Open; public FileWriter(string Filename) { m_File = File.OpenWrite(Filename); m_Open = true; } ~FileWriter() { if (m_Open) m_File.Close(); } public void Truncate() { if (!m_Open) throw new Exception("Trying to truncate an unopened file"); m_File.SetLength(0); } public void Write(byte[] Buffer) { Write(Buffer, Buffer.Length); } public void Write(byte[] Buffer, int Length) { if (Length < 0) throw new Exception("Invalid length " + Length); m_File.Write(Buffer, 0, Length); } public void Close() { if (!m_Open) throw new Exception("Trying to close an unopened file"); m_File.Close(); m_Open = false; } }