// // BEncodedList.cs // // Authors: // Alan McGovern alan.mcgovern@gmail.com // // Copyright (C) 2006 Alan McGovern // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Tancoder.Torrent.BEncoding { /// /// Class representing a BEncoded list /// public class BEncodedList : BEncodedValue, IList { #region Member Variables private List list; #endregion #region Constructors /// /// Create a new BEncoded List with default capacity /// public BEncodedList() : this(new List()) { } /// /// Create a new BEncoded List with the supplied capacity /// /// The initial capacity public BEncodedList(int capacity) : this(new List(capacity)) { } public BEncodedList(IEnumerable list) { if (list == null) throw new ArgumentNullException("list"); this.list = new List(list); } private BEncodedList(List value) { this.list = value; } #endregion #region Encode/Decode Methods /// /// Encodes the list to a byte[] /// /// The buffer to encode the list to /// The offset to start writing the data at /// public override int Encode(byte[] buffer, int offset) { int written = 0; buffer[offset] = (byte)'l'; written++; for (int i = 0; i < this.list.Count; i++) written += this.list[i].Encode(buffer, offset + written); buffer[offset + written] = (byte)'e'; written++; return written; } /// /// Decodes a BEncodedList from the given StreamReader /// /// internal override void DecodeInternal(RawReader reader) { if (reader.ReadByte() != 'l') // Remove the leading 'l' throw new BEncodingException("Invalid data found. Aborting"); while ((reader.PeekByte() != -1) && (reader.PeekByte() != 'e')) list.Add(BEncodedValue.Decode(reader)); if (reader.ReadByte() != 'e') // Remove the trailing 'e' throw new BEncodingException("Invalid data found. Aborting"); } #endregion #region Helper Methods /// /// Returns the size of the list in bytes /// /// public override int LengthInBytes() { int length = 0; length += 1; // Lists start with 'l' for (int i=0; i < this.list.Count; i++) length += this.list[i].LengthInBytes(); length += 1; // Lists end with 'e' return length; } #endregion #region Overridden Methods public override bool Equals(object obj) { BEncodedList other = obj as BEncodedList; if (other == null) return false; for (int i = 0; i < this.list.Count; i++) if (!this.list[i].Equals(other.list[i])) return false; return true; } public override int GetHashCode() { int result = 0; for (int i = 0; i < list.Count; i++) result ^= list[i].GetHashCode(); return result; } public override string ToString() { return System.Text.Encoding.UTF8.GetString(Encode()); } #endregion #region IList methods public void Add(BEncodedValue item) { this.list.Add(item); } public void AddRange (IEnumerable collection) { list.AddRange (collection); } public void Clear() { this.list.Clear(); } public bool Contains(BEncodedValue item) { return this.list.Contains(item); } public void CopyTo(BEncodedValue[] array, int arrayIndex) { this.list.CopyTo(array, arrayIndex); } public int Count { get { return this.list.Count; } } public int IndexOf(BEncodedValue item) { return this.list.IndexOf(item); } public void Insert(int index, BEncodedValue item) { this.list.Insert(index, item); } public bool IsReadOnly { get { return false; } } public bool Remove(BEncodedValue item) { return this.list.Remove(item); } public void RemoveAt(int index) { this.list.RemoveAt(index); } public BEncodedValue this[int index] { get { return this.list[index]; } set { this.list[index] = value; } } public IEnumerator GetEnumerator() { return this.list.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } #endregion } }