public static void Read(this Stream input, SafeUnmanagedArray buffer, long offset, int length) { try { using (UnmanagedMemoryStream output = new UnmanagedMemoryStream(buffer, offset, length, FileAccess.Write)) { byte[] buff = new byte[Math.Min(32 * 1024, length)]; input.CopyToStream(output, length, buff); } } catch { buffer.Dispose(); throw; } }
public static SafeUnmanagedArray ReadBuff(this Stream input, int size) { SafeUnmanagedArray handle = new SafeUnmanagedArray(size); try { using (UnmanagedMemoryStream output = new UnmanagedMemoryStream(handle, 0, size, FileAccess.Write)) { byte[] buff = new byte[Math.Min(32 * 1024, size)]; input.CopyToStream(output, size, buff); } } catch { handle.Dispose(); throw; } return(handle); }