/// <summary> /// Reads a compressed 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[] ReadCompressedByteArray(this Stream stream, bool validityCheck = false) { var data = stream.ReadByteArray(); if (data.IsNullOrEmpty()) { return(null); } using (var ms = new MemoryStream(data)) using (var cms = new GZipStream(ms, CompressionMode.Decompress)) return(cms.ReadByteArray(validityCheck)); }
/// <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); } }
/// <summary> /// Reads a <see cref="System.Key"/> from this stream. /// </summary> /// <param name="stream">The stream to read.</param> /// <returns>A <see cref="System.Key"/> read from the stream.</returns> public static Key ReadKey(this Stream stream) { return(new Key(stream.ReadByteArray(), false)); }
public static object ReadObject <T>(this Stream stream, BytesToObjectConverter <T> converter, bool validityCheck = true) { return(converter(stream.ReadByteArray(validityCheck))); }
public static object ReadObject(this Stream stream) { var type = (CommonTypeStreamCode)stream.ReadByte(); switch (type) { case CommonTypeStreamCode.String: return(stream.ReadString()); case CommonTypeStreamCode.Int32: return(stream.ReadInt32()); case CommonTypeStreamCode.Int64: return(stream.ReadInt64()); case CommonTypeStreamCode.UInt32: return(stream.ReadUInt32()); case CommonTypeStreamCode.UInt64: return(stream.ReadUInt64()); case CommonTypeStreamCode.Byte: return(stream.ReadByte()); case CommonTypeStreamCode.DateTime: return(stream.ReadDateTime()); case CommonTypeStreamCode.Int16: return(stream.ReadInt16()); case CommonTypeStreamCode.UInt16: return(stream.ReadUInt16()); case CommonTypeStreamCode.Single: return(stream.ReadSingle()); case CommonTypeStreamCode.Double: return(stream.ReadDouble()); case CommonTypeStreamCode.SByte: return(stream.ReadSByte()); case CommonTypeStreamCode.ByteArray: return(stream.ReadByteArray()); case CommonTypeStreamCode.Int32Array: return(stream.ReadInt32Array(false)); case CommonTypeStreamCode.Int32List: return(new List <int>(stream.ReadInt32Array(false))); case CommonTypeStreamCode._glist: { var glistType = Type.GetType(stream.ReadString()); var glist = (IList)Activator.CreateInstance(glistType); var count = stream.ReadInt32(); var fieldCount = stream.ReadUInt16(); var propertyCount = stream.ReadUInt16(); var objType = glistType.GetGenericArguments()[0]; var fields = new FieldInfo[fieldCount]; var properties = new PropertyInfo[propertyCount]; for (var i = 0; i < fieldCount; ++i) { fields[i] = objType.GetField(stream.ReadString()); } for (var i = 0; i < propertyCount; ++i) { properties[i] = objType.GetProperty(stream.ReadString()); } for (var i = 0; i < count; ++i) { glist.Add(_innerReadObject(stream, objType, fields, properties)); } return(glist); } default: { var objName = stream.ReadString(); var fieldCount = stream.ReadUInt16(); var objType = Assembly.GetExecutingAssembly().GetType(objName); var obj = Activator.CreateInstance(objType); for (var i = 0; i < fieldCount; ++i) { objType.GetField(stream.ReadString()).SetValue(obj, stream.ReadObject()); } return(obj); } } }