Esempio n. 1
0
        /// <summary>
        /// Reads the meta number from this record stream.
        /// This method only reads from the stream but does not change the current value of MetaNumber property.
        /// </summary>
        /// <returns>The meta number stored in this stream.</returns>
        protected int ReadMetaNumber()
        {
            if (_infoPos == -1)
            {
                throw new InvalidOperationException();
            }

            _infoStream.SeekTo(_infoPos);
            if (!_infoStream.Check(IOChecks.RecordStream))
            {
                throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
            }
            return(_infoStream.ReadInt32());
        }
        /// <summary>
        /// Reads a Boolean array from this stream.
        /// </summary>
        /// <param name="stream">The stream to read.</param>
        /// <param name="validityCheck">Set this parameter <c>true</c> if there is a check code before the Boolean array to prevent data corruption; otherwise, set this <c>false</c>.</param>
        /// <returns>
        /// A Boolean array read from the stream.
        /// </returns>
        /// <exception cref="System.IO.InvalidDataException">Raises if data in the stream is corrupted.</exception>
        public static bool[] ReadBooleans(this Stream stream, bool validityCheck = true)
        {
            if (!validityCheck || stream.Check(IOChecks.Booleans))
            {
                var len = stream.ReadInt32();
                if (len == 0)
                {
                    return(null);
                }
                else if (len < 0)
                {
                    throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
                }

                var count = (int)Math.Ceiling(len / 8f);
                var bytes = stream.ReadBytes(count);
                var bools = new bool[len];
                for (int i = 0; i < len; i++)
                {
                    bools[i] = bytes.GetBit(i);
                }
                return(bools);
            }
            else
            {
                throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
            }
        }
        public static string[] ReadTextList(this Stream stream, Encoding encoding, bool validityCheck)
        {
            if (!validityCheck || stream.Check(IOChecks.StringList))
            {
                var stringCount = stream.ReadInt32();
                if (stringCount < 0)
                {
                    throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
                }
                if (stringCount != 0)
                {
                    var output = new string[stringCount];
                    if (encoding == null)
                    {
                        encoding = Encoding.UTF8;
                    }
                    for (int i = 0; i < stringCount; ++i)
                    {
                        output[i] = stream.ReadText(encoding, false);
                    }
                    return(output);
                }
                return(null);
            }

            throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
        }
Esempio n. 4
0
        static void _innerReadList <T>(_readItem <T> read, Stream stream, IList <T> list, bool validityCheck = true)
        {
            //var pos = stream.Position;

            if (!validityCheck || stream.Check((Int64)29))
            {
                while (true)
                {
                    //reads and checks length
                    var len = stream.ReadInt32();
                    if (len < 0)
                    {
                        //stream.SeekTo(pos);
                        throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
                    }

                    //reads and checks occupancy
                    var occupancy = stream.ReadInt32();
                    if (occupancy < 0)
                    {
                        //stream.SeekTo(pos);
                        throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
                    }

                    //reads and check next point
                    var next = stream.ReadInt64();
                    if (next < 0)
                    {
                        //stream.SeekTo(pos);
                        throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
                    }

                    for (int i = 0; i < occupancy; i++)
                    {
                        list.Add((T)read(stream));
                    }

                    if (next == 0 || occupancy < len)
                    {
                        break;
                    }
                    else
                    {
                        stream.SeekTo(next);
                    }
                }
            }
            else
            {
                throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Reads objects from this stream and stores them in a list.
        /// </summary>
        /// <param name="stream">A <see cref="System.IO.Stream"/>.</param>
        /// <param name="list">A list used to store the objects.</param>
        /// <param name="converter">A delegate used to convert the read bytes to the desired object.</param>
        /// <param name="validityCheck">Indicates whether to read a countersign from the stream and perform the validity check.</param>
        public static void ReadObjects <T>(this Stream stream,
                                           IList <T> list, BytesToObjectConverter <T> converter, bool validityCheck = true)
        {
            var pos = stream.Position;

            if (!validityCheck || stream.Check((Int64)29))
            {
                while (true)
                {
                    var len = stream.ReadInt32();
                    if (len < 0)
                    {
                        stream.SeekTo(pos);
                        throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
                    }

                    var occupancy = stream.ReadInt32();

                    if (occupancy < 0)
                    {
                        stream.SeekTo(pos);
                        throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
                    }

                    var next = stream.ReadInt64();
                    while (occupancy > 0)
                    {
                        var p1      = stream.Position;
                        var b       = stream.ReadByteArray(validityCheck);
                        var advance = stream.Position - p1;
                        occupancy -= (int)advance;
                        list.Add(converter(b));
                    }
                    if (next <= 0)
                    {
                        break;
                    }
                    else
                    {
                        stream.SeekTo(next);
                    }
                }
            }
            else
            {
                throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Reads a System.Segment object from this stream.
 /// </summary>
 /// <param name="stream">This stream.</param>
 /// <returns>A System.Segment object.</returns>
 internal static SegmentMapping ReadSegmentMapping(this Stream stream, bool validityCheck = false)
 {
     if (!validityCheck || stream.Check(IOChecks.Common))
     {
         var b = new SegmentMapping();
         b.OriginalStartPosition = stream.ReadInt32();
         b.OriginalEndPosition   = stream.ReadInt32();
         b.MappingStartPosition  = stream.ReadInt32();
         b.MappingEndPosition    = stream.ReadInt32();
         return(b);
     }
     else
     {
         throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Reads string dictionary items from this stream.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="dictionary">A dictionary that stores dictionary items read from the stream.</param>
        /// <param name="validityCheck">
        /// <c>true</c> if a code is written into the stream
        /// and any reading method can check this code to determine whether the data is not corrupted.
        /// </param>
        /// <returns>A string dictionary read from the stream.</returns>
        public static void ReadStringDictionary(this Stream stream, IDictionary <string, string> dictionary, bool validityCheck = true)
        {
            if (validityCheck)
            {
                stream.Check(IOChecks.StringList);
            }
            var count = stream.ReadInt32();

            if (count == 0)
            {
                return;
            }
            while (count-- > 0)
            {
                dictionary.Add(stream.ReadString(false), stream.ReadString(false));
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Reads a string instance from this stream.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="validityCheck">Indicates whether to read
        /// a countersign before the string and perform data-validity check.</param>
        /// <returns>A string read from the stream.</returns>
        public static string ReadString(this Stream stream, bool validityCheck = false)
        {
            if (!validityCheck || stream.Check((Int64)17))
            {
                byte para = (byte)stream.ReadByte();
                if (para == byte.MaxValue)
                {
                    return(string.Empty);
                }

                if (para <= 0x0F)
                {
                    return(_srstring(stream, para));
                }

                int len;
                if (para.GetBitAt6())
                {
                    para = para.SetBitZeroAt6();
                    len  = stream.ReadByte();
                }
                else if (para.GetBitAt7())
                {
                    para = para.SetBitZeroAt7();
                    len  = (int)stream.ReadUInt16();
                }
                else
                {
                    len = (int)stream.ReadUInt32();
                }

                byte     cp    = (byte)(para & 0x30);
                Encoding coder = _getCoder(cp);
                if (coder == null)
                {
                    coder = Encoding.GetEncoding(stream.ReadUInt16());
                }
                var alg = (ByteCompressionMethods)((byte)(para & 0xCF));
                return(coder.GetString(stream.ReadBytes(len).DeCompress(alg)));
            }
            else
            {
                throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
            }
        }
Esempio n. 9
0
 /// <summary>
 /// Skips a byte array in this stream.
 /// </summary>
 /// <param name="stream">A stream.</param>
 /// <param name="validityCheck">Set this parameter <c>true</c> if there is a check code before the array to skip; otherwise, set this <c>false</c>.</param>
 /// <exception cref="System.IO.InvalidDataException">Raises if data in the stream is corrupted.</exception>
 public static void SkipByteArray(this Stream stream, bool validityCheck)
 {
     if (!validityCheck || stream.Check((Int64)37))
     {
         var len = stream.ReadInt32();
         if (len < 0)
         {
             throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
         }
         else if (len > 0)
         {
             stream.SeekForward(len);
         }
     }
     else
     {
         throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Skips a string instance in this stream.
        /// </summary>
        /// <param name="stream">The stream to operate on.</param>
        /// <param name="validityCheck">Indicates whether to read
        /// a countersign before the string and perform data-validity check.</param>
        public static void SkipString(this Stream stream, bool validityCheck = true)
        {
            if (!validityCheck || stream.Check((Int64)17))
            {
                byte para = (byte)stream.ReadByte();
                if (para == byte.MaxValue)
                {
                    return;
                }

                if (para <= 0x0F)
                {
                    _sfstring(stream); return;
                }

                int len = 0;
                if (para.GetBitAt6())
                {
                    len += stream.ReadByte();
                }
                else if (para.GetBitAt7())
                {
                    len += stream.ReadUInt16();
                }
                else
                {
                    len += Convert.ToInt32(stream.ReadUInt32());
                }

                byte cp = (byte)(para & 0x30);
                if (cp == 0x00)
                {
                    len += 2;
                }

                stream.Seek(len, SeekOrigin.Current);
            }
            else
            {
                throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Reads a string dictionary from this stream.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="validityCheck">
        /// <c>true</c> if a code is written into the stream
        /// and any reading method can check this code to determine whether the data is not corrupted.
        /// </param>
        /// <returns>A string dictionary read from the stream.</returns>
        public static Dictionary <string, string> ReadStringDictionary <T>(this Stream stream, bool validityCheck = true)
        {
            if (validityCheck)
            {
                stream.Check(IOChecks.StringList);
            }
            var count = stream.ReadInt32();

            if (count == 0)
            {
                return(null);
            }
            var set = new Dictionary <string, string>();

            while (count-- > 0)
            {
                set.Add(stream.ReadString(false), stream.ReadString(false));
            }
            return(set);
        }
Esempio n. 12
0
 /// <summary>
 /// Reads a byte array from this stream.
 /// </summary>
 /// <param name="stream">The stream to read from.</param>
 /// <param name="validityCheck">Set this parameter <c>true</c> if there is a check code before the array to detect data corruption; otherwise, set this <c>false</c>.</param>
 /// <returns>
 /// The byte array read from the stream.
 /// </returns>
 /// <exception cref="System.IO.InvalidDataException">Raises if data in the stream is corrupted.</exception>
 public static byte[] ReadByteArray(this Stream stream, bool validityCheck)
 {
     if (!validityCheck || stream.Check((Int64)37))
     {
         var len = stream.ReadInt32();
         if (len == 0)
         {
             return(null);
         }
         else if (len < 0)
         {
             throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
         }
         return(stream.ReadBytes(len));
     }
     else
     {
         throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
     }
 }
Esempio n. 13
0
        /// <summary>
        /// Reads a string hash set from this stream.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="validityCheck">
        /// <c>true</c> if a code is written into the stream
        /// and any reading method can check this code to determine whether the data is not corrupted.
        /// </param>
        /// <returns>A string hash set read from the stream.</returns>
        public static HashSet <string> ReadStringHashSet(this Stream stream, bool validityCheck = true)
        {
            if (validityCheck)
            {
                stream.Check((Int64)18);
            }
            var count = stream.ReadInt32();

            if (count == 0)
            {
                return(null);
            }
            var set = new HashSet <string>();

            while (count-- > 0)
            {
                set.Add(stream.ReadString(false));
            }
            return(set);
        }
Esempio n. 14
0
 /// <summary>
 /// Reads a System.DateTime integer array from this stream. The method does not require an argument specifying the length of the array; <seealso cref="ReadDateTimes"/>.
 /// </summary>
 /// <param name="stream">The stream to read.</param>
 /// <param name="validityCheck">Set this parameter <c>true</c> if there is a check code before the array to prevent data corruption; otherwise, set this <c>false</c>.</param>
 /// <param name="buffer">A byte array used to temporarily store data read from the stream.</param>
 /// <returns>
 /// The System.DateTime array read from the stream.
 /// </returns>
 /// <exception cref="System.IO.InvalidDataException">Raises if data in the stream is corrupted.</exception>
 public static DateTime[] ReadDateTimeArray(this Stream stream, byte[] buffer, bool validityCheck = true)
 {
     if (!validityCheck || stream.Check((long)37))
     {
         var len = stream.ReadInt32(buffer);
         if (len == 0)
         {
             return(null);
         }
         else if (len < 0)
         {
             throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
         }
         return(stream.ReadDateTimes(len, buffer));
     }
     else
     {
         throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
     }
 }
Esempio n. 15
0
 /// <summary>
 /// Reads a System.DateTime integer array from this stream into the specified buffer. This method avoids an additional data copy to enchance performance.
 /// </summary>
 /// <param name="stream">The stream to read.</param>
 /// <param name="validityCheck">Set this parameter <c>true</c> if there is a check code before the array to prevent data corruption; otherwise, set this <c>false</c>.</param>
 /// <param name="buffer">A byte array used to store data read from the stream. You may later get a DateTime pointer of this array for fast iteration.</param>
 /// <returns>
 /// true if the array read from the stream is not empty; otherwise, false.
 /// </returns>
 /// <exception cref="System.IO.InvalidDataException">Raises if data in the stream is corrupted.</exception>
 public static unsafe bool ReadDateTimeArrayToBuffer(this Stream stream, byte[] buffer, bool validityCheck = true)
 {
     if (!validityCheck || stream.Check((long)37))
     {
         var len = stream.ReadInt32(buffer);
         if (len == 0)
         {
             return(false);
         }
         else if (len < 0)
         {
             throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
         }
         stream.Read(buffer, 0, len * sizeof(DateTime));
         return(true);
     }
     else
     {
         throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
     }
 }
Esempio n. 16
0
 public static IEnumerator <string> ReadStringListEnumerator(this Stream stream, bool validityCheck = true)
 {
     if (!validityCheck || stream.Check(IOChecks.StringList))
     {
         var stringCount = stream.ReadInt32();
         if (stringCount < 0)
         {
             throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
         }
         else if (stringCount != 0)
         {
             for (int i = 0; i < stringCount; ++i)
             {
                 yield return(stream.ReadString(false));
             }
         }
     }
     else
     {
         throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
     }
 }
Esempio n. 17
0
        static void _innerWriteList <T>(_writeItem <T> write, Stream stream, IList <T> list, WritingMode mode = WritingMode.New, bool validityCheck = true)
        {
            //TODO need to deal with objects of variable binary length

            if (mode == WritingMode.New)
            {
                if (validityCheck)
                {
                    stream.WriteCheckCode((long)29);
                }

                if (list == null || list.Count == 0)
                {
                    stream.WriteInt32(0); // current block length (total number of available slots for the objects to write)
                    stream.WriteInt32(0); // current block occupancy (actual number of objects in the current block)
                    stream.WriteInt64(0); // next block position
                    return;
                }

                stream.WriteInt32(list.Count);
                stream.WriteInt32(list.Count);
                stream.WriteInt64(0);
                for (var i = 0; i < list.Count; i++)
                {
                    write(stream, list[i]);
                }
            }
            else if (mode == WritingMode.Override)
            {
                if (!validityCheck || stream.Check((Int64)29))
                {
                    var i = 0;
                    var totalItemCountToWrite = list.Count;
                    while (true)
                    {
                        var count = stream.ReadInt32();
                        if (list.Count == 0)
                        {
                            stream.SkipInt32();
                            stream.WriteInt32(0); // writes occupancy as zero
                            return;
                        }

                        var occupancyCountPtrPos = stream.Position;
                        stream.SkipInt32();

                        var nextPtrPos        = stream.Position;
                        var nextBlockPosition = stream.ReadInt64();

                        var oi = i;
                        for (; count > 0 && i < totalItemCountToWrite; count--, i++)
                        {
                            write(stream, list[i]);
                        }

                        var currPos = stream.Position;
                        stream.SeekTo(occupancyCountPtrPos);
                        stream.WriteInt32(i - oi);
                        stream.SeekTo(currPos);

                        if (i < totalItemCountToWrite)
                        {
                            if (nextBlockPosition == 0)
                            {
                                stream.SeekTo(nextPtrPos); // writes the position of the new block
                                stream.WriteInt64(stream.Length);
                                stream.SeekToEnd();

                                stream.WriteInt32(totalItemCountToWrite - i);
                                stream.WriteInt32(totalItemCountToWrite - i);

                                stream.WriteInt64(0);
                                for (; i < totalItemCountToWrite; i++)
                                {
                                    write(stream, list[i]);
                                }
                                break;
                            }
                            else
                            {
                                stream.SeekTo(nextBlockPosition);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
                }
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Writes a set of objects, each of them fetched from an enumerator, to this stream.
        /// </summary>
        /// <param name="stream">A <see cref="System.IO.Stream"/>.</param>
        /// <param name="enumerator">An enumerator used to get each object.</param>
        /// <param name="mode">Indicates whether to override existing list or create a new list in the stream.</param>
        /// <param name="converter">A delegate used to convert each item in the list to a byte array.</param>
        /// <param name="validityCheck">Indicates whether to write a validity-check countersign before the list in the stream.</param>
        public static void WriteObjects <T>(this Stream stream, IEnumerator enumerator, WritingMode mode,
                                            ObjectToBytesConverter <T> converter, bool validityCheck = true)
        {
            if (mode == WritingMode.New)
            {
                if (validityCheck)
                {
                    stream.WriteCheckCode((Int64)29);
                }

                if (enumerator.MoveNext())
                {
                    _writeEnumeratorToEnd(stream, enumerator, converter, validityCheck);
                }
                else
                {
                    stream.WriteInt32(0);
                    stream.WriteInt32(0);
                    stream.WriteInt64(0);
                }
            }
            else
            {
                if (!validityCheck || stream.Check((Int64)29))
                {
                    int  i        = 0;
                    bool moreObjs = enumerator.MoveNext();
                    if (!moreObjs)
                    {
                        stream.SkipInt32();
                        stream.WriteInt32(0);
                        var next = stream.ReadInt64();
                        stream.WriteInt64(-Math.Abs(next));
                        return;
                    }

                    while (moreObjs)
                    {
                        var len  = stream.ReadInt32();
                        var olen = len;

                        var usedLenPos = stream.Position;
                        var usedLen    = stream.ReadInt32();

                        var nextPtrPos = stream.Position;
                        var next       = stream.ReadInt64();

                        int objLen;
                        for (; len > 0 && moreObjs; len -= objLen, i++)
                        {
                            var b = converter((T)enumerator.Current);
                            objLen = stream._byteArrayOccupancy(b, validityCheck);

                            if (objLen > len)
                            {
                                break;
                            }
                            else
                            {
                                stream.WriteByteArray(b, validityCheck);
                                moreObjs = enumerator.MoveNext();
                            }
                        }

                        var nUsedLen = olen - len;

                        var currPos = stream.Position;
                        stream.SeekTo(usedLenPos);
                        stream.WriteInt32(nUsedLen);
                        stream.SeekTo(currPos);

                        if (moreObjs)
                        {
                            if (next == 0)
                            {
                                stream.SeekTo(nextPtrPos);
                                stream.WriteInt64(stream.Length);
                                stream.SeekToEnd();

                                _writeEnumeratorToEnd(stream, enumerator, converter, validityCheck);
                                break;
                            }
                            else
                            {
                                if (next < 0)
                                {
                                    stream.SeekTo(nextPtrPos);
                                    next = -next;
                                    stream.WriteInt64(next);
                                }
                                stream.SeekTo(next);
                            }
                        }
                        else
                        {
                            if (next != 0)
                            {
                                currPos = stream.Position;
                                stream.SeekTo(nextPtrPos);
                                stream.WriteInt64(-Math.Abs(next));
                                stream.SeekTo(currPos);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    throw new InvalidDataException(IOResources.ERR_StreamExtension_DataIrrecognizable);
                }
            }
        }