using System; using System.Collections; namespace Be.Windows.Forms { /// /// Represents a collection of bytes. /// public class ByteCollection : CollectionBase { /// /// Initializes a new instance of ByteCollection class. /// public ByteCollection() { } /// /// Initializes a new instance of ByteCollection class. /// /// an array of bytes to add to collection public ByteCollection(byte[] bs) { AddRange(bs); } /// /// Gets or sets the value of a byte /// public byte this[int index] { get { return (byte)List[index]; } set { List[index] = value; } } /// /// Adds a byte into the collection. /// /// the byte to add public void Add(byte b) { List.Add(b); } /// /// Adds a range of bytes to the collection. /// /// the bytes to add public void AddRange(byte[] bs) { InnerList.AddRange(bs); } /// /// Removes a byte from the collection. /// /// the byte to remove public void Remove(byte b) { List.Remove(b); } /// /// Removes a range of bytes from the collection. /// /// the index of the start byte /// the count of the bytes to remove public void RemoveRange(int index, int count) { InnerList.RemoveRange(index, count); } /// /// Inserts a range of bytes to the collection. /// /// the index of start byte /// an array of bytes to insert public void InsertRange(int index, byte[] bs) { InnerList.InsertRange(index, bs); } /// /// Gets all bytes in the array /// /// an array of bytes. public byte[] GetBytes() { byte[] bytes = new byte[Count]; InnerList.CopyTo(0, bytes, 0, bytes.Length); return bytes; } /// /// Inserts a byte to the collection. /// /// the index /// a byte to insert public void Insert(int index, byte b) { InnerList.Insert(index, b); } /// /// Returns the index of the given byte. /// public int IndexOf(byte b) { return InnerList.IndexOf(b); } /// /// Returns true, if the byte exists in the collection. /// public bool Contains(byte b) { return InnerList.Contains(b); } /// /// Copies the content of the collection into the given array. /// public void CopyTo(byte[] bs, int index) { InnerList.CopyTo(bs, index); } /// /// Copies the content of the collection into an array. /// /// the array containing all bytes. public byte[] ToArray() { byte[] data = new byte[this.Count]; this.CopyTo(data, 0); return data; } } }