protected override bool Read(IntPtr dataOut, int bytesToRead, out int bytesRead, CefResourceReadCallback callback)
 {
     // Backwards compatibility. ReadResponse will be called.
     callback.Dispose();
     bytesRead = -1;
     return(false);
 }
Ejemplo n.º 2
0
    protected override bool Read(IntPtr dataOut, int bytesToRead, out int bytesRead, CefResourceReadCallback callback)
    {
        bytesRead = 0;

        //We don't need the callback, as it's an unmanaged resource we should dispose it (could wrap it in a using statement).
        callback.Dispose();

        if (Stream is null)
        {
            return(false);
        }

        //Data out represents an underlying unmanaged buffer (typically 64kb in size).
        //We reuse a temp buffer where possible
        if (tempBuffer is null || tempBuffer.Length < bytesToRead)
        {
            tempBuffer = new byte[bytesToRead];
        }

        //Only read the number of bytes that can be written to dataOut
        bytesRead = Stream.Read(tempBuffer, 0, bytesToRead);

        // To indicate response completion set bytesRead to 0 and return false
        if (bytesRead == 0)
        {
            return(false);
        }

        using (var safeBuffer = new CefSafeBuffer(dataOut, (ulong)bytesRead))
            using (var dataOutStream = new UnmanagedMemoryStream(safeBuffer, 0, bytesRead, FileAccess.Write))
            {
                //We need to use bytesRead instead of tempbuffer.Length otherwise
                //garbage from the previous copy would be written to dataOut
                dataOutStream.Write(tempBuffer, 0, bytesRead);
            }

        return(bytesRead > 0);
    }