FyreVM.Quetzal.FromStream C# (CSharp) Method

FromStream() public static method

Loads a collection of chunks from a Quetzal file.
Duplicate chunks are not supported by this class. Only the last chunk of a given type will be available.
public static FromStream ( Stream stream ) : Quetzal
stream Stream The stream to read from.
return Quetzal
        public static Quetzal FromStream(Stream stream)
        {
            Quetzal result = new Quetzal();

            uint type = BigEndian.ReadInt32(stream);
            if (type != FORM && type != LIST && type != CAT_)
                throw new ArgumentException("Invalid IFF type");

            int length = (int)BigEndian.ReadInt32(stream);
            byte[] buffer = new byte[length];
            int amountRead = stream.Read(buffer, 0, (int)length);
            if (amountRead < length)
                throw new ArgumentException("Quetzal file is too short");

            stream = new MemoryStream(buffer);
            type = BigEndian.ReadInt32(stream);
            if (type != IFZS)
                throw new ArgumentException("Wrong IFF sub-type: not a Quetzal file");

            while (stream.Position < stream.Length)
            {
                type = BigEndian.ReadInt32(stream);
                length = (int)BigEndian.ReadInt32(stream);
                byte[] chunk = new byte[length];
                amountRead = stream.Read(chunk, 0, length);
                if (amountRead < length)
                    throw new ArgumentException("Chunk extends past end of file");

                result.chunks[type] = chunk;
            }

            return result;
        }