using System;
using System.IO;
/// <summary>
/// 이진 파일 관리자
/// </summary>
public class BinaryFileManager : IDisposable
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Public
#region Field
/// <summary>
/// 파일 스트림
/// </summary>
private FileStream fileStream;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 파일 스트림 - FileStream
/// <summary>
/// 파일 스트림
/// </summary>
public FileStream FileStream
{
get
{
return this.fileStream;
}
}
#endregion
#region 파일 길이 - FileLength
/// <summary>
/// 파일 길이
/// </summary>
public long FileLength
{
get
{
if(this.fileStream != null)
{
return this.fileStream.Length;
}
else
{
return 0L;
}
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - BinaryFileManager(fileStream)
/// <summary>
/// 생성자
/// </summary>
/// <param name="fileStream">파일 스트림</param>
public BinaryFileManager(FileStream fileStream)
{
this.fileStream = fileStream;
}
#endregion
#region 생성자 - BinaryFileManager(filePath, fileMode, fileAccess)
/// <summary>
/// 생성자
/// </summary>
/// <param name="filePath">파일 경로</param>
/// <param name="fileMode">파일 모드</param>
/// <param name="fileAccess">파일 액세스</param>
public BinaryFileManager(string filePath, FileMode fileMode, FileAccess fileAccess)
{
this.fileStream = new FileStream(filePath, fileMode, fileAccess);
}
#endregion
#region 생성자 - BinaryFileManager(filePath)
/// <summary>
/// 생성자
/// </summary>
/// <param name="filePath">파일 경로</param>
public BinaryFileManager(string filePath) : this(filePath, FileMode.Open, FileAccess.Read)
{
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 생성자 - BinaryFileManager()
/// <summary>
/// 생성자
/// </summary>
private BinaryFileManager()
{
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Destructor
#region 소멸자 - ~BinaryFileManager()
/// <summary>
/// 소멸자
/// </summary>
~BinaryFileManager()
{
Close();
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
#region 파일 읽기 - ReadFile(filePath)
/// <summary>
/// 파일 읽기
/// </summary>
/// <param name="filePath">파일 경로</param>
/// <returns>읽기 바이트 배열</returns>
public static byte[] ReadFile(string filePath)
{
FileStream fileStream = null;
byte[] bufferByteArray = null;
try
{
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
int fileLength = (int)fileStream.Length;
bufferByteArray = new byte[fileLength];
int countRead;
int totalCountRead = 0;
while((countRead = fileStream.Read(bufferByteArray, totalCountRead, fileLength - totalCountRead)) > 0)
{
totalCountRead += countRead;
}
}
finally
{
if(fileStream != null)
{
fileStream.Close();
fileStream.Dispose();
fileStream = null;
}
}
return bufferByteArray;
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Public
#region 쓰기 - Write(seekOrigin, startPosition, sourceByteArray, sourceByteArrayOffset, writeLength)
/// <summary>
/// 쓰기
/// </summary>
/// <param name="seekOrigin">탐색 기준</param>
/// <param name="startPosition">시작 위치</param>
/// <param name="sourceByteArray">소스 바이트 배열</param>
/// <param name="sourceByteArrayOffset">소스 바이트 배열 오프셋</param>
/// <param name="writeLength">쓰기 길이</param>
public void Write(SeekOrigin seekOrigin, long startPosition, byte[] sourceByteArray, int sourceByteArrayOffset, int writeLength)
{
this.fileStream.Seek(startPosition, seekOrigin);
this.fileStream.Write(sourceByteArray, sourceByteArrayOffset, writeLength);
}
#endregion
#region 쓰기 - Write(seekOrigin, startPosition, sourceByteArray)
/// <summary>
/// 쓰기
/// </summary>
/// <param name="seekOrigin">탐색 기준</param>
/// <param name="startPosition">시작 위치</param>
/// <param name="sourceByteArray">소스 바이트 배열</param>
public void Write(SeekOrigin seekOrigin, long startPosition, byte[] sourceByteArray)
{
Write(seekOrigin, startPosition, sourceByteArray, 0, sourceByteArray.Length);
}
#endregion
#region 쓰기 - Write(startPosition, sourceByteArray, sourceByteArrayOffset, writeLength)
/// <summary>
/// 쓰기
/// </summary>
/// <param name="lStartPosition">시작 위치</param>
/// <param name="pSourceByteArray">소스 바이트 배열<param>
/// <param name="nBufferOffset">소스 바이트 배열 오프셋</param>
/// <param name="nWriteLength">쓰기 길이</param>
public void Write(long startPosition, byte[] sourceByteArray, int sourceByteArrayOffset, int writeLength)
{
Write(SeekOrigin.Begin, startPosition, sourceByteArray, sourceByteArrayOffset, writeLength);
}
#endregion
#region 쓰기 - Write(startPosition, sourceByteArray)
/// <summary>
/// 쓰기
/// </summary>
/// <param name="startPosition">시작 위치</param>
/// <param name="sourceByteArray">소스 바이트 배열</param>
public void Write(long startPosition, byte[] sourceByteArray)
{
Write(SeekOrigin.Begin, startPosition, sourceByteArray, 0, sourceByteArray.Length);
}
#endregion
#region 쓰기 - Write(sourceByteArray, sourceByteArrayOffset, writeLength)
/// <summary>
/// 쓰기
/// </summary>
/// <param name="sourceByteArray">소스 바이트 배열</param>
/// <param name="sourceByteArrayOffset">소스 바이트 배열 오프셋</param>
/// <param name="writeLength">쓰기 길이</param>
public void Write(byte[] sourceByteArray, int sourceByteArrayOffset, int writeLength)
{
Write(SeekOrigin.Begin, 0L, sourceByteArray, sourceByteArrayOffset, writeLength);
}
#endregion
#region 쓰기 - Write(sourceByteArray)
/// <summary>
/// 쓰기
/// </summary>
/// <param name="sourceByteArray">소스 바이트 배열</param>
public void Write(byte[] sourceByteArray)
{
Write(SeekOrigin.Begin, 0L, sourceByteArray, 0, sourceByteArray.Length);
}
#endregion
#region 읽기 - Read(seekOrigin, startPosition, targetByteArray, targetByteArrayOffset, readLength)
/// <summary>
/// 읽기
/// </summary>
/// <param name="seekOrigin">탐색 기준</param>
/// <param name="startPosition">시작 위치</param>
/// <param name="targetByteArray">타겟 바이트 배열</param>
/// <param name="targetByteArrayOffset">타겟 바이트 배열 오프셋</param>
/// <param name="readLength">읽기 길이</param>
/// <returns>실제 읽은 길이</returns>
public int Read(SeekOrigin seekOrigin, long startPosition, byte[] targetByteArray, int targetByteArrayOffset, int readLength)
{
this.fileStream.Seek(startPosition, seekOrigin);
return this.fileStream.Read(targetByteArray, targetByteArrayOffset, readLength);
}
#endregion
#region 읽기 - Read(seekOrigin, startPosition, targetByteArray)
/// <summary>
/// 읽기
/// </summary>
/// <param name="seekOrigin">탐색 기준</param>
/// <param name="startPosition">시작 위치</param>
/// <param name="targetByteArray">타겟 바이트 배열</param>
/// <returns>실제 읽은 길이</returns>
public int Read(SeekOrigin seekOrigin, long startPosition, byte[] targetByteArray)
{
return Read(seekOrigin, startPosition, targetByteArray, 0, targetByteArray.Length);
}
#endregion
#region 읽기 - Read(startPosition, targetByteArray, targetByteArrayOffset, readLength)
/// <summary>
/// 읽기
/// </summary>
/// <param name="startPosition">시작 위치</param>
/// <param name="targetByteArray">타겟 바이트 배열</param>
/// <param name="targetByteArrayOffset">타겟 바이트 배열 오프셋</param>
/// <param name="readLength">읽기 길이</param>
/// <returns>실제 읽은 길이</returns>
public int Read(long startPosition, byte[] targetByteArray, int targetByteArrayOffset, int readLength)
{
return Read(SeekOrigin.Begin, startPosition, targetByteArray, targetByteArrayOffset, readLength);
}
#endregion
#region 읽기 - Read(startPosition, targetByteArray)
/// <summary>
/// 읽기
/// </summary>
/// <param name="startPosition">시작 위치</param>
/// <param name="targetByteArray">타겟 바이트 배열</param>
/// <returns>실제 읽은 길이</returns>
public int Read(long startPosition, byte[] targetByteArray)
{
return Read(SeekOrigin.Begin, startPosition, targetByteArray, 0, targetByteArray.Length);
}
#endregion
#region 읽기 - Read(targetByteArray, targetByteArrayOffset, readLength)
/// <summary>
/// 읽기
/// </summary>
/// <param name="targetByteArray">타겟 바이트 배열</param>
/// <param name="targetByteArrayOffset">타겟 바이트 배열 오프셋</param>
/// <param name="readLength">읽기 길이</param>
/// <returns>실제 읽은 길이</returns>
public int Read(byte[] targetByteArray, int targetByteArrayOffset, int readLength)
{
return Read(SeekOrigin.Begin, 0L, targetByteArray, targetByteArrayOffset, readLength);
}
#endregion
#region 읽기 - Read(targetByteArray)
/// <summary>
/// 읽기
/// </summary>
/// <param name="targetByteArray">타겟 바이트 배열</param>
/// <returns>실제 읽은 길이</returns>
public int Read(byte[] targetByteArray)
{
return Read(SeekOrigin.Begin, 0L, targetByteArray, 0, targetByteArray.Length);
}
#endregion
#region 닫기 - Close()
/// <summary>
/// 닫기
/// </summary>
public void Close()
{
if(this.fileStream != null)
{
this.fileStream.Close();
this.fileStream.Dispose();
this.fileStream = null;
}
}
#endregion
#region 해제하기 - Dispose()
/// <summary>
/// 해제하기
/// </summary>
public void Dispose()
{
Close();
}
#endregion
}