diff --git a/src/neo/IO/Helper.cs b/src/neo/IO/Helper.cs index e44e5bbe0a..ab8b2ce404 100644 --- a/src/neo/IO/Helper.cs +++ b/src/neo/IO/Helper.cs @@ -12,8 +12,6 @@ namespace Neo.IO { public static class Helper { - public const int GroupingSizeInBytes = 16; - public static T AsSerializable(this byte[] value, int start = 0) where T : ISerializable, new() { using (MemoryStream ms = new MemoryStream(value, start, value.Length - start, false)) @@ -144,24 +142,6 @@ public static int GetVarSize(this string value) return GetVarSize(size) + size; } - public static byte[] ReadBytesWithGrouping(this BinaryReader reader) - { - using (MemoryStream ms = new MemoryStream()) - { - int count; - do - { - byte[] group = reader.ReadFixedBytes(GroupingSizeInBytes); - count = reader.ReadByte(); - if (count > GroupingSizeInBytes) - throw new FormatException(); - if (count > 0) - ms.Write(group, 0, count); - } while (count == GroupingSizeInBytes); - return ms.ToArray(); - } - } - public static byte[] ReadFixedBytes(this BinaryReader reader, int size) { var index = 0; @@ -277,25 +257,6 @@ public static void Write(this BinaryWriter writer, IReadOnlyCollection val } } - public static void WriteBytesWithGrouping(this BinaryWriter writer, byte[] value) - { - int index = 0; - int remain = value.Length; - while (remain >= GroupingSizeInBytes) - { - writer.Write(value, index, GroupingSizeInBytes); - writer.Write((byte)GroupingSizeInBytes); - index += GroupingSizeInBytes; - remain -= GroupingSizeInBytes; - } - if (remain > 0) - writer.Write(value, index, remain); - int padding = GroupingSizeInBytes - remain; - for (int i = 0; i < padding; i++) - writer.Write((byte)0); - writer.Write((byte)remain); - } - public static void WriteFixedString(this BinaryWriter writer, string value, int length) { if (value == null) diff --git a/src/neo/Ledger/StorageKey.cs b/src/neo/Ledger/StorageKey.cs index af7258f2ce..bf1e3c447b 100644 --- a/src/neo/Ledger/StorageKey.cs +++ b/src/neo/Ledger/StorageKey.cs @@ -1,6 +1,7 @@ using Neo.Cryptography; using Neo.IO; using System; +using System.Buffers.Binary; using System.IO; namespace Neo.Ledger @@ -10,31 +11,22 @@ public class StorageKey : IEquatable, ISerializable public int Id; public byte[] Key; - int ISerializable.Size => sizeof(int) + (Key.Length / 16 + 1) * 17; + int ISerializable.Size => sizeof(int) + Key.Length; - internal static byte[] CreateSearchPrefix(int id, byte[] prefix) + internal static byte[] CreateSearchPrefix(int id, ReadOnlySpan prefix) { - using (MemoryStream ms = new MemoryStream()) - { - int index = 0; - int remain = prefix.Length; - while (remain >= 16) - { - ms.Write(prefix, index, 16); - ms.WriteByte(16); - index += 16; - remain -= 16; - } - if (remain > 0) - ms.Write(prefix, index, remain); - return Helper.Concat(BitConverter.GetBytes(id), ms.ToArray()); - } + byte[] buffer = new byte[sizeof(int) + prefix.Length]; + BinaryPrimitives.WriteInt32LittleEndian(buffer, id); + prefix.CopyTo(buffer.AsSpan(sizeof(int))); + return buffer; } + //If the base stream of the reader doesn't support seeking, a NotSupportedException is thrown. + //But StorageKey never works with NetworkStream, so it doesn't matter. void ISerializable.Deserialize(BinaryReader reader) { Id = reader.ReadInt32(); - Key = reader.ReadBytesWithGrouping(); + Key = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); } public bool Equals(StorageKey other) @@ -60,7 +52,7 @@ public override int GetHashCode() void ISerializable.Serialize(BinaryWriter writer) { writer.Write(Id); - writer.WriteBytesWithGrouping(Key); + writer.Write(Key); } } } diff --git a/tests/neo.UnitTests/IO/UT_IOHelper.cs b/tests/neo.UnitTests/IO/UT_IOHelper.cs index 3b36695b2d..1128270716 100644 --- a/tests/neo.UnitTests/IO/UT_IOHelper.cs +++ b/tests/neo.UnitTests/IO/UT_IOHelper.cs @@ -285,45 +285,6 @@ public void TestGetVarSizeString() Assert.AreEqual(3, result); } - [TestMethod] - public void TestReadBytesWithGrouping() - { - for (int i = 0; i < 2; i++) - { - if (i == 0) - { - byte[] caseArray = new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA}; - MemoryStream stream = new MemoryStream(); - BinaryWriter writer = new BinaryWriter(stream); - Neo.IO.Helper.WriteBytesWithGrouping(writer, caseArray); - stream.Seek(0, SeekOrigin.Begin); - BinaryReader reader = new BinaryReader(stream); - byte[] result = Neo.IO.Helper.ReadBytesWithGrouping(reader); - Assert.AreEqual(Encoding.Default.GetString(caseArray), Encoding.Default.GetString(result)); - } - else - { - byte[] caseArray = new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,0x10, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,0x10, - 0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x11}; - MemoryStream stream = new MemoryStream(); - BinaryWriter writer = new BinaryWriter(stream); - writer.Write(caseArray); - stream.Seek(0, SeekOrigin.Begin); - BinaryReader reader = new BinaryReader(stream); - Action action = () => Neo.IO.Helper.ReadBytesWithGrouping(reader); - action.Should().Throw(); - } - } - } - [TestMethod] public void TestReadFixedString() { @@ -474,28 +435,6 @@ public void TestWriteGeneric() 0x00,0x00,0x00,0x00,0x00}), Encoding.Default.GetString(byteArray)); } - - [TestMethod] - public void TestWriteBytesWithGrouping() - { - MemoryStream stream = new MemoryStream(); - BinaryWriter writer = new BinaryWriter(stream); - Neo.IO.Helper.WriteBytesWithGrouping(writer, new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA}); - stream.Seek(0, SeekOrigin.Begin); - byte[] byteArray = new byte[stream.Length]; - stream.Read(byteArray, 0, (int)stream.Length); - Assert.AreEqual(Encoding.Default.GetString(new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,0x10, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, - 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,0x10, - 0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x04}), Encoding.Default.GetString(byteArray)); - } - [TestMethod] public void TestWriteFixedString() {