diff --git a/src/StreamExtended/Network/CopyStream.cs b/src/StreamExtended/Network/CopyStream.cs index 33a3291..197f887 100644 --- a/src/StreamExtended/Network/CopyStream.cs +++ b/src/StreamExtended/Network/CopyStream.cs @@ -54,6 +54,11 @@ public byte PeekByteFromBuffer(int index) return reader.PeekByteAsync(index, cancellationToken); } + public Task PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default(CancellationToken)) + { + return reader.PeekBytesAsync(index, size, cancellationToken); + } + public void Flush() { //send out the current data from from the buffer diff --git a/src/StreamExtended/Network/CustomBufferedPeekStream.cs b/src/StreamExtended/Network/CustomBufferedPeekStream.cs index 01f4cb7..ab3d862 100644 --- a/src/StreamExtended/Network/CustomBufferedPeekStream.cs +++ b/src/StreamExtended/Network/CustomBufferedPeekStream.cs @@ -85,6 +85,17 @@ byte ICustomStreamReader.PeekByteFromBuffer(int index) return baseStream.PeekByteFromBuffer(index); } + /// + /// Peeks bytes asynchronous. + /// + /// The index. + /// The cancellation token. + /// + Task ICustomStreamReader.PeekBytesAsync(int index, int size, CancellationToken cancellationToken) + { + return baseStream.PeekBytesAsync(index, size, cancellationToken); + } + /// /// Peeks a byte asynchronous. /// diff --git a/src/StreamExtended/Network/CustomBufferedStream.cs b/src/StreamExtended/Network/CustomBufferedStream.cs index c81659c..9db04b9 100644 --- a/src/StreamExtended/Network/CustomBufferedStream.cs +++ b/src/StreamExtended/Network/CustomBufferedStream.cs @@ -240,6 +240,30 @@ public override int ReadByte() return streamBuffer[bufferPos + index]; } + /// + /// Peeks bytes asynchronous. + /// + /// The index. + /// The cancellation token. + /// + public async Task PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default(CancellationToken)) + { + + if (Available <= index) + { + await FillBufferAsync(cancellationToken); + } + + if (Available <= (index + size)) + { + return null; + } + + var vRet = new byte[size]; + Array.Copy(streamBuffer, index, vRet, 0, size); + return vRet; + } + /// /// Peeks a byte from buffer. /// @@ -432,6 +456,7 @@ public bool FillBuffer() else { closed = true; + throw new IOException($"{nameof(CustomBufferedStream)} closed"); } return result; @@ -439,7 +464,7 @@ public bool FillBuffer() catch { closed = true; - return false; + throw;//rethrow } } @@ -481,6 +506,7 @@ public bool FillBuffer() else { closed = true; + throw new IOException($"{nameof(CustomBufferedStream)} closed"); } return result; @@ -488,7 +514,7 @@ public bool FillBuffer() catch { closed = true; - return false; + throw;//rethrow } } diff --git a/src/StreamExtended/Network/ICustomStreamReader.cs b/src/StreamExtended/Network/ICustomStreamReader.cs index 058dc01..89d3278 100644 --- a/src/StreamExtended/Network/ICustomStreamReader.cs +++ b/src/StreamExtended/Network/ICustomStreamReader.cs @@ -37,6 +37,14 @@ public interface ICustomStreamReader /// Task PeekByteAsync(int index, CancellationToken cancellationToken = default(CancellationToken)); + /// + /// Peeks bytes asynchronous. + /// + /// The index. + /// The cancellation token. + /// + Task PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default(CancellationToken)); + byte ReadByteFromBuffer(); ///