服务器ByteBuf消除警告

服务器ByteBuf消除警告
This commit is contained in:
ALEXTANGXIAO
2023-09-05 23:57:12 +08:00
parent caf5b2b54e
commit 14e95107c9

View File

@@ -40,6 +40,7 @@ namespace Bright.Serialization
public sealed class ByteBuf : ICloneable, IEquatable<ByteBuf> public sealed class ByteBuf : ICloneable, IEquatable<ByteBuf>
{ {
#pragma warning disable CS8618
public ByteBuf() public ByteBuf()
{ {
Bytes = Array.Empty<byte>(); Bytes = Array.Empty<byte>();
@@ -76,6 +77,7 @@ namespace Bright.Serialization
{ {
return new ByteBuf(bytes, 0, bytes.Length); return new ByteBuf(bytes, 0, bytes.Length);
} }
#pragma warning restore CS8618
public void Replace(byte[] bytes) public void Replace(byte[] bytes)
{ {
@@ -1015,20 +1017,24 @@ namespace Bright.Serialization
return ((long)((ulong)x >> 1) ^ ((x & 1) << 63)); return ((long)((ulong)x >> 1) ^ ((x & 1) << 63));
} }
public void WriteString(string x) public void WriteString(string? x)
{ {
var n = x != null ? Encoding.UTF8.GetByteCount(x) : 0; var n = x != null ? Encoding.UTF8.GetByteCount(x) : 0;
WriteSize(n); WriteSize(n);
if (n > 0) if (n > 0)
{ {
EnsureWrite(n); EnsureWrite(n);
Encoding.UTF8.GetBytes(x, 0, x.Length, Bytes, WriterIndex); if (x != null)
{
Encoding.UTF8.GetBytes(x, 0, x.Length, this.Bytes, this.WriterIndex);
}
WriterIndex += n; WriterIndex += n;
} }
} }
// byte[], [start, end) // byte[], [start, end)
public static Func<byte[], int, int, string> StringCacheFinder { get; set; } public static Func<byte[], int, int, string>? StringCacheFinder { get; set; }
public string ReadString() public string ReadString()
{ {
@@ -1056,14 +1062,14 @@ namespace Bright.Serialization
} }
} }
public void WriteBytes(byte[] x) public void WriteBytes(byte[]? x)
{ {
var n = x != null ? x.Length : 0; var n = x != null ? x.Length : 0;
WriteSize(n); WriteSize(n);
if (n > 0) if (n > 0)
{ {
EnsureWrite(n); EnsureWrite(n);
x.CopyTo(Bytes, WriterIndex); x?.CopyTo(Bytes, WriterIndex);
WriterIndex += n; WriterIndex += n;
} }
} }
@@ -1485,12 +1491,12 @@ namespace Bright.Serialization
return string.Join(".", datas); return string.Join(".", datas);
} }
public override bool Equals(object obj) public override bool Equals(object? obj)
{ {
return (obj is ByteBuf other) && Equals(other); return (obj is ByteBuf other) && Equals(other);
} }
public bool Equals(ByteBuf other) public bool Equals(ByteBuf? other)
{ {
if (other == null) if (other == null)
{ {