/// <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); } }
public static object ReadObject <T>(this Stream stream, BytesToObjectConverter <T> converter, bool validityCheck = true) { return(converter(stream.ReadByteArray(validityCheck))); }