using System; using System.Diagnostics; namespace TwlBackupBlock { /// /// バイト単位でアクセスする機能だけを持ったデータ本体です。 /// public class Body : AbstractBody { private byte[] data; /// /// 指定したデータ本体をもとに、Bodyクラスの新しいインスタンスを初期化します。 /// /// public Body(byte[] data) { if (data == null) { throw new ArgumentNullException("data"); } this.data = (byte[])data.Clone(); } public override byte this[int index] { get { if (index < 0 || index >= data.Length) { throw new ArgumentOutOfRangeException("index"); } return data[index]; } set { if (index < 0 || index >= data.Length) { throw new ArgumentOutOfRangeException("index"); } data[index] = value; } } public override int Length { get { Debug.Assert(data != null); return data.Length; } } public override byte[] GetBytes() { Debug.Assert(data != null); return (byte[])data.Clone(); } public override void SetBytes(byte[] bytes) { Debug.Assert(data != null); if (bytes == null) { throw new ArgumentNullException("bytes"); } if (bytes.Length != data.Length) { string message = string.Format("bytes.Length != {0} (bytes.Length:{1})", data.Length, bytes.Length); throw new ArgumentException("bytes", message); } bytes.CopyTo(data, 0); } public override object Clone() { return new Body(GetBytes()); } } }