/// <summary> /// Writes the data contained in the given buffer /// </summary> /// <param name="buffer">The buffer to write</param> public unsafe override void Write(ReadOnlySpan <byte> buffer) { if (buffer.IsEmpty) { return; } uint bytesWritten = 0; try { fixed(byte *b = &buffer[0]) { comStream.Write(b, (uint)buffer.Length, &bytesWritten); } } catch { } if (bytesWritten < buffer.Length) { throw new IOException(SR.DataStreamWrite); } }
private static int Write(IntPtr thisPtr, byte *pv, uint cb, uint *pcbWritten) { try { Interop.Ole32.IStream instance = ComInterfaceDispatch.GetInstance <Interop.Ole32.IStream>((ComInterfaceDispatch *)thisPtr); instance.Write(pv, cb, pcbWritten); return(S_OK); } catch (Exception ex) { Debug.WriteLine(ex); return(ex.HResult); } }
public unsafe void CopyTo(Interop.Ole32.IStream pstm, ulong cb, ulong *pcbRead, ulong *pcbWritten) { byte[] buffer = ArrayPool <byte> .Shared.Rent(4096); ulong remaining = cb; ulong totalWritten = 0; ulong totalRead = 0; fixed(byte *b = buffer) { while (remaining > 0) { uint read = remaining < (ulong)buffer.Length ? (uint)remaining : (uint)buffer.Length; Read(b, read, &read); remaining -= read; totalRead += read; if (read == 0) { break; } uint written; pstm.Write(b, read, &written); totalWritten += written; } } ArrayPool <byte> .Shared.Return(buffer); if (pcbRead != null) { *pcbRead = totalRead; } if (pcbWritten != null) { *pcbWritten = totalWritten; } }