Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of ReadExactly & ReadExactlyAsync. #361

Merged
merged 1 commit into from
May 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Obsidian/Net/MinecraftStream.Reading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class MinecraftStream
public byte ReadUnsignedByte()
{
Span<byte> buffer = stackalloc byte[1];
BaseStream.Read(buffer);
BaseStream.ReadExactly(buffer);
return buffer[0];
}

Expand Down Expand Up @@ -53,7 +53,7 @@ public async Task<bool> ReadBooleanAsync()
public ushort ReadUnsignedShort()
{
Span<byte> buffer = stackalloc byte[2];
this.Read(buffer);
this.ReadExactly(buffer);
return BinaryPrimitives.ReadUInt16BigEndian(buffer);
}

Expand All @@ -68,89 +68,89 @@ public async Task<ushort> ReadUnsignedShortAsync()
public short ReadShort()
{
Span<byte> buffer = stackalloc byte[2];
this.Read(buffer);
this.ReadExactly(buffer);
return BinaryPrimitives.ReadInt16BigEndian(buffer);
}

public async Task<short> ReadShortAsync()
{
using var buffer = new RentedArray<byte>(sizeof(short));
await this.ReadAsync(buffer);
await this.ReadExactlyAsync(buffer);
return BinaryPrimitives.ReadInt16BigEndian(buffer);
}

[ReadMethod]
public int ReadInt()
{
Span<byte> buffer = stackalloc byte[4];
this.Read(buffer);
this.ReadExactly(buffer);
return BinaryPrimitives.ReadInt32BigEndian(buffer);
}

public async Task<int> ReadIntAsync()
{
using var buffer = new RentedArray<byte>(sizeof(int));
await this.ReadAsync(buffer);
await this.ReadExactlyAsync(buffer);
return BinaryPrimitives.ReadInt32BigEndian(buffer);
}

[ReadMethod]
public long ReadLong()
{
Span<byte> buffer = stackalloc byte[8];
this.Read(buffer);
this.ReadExactly(buffer);
return BinaryPrimitives.ReadInt64BigEndian(buffer);
}

public async Task<long> ReadLongAsync()
{
using var buffer = new RentedArray<byte>(sizeof(long));
await this.ReadAsync(buffer);
await this.ReadExactlyAsync(buffer);
return BinaryPrimitives.ReadInt64BigEndian(buffer);
}

[ReadMethod]
public ulong ReadUnsignedLong()
{
Span<byte> buffer = stackalloc byte[8];
this.Read(buffer);
this.ReadExactly(buffer);
return BinaryPrimitives.ReadUInt64BigEndian(buffer);
}

public async Task<ulong> ReadUnsignedLongAsync()
{
using var buffer = new RentedArray<byte>(sizeof(ulong));
await this.ReadAsync(buffer);
await this.ReadExactlyAsync(buffer);
return BinaryPrimitives.ReadUInt64BigEndian(buffer);
}

[ReadMethod]
public float ReadFloat()
{
Span<byte> buffer = stackalloc byte[4];
this.Read(buffer);
this.ReadExactly(buffer);
return BinaryPrimitives.ReadSingleBigEndian(buffer);
}

public async Task<float> ReadFloatAsync()
{
using var buffer = new RentedArray<byte>(sizeof(float));
await this.ReadAsync(buffer);
await this.ReadExactlyAsync(buffer);
return BinaryPrimitives.ReadSingleBigEndian(buffer);
}

[ReadMethod]
public double ReadDouble()
{
Span<byte> buffer = stackalloc byte[8];
this.Read(buffer);
this.ReadExactly(buffer);
return BinaryPrimitives.ReadDoubleBigEndian(buffer);
}

public async Task<double> ReadDoubleAsync()
{
using var buffer = new RentedArray<byte>(sizeof(double));
await this.ReadAsync(buffer);
await this.ReadExactlyAsync(buffer);
return BinaryPrimitives.ReadDoubleBigEndian(buffer);
}

Expand All @@ -159,7 +159,7 @@ public string ReadString(int maxLength = 32767)
{
var length = ReadVarInt();
var buffer = new byte[length];
this.Read(buffer, 0, length);
this.ReadExactly(buffer);

var value = Encoding.UTF8.GetString(buffer);
if (maxLength > 0 && value.Length > maxLength)
Expand All @@ -177,7 +177,7 @@ public async Task<string> ReadStringAsync(int maxLength = 32767)
{
buffer.Span.Reverse();
}
await this.ReadAsync(buffer);
await this.ReadExactlyAsync(buffer);

var value = Encoding.UTF8.GetString(buffer);
if (maxLength > 0 && value.Length > maxLength)
Expand Down
Loading