Provides utility methods for working with byte arrays and pointers.
Beispiel #1
0
        void Preload()
        {
            using (FileStream fs = OpenRead()) {
                CacheSize = (int)(fs.Length / BlockDBEntry.Size);
                EnsureCapacity(CacheSize);
                LastFlushedIndex = CacheSize;

                // Converting from byte[] to BlockDBEntry[] on the fly
                // This is possible because BlockDBEntry is a sequentially packed struct
                fixed(BlockDBEntry *pCacheStart = cacheStore)
                {
                    fixed(byte *pBuffer = ioBuffer)
                    {
                        byte *pCache = (byte *)pCacheStart;

                        while (fs.Position < fs.Length)
                        {
                            int bytesToRead   = Math.Min(BufferSize, (int)(fs.Length - fs.Position));
                            int bytesInBuffer = 0;
                            do
                            {
                                int bytesRead = fs.Read(ioBuffer, bytesInBuffer, BufferSize - bytesInBuffer);
                                bytesInBuffer += bytesRead;
                            } while(bytesInBuffer < bytesToRead);
                            BufferUtil.MemCpy(pBuffer, pCache, bytesInBuffer);
                            pCache += bytesInBuffer;
                        }
                    }
                }
            }
        }