public unsafe byte[] ReadFromStreamMaxLength(string streamName, int maxSize) { byte[] result = null; Util.InvokeComCall(MsgStorageErrorCode.FailedRead, delegate { Interop.IStream stream = null; try { this.iStorage.OpenStream(streamName, IntPtr.Zero, 16U, 0U, out stream); System.Runtime.InteropServices.ComTypes.STATSTG statstg; stream.Stat(out statstg, 1U); if (statstg.cbSize > (long)maxSize) { throw new MsgStorageException(MsgStorageErrorCode.StorageStreamTooLong, MsgStorageStrings.StreamTooBig(streamName, statstg.cbSize)); } int num = (int)statstg.cbSize; int num2 = 0; result = new byte[num]; if (result.Length != 0) { try { fixed(byte *ptr = &result[0]) { stream.Read(ptr, result.Length, out num2); } } finally { byte *ptr = null; } } if (num2 != result.Length) { throw new MsgStorageException(MsgStorageErrorCode.StorageStreamTruncated, MsgStorageStrings.FailedRead(streamName)); } } finally { if (stream != null) { Marshal.ReleaseComObject(stream); } } }); return(result); }
public unsafe int ReadFromStream(string streamName, byte[] buffer, int size) { this.CheckDisposed("ComStream::ReadFromStream"); Util.ThrowOnNullArgument(streamName, "streamName"); Util.ThrowOnNullArgument(buffer, "buffer"); Util.ThrowOnOutOfRange(size >= 0, "size"); int result = 0; Util.InvokeComCall(MsgStorageErrorCode.FailedRead, delegate { Interop.IStream stream = null; try { this.iStorage.OpenStream(streamName, IntPtr.Zero, 16U, 0U, out stream); int result = 0; if (size != 0) { try { fixed(byte *ptr = &buffer[0]) { stream.Read(ptr, size, out result); } } finally { byte *ptr = null; } } result = result; } finally { if (stream != null) { Marshal.ReleaseComObject(stream); } } }); return(result); }