public bool Equals(ComAdapterStream stream)
 {
     if (stream == null)
     {
         return(false);
     }
     return(stream.source == this.source);
 }
        public static void CopyTo(this Stream source, com.IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
        {
            if (!source.CanRead)
            {
                throw new NotSupportedException("Source stream doesn't support reading.");
            }
            using (var destination = new ComAdapterStream(pstm))
            {
                if (!destination.CanWrite)
                {
                    throw new NotSupportedException("Destination stream doesn't support writing.");
                }
                var buffer            = new byte[4096];
                var totalBytesRead    = 0L;
                var totalBytesWritten = 0L;
                var chunkLength       = buffer.Length;
                if (cb < chunkLength)
                {
                    chunkLength = (int)cb;
                }
                while (totalBytesWritten < cb)
                {
                    // Read
                    var bytesRead = source.Read(buffer, 0, chunkLength);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    totalBytesRead += bytesRead;
                    // Write
                    var previousPosition = destination.Position;
                    destination.Write(buffer, 0, bytesRead);
                    var bytesWritten = destination.Position - previousPosition;
                    if (bytesWritten == 0)
                    {
                        break;
                    }
                    totalBytesWritten += bytesWritten;

                    if (bytesRead != bytesWritten)
                    {
                        break;
                    }
                }
                if (pcbRead != IntPtr.Zero)
                {
                    iop.Marshal.WriteInt64(pcbRead, totalBytesRead);
                }
                if (pcbWritten != IntPtr.Zero)
                {
                    iop.Marshal.WriteInt64(pcbWritten, totalBytesWritten);
                }
            }
        }