Beispiel #1
0
        /// <summary>
        /// Factory method for opening the memory mapped file. The content of the map is assumed to come from localFileName.
        /// This can throw FileLoadException in case of error.
        /// </summary>
        /// <param name="localFileName"></param>
        /// <param name="binaryDocument"></param>
        /// <returns></returns>
        public static UnmanagedBinaryMemoryBlock CreateUnmanagedBinaryMemoryBlock(
            string localFileName,
            IBinaryDocument binaryDocument
            )
        {
            using (FileStream stream = new FileStream(localFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                if (stream.Length != binaryDocument.Length)
                {
                    throw new IOException("File size difference: " + localFileName);
                }
                if (stream.Length > Int32.MaxValue)
                {
                    throw new IOException("File too Big: " + localFileName);
                }
                UnmanagedBinaryMemoryBlock unmanagedBinaryMemoryBlock = new UnmanagedBinaryMemoryBlock(binaryDocument);
                byte *pMainBuffer = (byte *)unmanagedBinaryMemoryBlock._pointer;
                //Read a fixed length block at a time, so that the GC does not come under pressure from lots of large byte arrays.
                int    fileRemainingLen = (int)binaryDocument.Length;
                int    copyBufferLen    = 8096;
                byte[] tempBuffer       = new byte[copyBufferLen];
                fixed(byte *tempBufferPtr = tempBuffer)
                {
                    while (fileRemainingLen > 0)
                    {
                        if (fileRemainingLen < copyBufferLen)
                        {
                            copyBufferLen = fileRemainingLen;
                        }
                        stream.Read(tempBuffer, 0, copyBufferLen);
                        byte *iterBuffer = tempBufferPtr;
                        byte *endBuffer  = tempBufferPtr + copyBufferLen;
                        while (iterBuffer < endBuffer)
                        {
                            *pMainBuffer++ = *iterBuffer++;
                        }
                        fileRemainingLen -= copyBufferLen;
                    }
                }

                return(unmanagedBinaryMemoryBlock);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates an unmanaged binary memory block and copies the contents of the given byte enumeration into the block.
        /// </summary>
        /// <param name="stream">A stream of bytes that are to be copied into the resulting memory block.</param>
        /// <param name="binaryDocument">The binary document whose contents are stored in the given file.</param>
        /// <exception cref="System.IO.IOException">The length of the stream is not the same as the length of the binary document, or the stream length is greater than Int32.MaxValue.</exception>
        public static UnmanagedBinaryMemoryBlock CreateUnmanagedBinaryMemoryBlock(IEnumerable <byte> stream, IBinaryDocument binaryDocument)
        {
            UnmanagedBinaryMemoryBlock unmanagedBinaryMemoryBlock = new UnmanagedBinaryMemoryBlock(binaryDocument);
            byte *pMainBuffer = (byte *)unmanagedBinaryMemoryBlock.Pointer;
            byte *endOfBuffer = pMainBuffer + binaryDocument.Length;

            foreach (var b in stream)
            {
                if (pMainBuffer == endOfBuffer)
                {
                    throw new IOException("stream length != binaryDocument.Length: " + binaryDocument.Location);
                }
                *pMainBuffer++ = b;
            }
            if (pMainBuffer != endOfBuffer)
            {
                throw new IOException("stream length != binaryDocument.Length: " + binaryDocument.Location);
            }
            return(unmanagedBinaryMemoryBlock);
        }
Beispiel #3
0
        /// <summary>
        /// Creates an unmanaged binary memory block and copies the contents of the given stream into the block.
        /// </summary>
        /// <param name="stream">A stream of bytes that are to be copied into the resulting memory block.</param>
        /// <param name="binaryDocument">The binary document whose contents are stored in the given file.</param>
        /// <exception cref="System.IO.IOException">The length of the stream is not the same as the length of the binary document, or the stream length is greater than Int32.MaxValue.</exception>
        public static UnmanagedBinaryMemoryBlock CreateUnmanagedBinaryMemoryBlock(Stream stream, IBinaryDocument binaryDocument)
        {
            if (stream.Length != binaryDocument.Length)
            {
                throw new IOException("stream.Length != binaryDocument.Length: " + binaryDocument.Location);
            }
            if (stream.Length > Int32.MaxValue)
            {
                throw new IOException("stream.Length > Int32.MaxValue: " + binaryDocument.Location);
            }
            UnmanagedBinaryMemoryBlock unmanagedBinaryMemoryBlock = new UnmanagedBinaryMemoryBlock(binaryDocument);
            byte *pMainBuffer = (byte *)unmanagedBinaryMemoryBlock.Pointer;
            //Read a fixed length block at a time, so that the GC does not come under pressure from lots of large byte arrays.
            int remainingLength  = (int)binaryDocument.Length;
            int copyBufferLength = 8096;

            byte[] tempBuffer = new byte[copyBufferLength];
            fixed(byte *tempBufferPtr = tempBuffer)
            {
                while (remainingLength > 0)
                {
                    if (remainingLength < copyBufferLength)
                    {
                        copyBufferLength = remainingLength;
                    }
                    stream.Read(tempBuffer, 0, copyBufferLength);
                    byte *iterBuffer = tempBufferPtr;
                    byte *endBuffer  = tempBufferPtr + copyBufferLength;
                    while (iterBuffer < endBuffer)
                    {
                        *pMainBuffer++ = *iterBuffer++;
                    }
                    remainingLength -= copyBufferLength;
                }
            }

            return(unmanagedBinaryMemoryBlock);
        }
 /// <summary>
 /// Creates an unmanaged binary memory block and copies the contents of the given stream into the block.
 /// </summary>
 /// <param name="stream">A stream of bytes that are to be copied into the resulting memory block.</param>
 /// <param name="binaryDocument">The binary document whose contents are stored in the given file.</param>
 /// <exception cref="System.IO.IOException">The length of the stream is not the same as the length of the binary document, or the stream length is greater than Int32.MaxValue.</exception>
 public static UnmanagedBinaryMemoryBlock CreateUnmanagedBinaryMemoryBlock(Stream stream, IBinaryDocument binaryDocument)
 {
     if (stream.Length != binaryDocument.Length)
     throw new IOException("stream.Length != binaryDocument.Length: " + binaryDocument.Location);
       if (stream.Length > Int32.MaxValue)
     throw new IOException("stream.Length > Int32.MaxValue: " + binaryDocument.Location);
       UnmanagedBinaryMemoryBlock unmanagedBinaryMemoryBlock = new UnmanagedBinaryMemoryBlock(binaryDocument);
       byte* pMainBuffer = (byte*)unmanagedBinaryMemoryBlock.Pointer;
       //Read a fixed length block at a time, so that the GC does not come under pressure from lots of large byte arrays.
       int remainingLength = (int)binaryDocument.Length;
       int copyBufferLength = 8096;
       byte[] tempBuffer = new byte[copyBufferLength];
       fixed (byte* tempBufferPtr = tempBuffer) {
     while (remainingLength > 0) {
       if (remainingLength < copyBufferLength) {
     copyBufferLength = remainingLength;
       }
       stream.Read(tempBuffer, 0, copyBufferLength);
       byte* iterBuffer = tempBufferPtr;
       byte* endBuffer = tempBufferPtr + copyBufferLength;
       while (iterBuffer < endBuffer) {
     *pMainBuffer++ = *iterBuffer++;
       }
       remainingLength -= copyBufferLength;
     }
       }
       return unmanagedBinaryMemoryBlock;
 }
Beispiel #5
0
 /// <summary>
 /// Creates an unmanaged binary memory block and copies the contents of the given byte enumeration into the block.
 /// </summary>
 /// <param name="stream">A stream of bytes that are to be copied into the resulting memory block.</param>
 /// <param name="binaryDocument">The binary document whose contents are stored in the given file.</param>
 /// <exception cref="System.IO.IOException">The length of the stream is not the same as the length of the binary document, or the stream length is greater than Int32.MaxValue.</exception>
 public static UnmanagedBinaryMemoryBlock CreateUnmanagedBinaryMemoryBlock(IEnumerable<byte> stream, IBinaryDocument binaryDocument) {
   UnmanagedBinaryMemoryBlock unmanagedBinaryMemoryBlock = new UnmanagedBinaryMemoryBlock(binaryDocument);
   byte* pMainBuffer = (byte*)unmanagedBinaryMemoryBlock.Pointer;
   byte* endOfBuffer = pMainBuffer + binaryDocument.Length;
   foreach (var b in stream) {
     if (pMainBuffer == endOfBuffer) throw new IOException("stream length != binaryDocument.Length: " + binaryDocument.Location);
     *pMainBuffer++ = b;
   }
   if (pMainBuffer != endOfBuffer) throw new IOException("stream length != binaryDocument.Length: " + binaryDocument.Location);
   return unmanagedBinaryMemoryBlock;
 }
Beispiel #6
0
 /// <summary>
 /// Factory method for opening the memory mapped file. The content of the map is assumed to come from localFileName.
 /// This can throw FileLoadException in case of error.
 /// </summary>
 /// <param name="localFileName"></param>
 /// <param name="binaryDocument"></param>
 /// <returns></returns>
 public static UnmanagedBinaryMemoryBlock CreateUnmanagedBinaryMemoryBlock(
   string localFileName,
   IBinaryDocument binaryDocument
 ) {
   using (FileStream stream = new FileStream(localFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
     if (stream.Length != binaryDocument.Length)
       throw new IOException("File size difference: " + localFileName);
     if (stream.Length > Int32.MaxValue)
       throw new IOException("File too Big: " + localFileName);
     UnmanagedBinaryMemoryBlock unmanagedBinaryMemoryBlock = new UnmanagedBinaryMemoryBlock(binaryDocument);
     byte* pMainBuffer = (byte*)unmanagedBinaryMemoryBlock.Pointer;
     //Read a fixed length block at a time, so that the GC does not come under pressure from lots of large byte arrays.
     int fileRemainingLen = (int)binaryDocument.Length;
     int copyBufferLen = 8096;
     byte[] tempBuffer = new byte[copyBufferLen];
     fixed (byte* tempBufferPtr = tempBuffer) {
       while (fileRemainingLen > 0) {
         if (fileRemainingLen < copyBufferLen) {
           copyBufferLen = fileRemainingLen;
         }
         stream.Read(tempBuffer, 0, copyBufferLen);
         byte* iterBuffer = tempBufferPtr;
         byte* endBuffer = tempBufferPtr + copyBufferLen;
         while (iterBuffer < endBuffer) {
           *pMainBuffer++ = *iterBuffer++;
         }
         fileRemainingLen -= copyBufferLen;
       }
     }
     return unmanagedBinaryMemoryBlock;
   }
 }