// SEQUENCE data type. // Copyright (C) 2008-2010 Malcolm Crowe, Lex Li, and other contributors. // // 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. /* * Created by SharpDevelop. * User: lextm * Date: 2008/4/30 * Time: 20:53 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace GSF.Net.Snmp { /// /// Array type. /// /// Represents SMIv1 SEQUENCE. public sealed class Sequence : ISnmpData { private readonly List _list = new List(); private readonly byte[] _length; private byte[] _buffer; /// /// Gets the enumerator. /// /// public IEnumerator GetEnumerator() { return _list.GetEnumerator(); } /// /// Creates an instance with varied instances. /// /// The length bytes. /// The items. public Sequence(byte[] length, params ISnmpData[] items) { if (items == null) { throw new ArgumentNullException(nameof(items)); } foreach (ISnmpData data in items.Where(data => data != null)) { _list.Add(data); } _length = length; } /// /// Creates an instance with varied instances. /// /// public Sequence(IEnumerable items) { if (items == null) { throw new ArgumentNullException(nameof(items)); } foreach (ISnmpData data in items.Where(data => data != null)) { _list.Add(data); } } /// /// Initializes a new instance of the class. /// /// The length. /// The stream. public Sequence(Tuple length, Stream stream) { if (length == null) { throw new ArgumentNullException(nameof(length)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } _length = length.Item2; if (length.Item1 == 0) { return; } long original = stream.Position; while (stream.Position < original + length.Item1) { _list.Add(DataFactory.CreateSnmpData(stream)); } } /// /// Item count in this . /// public int Length { get { return _list.Count; } } /// /// Gets the at the specified index. /// /// public ISnmpData this[int index] { get { return _list[index]; } } /// /// Type code. /// public SnmpType TypeCode { get { return SnmpType.Sequence; } } /// /// Appends the bytes to . /// /// The stream. public void AppendBytesTo(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (_buffer == null) { _buffer = ByteTool.ParseItems(_list); } stream.AppendBytes(TypeCode, _length, _buffer); } /// /// Returns a that represents this . /// /// public override string ToString() { StringBuilder result = new StringBuilder("SNMP SEQUENCE: "); foreach (ISnmpData item in _list) { result.Append(item).Append("; "); } return result.ToString(); } /// /// Gets the length bytes. /// /// System.Byte[]. public byte[] GetLengthBytes() { return _length; } } }