public override IBinaryDocumentMemoryBlock OpenBinaryDocument(IBinaryDocument sourceDocument) { try { return(base.OpenBinaryDocument(sourceDocument)); } catch (TypeLoadException) { return(ManagedBinaryMemoryBlock.CreateManagedBinaryMemoryBlock(sourceDocument.Location, sourceDocument)); } }
/// <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 ManagedBinaryMemoryBlock CreateManagedBinaryMemoryBlock( 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); } ManagedBinaryMemoryBlock managedBinaryMemoryBlock = new ManagedBinaryMemoryBlock(binaryDocument); byte *pMainBuffer = (byte *)managedBinaryMemoryBlock._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(managedBinaryMemoryBlock); } }
/// <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 ManagedBinaryMemoryBlock CreateManagedBinaryMemoryBlock( 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); ManagedBinaryMemoryBlock managedBinaryMemoryBlock = new ManagedBinaryMemoryBlock(binaryDocument); byte* pMainBuffer = (byte*)managedBinaryMemoryBlock._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 managedBinaryMemoryBlock; } }