Example #1
0
        /// <summary>
        /// Initializes a stream as a fixed-sized VDI file.
        /// </summary>
        /// <param name="stream">The stream to initialize.</param>
        /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
        /// <param name="capacity">The desired capacity of the new disk</param>
        /// <returns>An object that accesses the stream as a VDI file</returns>
        public static DiskImageFile InitializeFixed(Stream stream, Ownership ownsStream, long capacity)
        {
            PreHeaderRecord preHeader = PreHeaderRecord.Initialized();
            HeaderRecord    header    = HeaderRecord.Initialized(ImageType.Fixed, ImageFlags.None, capacity, 1024 * 1024, 0);

            byte[] blockTable = new byte[header.BlockCount * 4];
            for (int i = 0; i < header.BlockCount; ++i)
            {
                Utilities.WriteBytesLittleEndian((uint)i, blockTable, i * 4);
            }

            header.BlocksAllocated = header.BlockCount;

            stream.Position = 0;
            preHeader.Write(stream);
            header.Write(stream);

            stream.Position = header.BlocksOffset;
            stream.Write(blockTable, 0, blockTable.Length);

            long totalSize = header.DataOffset + ((long)header.BlockSize * (long)header.BlockCount);

            if (stream.Length < totalSize)
            {
                stream.SetLength(totalSize);
            }

            return(new DiskImageFile(stream, ownsStream));
        }
Example #2
0
        /// <summary>
        /// Initializes a stream as a dynamically-sized VDI file.
        /// </summary>
        /// <param name="stream">The stream to initialize.</param>
        /// <param name="ownsStream">Indicates if the new instance controls the lifetime of the stream.</param>
        /// <param name="capacity">The desired capacity of the new disk</param>
        /// <returns>An object that accesses the stream as a VDI file</returns>
        public static DiskImageFile InitializeDynamic(Stream stream, Ownership ownsStream, long capacity)
        {
            PreHeaderRecord preHeader = PreHeaderRecord.Initialized();
            HeaderRecord    header    = HeaderRecord.Initialized(ImageType.Dynamic, ImageFlags.None, capacity, 1024 * 1024, 0);

            byte[] blockTable = new byte[header.BlockCount * 4];
            for (int i = 0; i < blockTable.Length; ++i)
            {
                blockTable[i] = 0xFF;
            }

            header.BlocksAllocated = 0;

            stream.Position = 0;
            preHeader.Write(stream);
            header.Write(stream);

            stream.Position = header.BlocksOffset;
            stream.Write(blockTable, 0, blockTable.Length);

            return(new DiskImageFile(stream, ownsStream));
        }