/// <summary> /// Read() reads from an offset in the file. A PP_ArrayOutput must be /// provided so that output will be stored in its allocated buffer. This /// function might perform a partial read. /// </summary> /// <param name="buffer">An ArraySegment<byte> to hold the specified number of bytes read.</param> /// <param name="offset">The offset into the file.</param> /// <param name="bytesToRead">The number of bytes to read from <code>offset</code>.</param> /// <returns>Error code. If the return value is 0, then end-of-file was reached.</returns> public PPError Read(ArraySegment <byte> buffer, int offset, int bytesToRead) { var readToArrayAction = new Action <PPError, byte[]>( (result, bytes) => { Array.Copy(bytes, buffer.Array, Math.Min(bytes.Length, buffer.Count)); OnReadData(result); } ); var arrayOutput = new CompletionCallbackWithOutput <byte[]>(new CompletionCallbackWithOutputFunc <byte[]>(readToArrayAction)); return((PPError)PPBFileIO.ReadToArray(this, offset, bytesToRead, ref arrayOutput.ArrayOutput, arrayOutput)); }
private async Task <FileIOResult> ReadAsyncCore(ArraySegment <byte> buffer, int offset, int size, MessageLoop messageLoop = null) { var tcs = new TaskCompletionSource <FileIOResult>(); EventHandler <FileIOResult> handler = (s, e) => { tcs.TrySetResult(e); }; try { HandleReadData += handler; if (MessageLoop == null && messageLoop == null) { Read(buffer, offset, size); } else { Action <PPError> action = new Action <PPError>((e) => { using (var arrayOutput = new ArrayOutputAdapterWithStorage <byte[]>()) { PPArrayOutput ao = arrayOutput.PPArrayOutput; var result = (PPError)PPBFileIO.ReadToArray(this, offset, size, ref ao, new BlockUntilComplete()); var bytes = arrayOutput.Output; Array.Copy(bytes, buffer.Array, Math.Min(bytes.Length, buffer.Count)); tcs.TrySetResult(new FileIOResult((int)result, (int)result == 0)); } } ); InvokeHelper(action, messageLoop); } return(await tcs.Task); } catch (Exception exc) { Console.WriteLine(exc.Message); tcs.SetException(exc); return(new FileIOResult((int)PPError.Aborted, true)); } finally { HandleReadData -= handler; } }