Skip to content

Commit

Permalink
Recode
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoFeiDu committed Dec 6, 2021
1 parent 982df15 commit 06592f9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/Zaabee.Extensions/Zaabee.Extensions.Bytes.Stream.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public static async Task<MemoryStream> TryToStreamAsync(this byte[] buffer,

public static async Task WriteToAsync(this byte[] buffer, Stream stream,
CancellationToken cancellationToken = default) =>
#if NETSTANDARD2_0
await stream.WriteAsync(buffer, 0, buffer.Length, cancellationToken);
#else
await stream.WriteAsync(buffer, cancellationToken);
#endif

public static Task<bool> TryWriteToAsync(this byte[] buffer, Stream stream,
CancellationToken cancellationToken = default) =>
Expand Down
4 changes: 4 additions & 0 deletions src/Zaabee.Extensions/Zaabee.Extensions.Bytes.Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public static MemoryStream TryToStream(this byte[] buffer)
}

public static void WriteTo(this byte[] buffer, Stream stream) =>
#if NETSTANDARD2_0
stream.Write(buffer, 0, buffer.Length);
#else
stream.Write(buffer);
#endif

public static bool TryWriteTo(this byte[] buffer, Stream stream) =>
stream.TryWrite(buffer, 0, buffer.Length);
Expand Down
16 changes: 13 additions & 3 deletions src/Zaabee.Extensions/Zaabee.Extensions.Stream.Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ namespace Zaabee.Extensions;
public static partial class ZaabeeExtension
{
public static int TryRead(this Stream? stream, byte[] buffer) =>
stream.TryRead(buffer, 0, buffer.Length);
stream is not null && stream.CanRead
#if NETSTANDARD2_0
? stream.Read(buffer, 0, buffer.Length)
#else
? stream.Read(buffer)
#endif
: default;

public static int TryRead(this Stream? stream, byte[] buffer, int offset, int count) =>
stream is not null && stream.CanRead ? stream.Read(buffer, offset, count) : default;
stream is not null && stream.CanRead
? stream.Read(buffer, offset, count)
: default;

public static int TryReadByte(this Stream? stream) =>
stream is not null && stream.CanRead ? stream.ReadByte() : -1;
stream is not null && stream.CanRead
? stream.ReadByte()
: -1;

public static byte[] ReadToEnd(this Stream? stream)
{
Expand Down
13 changes: 11 additions & 2 deletions src/Zaabee.Extensions/Zaabee.Extensions.Stream.Write.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ namespace Zaabee.Extensions;
public static partial class ZaabeeExtension
{
public static async Task<bool> TryWriteAsync(this Stream? stream, byte[] buffer,
CancellationToken cancellationToken = default) =>
await stream.TryWriteAsync(buffer, 0, buffer.Length, cancellationToken);
CancellationToken cancellationToken = default)
{
var canWrite = stream is not null && stream.CanWrite;
if (canWrite)
#if NETSTANDARD2_0
await stream!.WriteAsync(buffer, 0, buffer.Length, cancellationToken);
#else
await stream!.WriteAsync(buffer, cancellationToken);
#endif
return canWrite;
}

public static async Task<bool> TryWriteAsync(this Stream? stream, byte[] buffer, int offset, int count,
CancellationToken cancellationToken = default)
Expand Down
13 changes: 11 additions & 2 deletions src/Zaabee.Extensions/Zaabee.Extensions.Stream.Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ namespace Zaabee.Extensions;

public static partial class ZaabeeExtension
{
public static bool TryWrite(this Stream? stream, byte[] buffer) =>
stream.TryWrite(buffer, 0, buffer.Length);
public static bool TryWrite(this Stream? stream, byte[] buffer)
{
var canWrite = stream is not null && stream.CanWrite;
if (canWrite)
#if NETSTANDARD2_0
stream!.Write(buffer, 0, buffer.Length);
#else
stream!.Write(buffer);
#endif
return canWrite;
}

public static bool TryWrite(this Stream? stream, byte[] buffer, int offset, int count)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Zaabee.Extensions/Zaabee.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2022.1.0</PackageVersion>
<Version>2022.1.0</Version>
<PackageVersion>2022.2.0</PackageVersion>
<Version>2022.2.0</Version>
<Authors>Mutuduxf</Authors>
<Company>Mutuduxf</Company>
<PackageTags>Zaabee;Extensions</PackageTags>
Expand Down
4 changes: 3 additions & 1 deletion tests/Zaabee.Extensions.UnitTest/StreamExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public void TryReadWriteTest()
var ms = new MemoryStream();
var bytes = new byte[1024];
var result = new byte[1024];
for (var i = 0; i < bytes.Length; i++) bytes[i] = (byte) (i % (byte.MaxValue + 1));
for (var i = 0; i < bytes.Length; i++) bytes[i] = (byte)(i % (byte.MaxValue + 1));
Assert.True(ms.TryWrite(bytes));
Assert.Equal(0, ms.TrySeek(0, SeekOrigin.Begin));
Assert.Equal(1024, ms.TryRead(result));
Assert.Equal(0, ms.TrySeek(0, SeekOrigin.Begin));
Assert.Equal(1024, ms.TryRead(result, 0, result.Length));
Assert.True(BytesEqual(bytes, result));
}

Expand Down

0 comments on commit 06592f9

Please sign in to comment.