Esempio n. 1
0
        public static unsafe void ReadBytes(this BinaryReader reader, void *destination, long destinationSizeInBytes, long bytesToRead, ref byte[] work)
        {
            Contracts.AssertValue(reader);
            Contracts.Assert(bytesToRead >= 0);
            Contracts.Assert(destinationSizeInBytes >= bytesToRead);
            Contracts.Assert(destination != null);
            Contracts.AssertValueOrNull(work);

            // Size our read buffer to 70KB to stay off the LOH.
            const int blockSize       = 70 * 1024;
            int       desiredWorkSize = (int)Math.Min(blockSize, bytesToRead);

            EnsureSize(ref work, desiredWorkSize);

            fixed(void *src = work)
            {
                long offset = 0;

                while (offset < bytesToRead)
                {
                    int toRead = (int)Math.Min(bytesToRead - offset, blockSize);
                    int read   = reader.Read(work, 0, toRead);
                    Contracts.CheckDecode(read == toRead);
                    MemUtils.MemoryCopy(src, (byte *)destination + offset, destinationSizeInBytes - offset, read);
                    offset += read;
                }
                Contracts.Assert(offset == bytesToRead);
            }
        }