Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #46 from justcoding121/master
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
justcoding121 authored Apr 19, 2019
2 parents e7d203d + b8a6871 commit 5b797b8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/StreamExtended/Network/CopyStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public byte PeekByteFromBuffer(int index)
return reader.PeekByteAsync(index, cancellationToken);
}

public Task<byte[]> 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
Expand Down
11 changes: 11 additions & 0 deletions src/StreamExtended/Network/CustomBufferedPeekStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ byte ICustomStreamReader.PeekByteFromBuffer(int index)
return baseStream.PeekByteFromBuffer(index);
}

/// <summary>
/// Peeks bytes asynchronous.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<byte[]> ICustomStreamReader.PeekBytesAsync(int index, int size, CancellationToken cancellationToken)
{
return baseStream.PeekBytesAsync(index, size, cancellationToken);
}

/// <summary>
/// Peeks a byte asynchronous.
/// </summary>
Expand Down
30 changes: 28 additions & 2 deletions src/StreamExtended/Network/CustomBufferedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,30 @@ public override int ReadByte()
return streamBuffer[bufferPos + index];
}

/// <summary>
/// Peeks bytes asynchronous.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<byte[]> 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;
}

/// <summary>
/// Peeks a byte from buffer.
/// </summary>
Expand Down Expand Up @@ -432,14 +456,15 @@ public bool FillBuffer()
else
{
closed = true;
throw new IOException($"{nameof(CustomBufferedStream)} closed");
}

return result;
}
catch
{
closed = true;
return false;
throw;//rethrow
}
}

Expand Down Expand Up @@ -481,14 +506,15 @@ public bool FillBuffer()
else
{
closed = true;
throw new IOException($"{nameof(CustomBufferedStream)} closed");
}

return result;
}
catch
{
closed = true;
return false;
throw;//rethrow
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/StreamExtended/Network/ICustomStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public interface ICustomStreamReader
/// <returns></returns>
Task<int> PeekByteAsync(int index, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Peeks bytes asynchronous.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<byte[]> PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default(CancellationToken));

byte ReadByteFromBuffer();

/// <summary>
Expand Down

0 comments on commit 5b797b8

Please sign in to comment.