示例#1
0
 public SimpleList(SimpleList <TData> InitialList)
 {
     this.Add(InitialList);
 }
示例#2
0
        // -------------------------------------------------------------------------------------------
        /// <summary>
        /// Gives, in a target byte array, another one readed from a stream, until consume a number of bytes or before some marks conformed by more than one byte.
        /// Return the index of the found mark or -1 if the consumable bytes limit was reached.
        /// </summary>
        private static int CaptureSegmentWithComplexStreamReader(ref byte[] TargetSegment, BinaryReader TorrentReader, int BytesNumberLimit,
                                                                 int MarksMaxLength, params byte[][] LimitMarks)
        {
            int CaptureEndingCause    = -1;
            int MarksCount            = LimitMarks.GetLength(0);
            SimpleList <byte[]> Parts = new SimpleList <byte[]>();

            byte[] Part = new byte[0];
            int    ConsumedBytesCount = 0;
            int    ConsumedBytesTotal = 0;
            int    FoundedPosition    = 0;
            int    Pos = 0;

            while ((ConsumedBytesCount = (Part = TorrentReader.ReadBytes(BLOCK_SIZE_TINY)).Length) > 0)
            {
                ConsumedBytesTotal += ConsumedBytesCount;
                Pos = -1;
                foreach (byte[] Mark in LimitMarks)
                {
                    Pos++;
                    if ((FoundedPosition = Search(Part, Mark)) >= 0)
                    {
                        CaptureEndingCause = Pos;
                        TorrentReader.BaseStream.Seek((Part.Length - (FoundedPosition + Mark.Length)) * -1, SeekOrigin.Current);
                        Part = ExtractSegment(Part, 0, FoundedPosition);
                        break;
                    }
                }

                if (FoundedPosition >= 0)
                {
                    Parts.Add(Part);
                    break;
                }

                if (ConsumedBytesTotal >= BytesNumberLimit)
                {
                    throw new UsageAnomaly("Stream has been readed/consumed until reach the " + BytesNumberLimit.ToString() + " bytes limit.",
                                           new DataWagon("ConsumedBytesCount", ConsumedBytesCount));
                }

                if (ConsumedBytesCount == BLOCK_SIZE_TINY)
                {
                    Part = ExtractSegment(Part, 0, Part.Length - MarksMaxLength);
                }

                Parts.Add(Part);

                // If less than the expected bytes were read, then means the stream's end was reached.
                if (ConsumedBytesCount < BLOCK_SIZE_TINY)
                {
                    break;
                }

                TorrentReader.BaseStream.Seek(MarksMaxLength * -1, SeekOrigin.Current);
            }

            TargetSegment = new byte[((BLOCK_SIZE_TINY - MarksMaxLength) * (Parts.Count - 1)) + Part.Length];
            Pos           = 0;
            foreach (byte[] Fragment in Parts)
            {
                CopyByteArray(TargetSegment, Fragment, Pos, (byte)0, false);
                Pos += Fragment.Length;
            }

            return(CaptureEndingCause);
        }
示例#3
0
 /// <summary>
 /// Constructor
 /// </summary>
 internal InternalEnumerator(SimpleList <TData> List)
 {
     this.TraveledList = List;
 }
示例#4
0
        // -------------------------------------------------------------------------------------------
        /// <summary>
        /// Gives, in a target byte array, another one readed from a stream, until consume a number of bytes or before some marks conformed by only one byte.
        /// Return the index of the found mark or -1 if the consumable bytes limit was reached.
        /// </summary>
        private static int CaptureSegmentWithSimpleStreamReader(ref byte[] TargetSegment, BinaryReader TorrentReader, int BytesNumberLimit,
                                                                params byte[][] LimitMarks)
        {
            int  CaptureEndingCause = -1;
            int  MarksCount         = LimitMarks.GetLength(0);
            int  Ind = 0;
            int  Pos = 0;
            int  ConsumedBytesCount = 0;
            bool FoundedMark        = false;
            byte Atom = 0;
            SimpleList <byte[]> Parts = new SimpleList <byte[]>();

            byte[] Part           = new byte[0];
            int    PartBytesCount = 0;

            Parts.Add(Part);

            try
            {
                while (ConsumedBytesCount < BytesNumberLimit)
                {
                    Atom = TorrentReader.ReadByte();
                    ConsumedBytesCount++;
                    FoundedMark = false;

                    for (Ind = 0; Ind < MarksCount; Ind++)
                    {
                        if (Atom == LimitMarks[Ind][0])
                        {
                            FoundedMark        = true;
                            CaptureEndingCause = Ind;
                            break;
                        }
                    }

                    if (FoundedMark)
                    {
                        break;
                    }

                    if (PartBytesCount == BLOCK_SIZE_TINY)
                    {
                        Part           = new byte[BLOCK_SIZE_TINY];
                        PartBytesCount = 0;
                        Parts.Add(Part);
                    }

                    Part[PartBytesCount] = Atom;
                    PartBytesCount++;
                }

                throw new UsageAnomaly("Stream has been readed/consumed until reach the " + BytesNumberLimit.ToString() + " bytes limit.",
                                       new DataWagon("ConsumedBytesCount", ConsumedBytesCount));
            }
#pragma warning disable 168
            catch (EndOfStreamException Anomaly)
            {   // The possible end of the stream is expected.
            };
#pragma warning restore 168

            TargetSegment = new byte[(BLOCK_SIZE_TINY * (Parts.Count - 1)) + PartBytesCount];
            Ind           = 0;
            Pos           = 0;
            foreach (byte[] Fragment in Parts)
            {
                Ind++;
                CopyByteArray(TargetSegment, (Ind < Parts.Count ? Fragment : BytesHandling.ExtractSegment(Fragment, 0, PartBytesCount)), Pos, (byte)0, false);
                Pos += BLOCK_SIZE_TINY;
            }

            return(CaptureEndingCause);
        }