Skip to content

Commit 0217082

Browse files
committed
по замечаниям: контроль null, отрицательный счетчик, закрытие потока
1 parent 1a2ad44 commit 0217082

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

src/OneScript.StandardLibrary/Hash/HashImpl.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public HashImpl(IncrementalHash provider, HashFunctionEnum enumValue)
3434
_provider = provider;
3535
_enumValue = enumValue;
3636
_calculated = false;
37-
if (enumValue == HashFunctionEnum.CRC32 || provider==null)
37+
if (enumValue == HashFunctionEnum.CRC32)
3838
{
3939
_crc32 = new Crc32();
4040
}
41+
else ArgumentNullException.ThrowIfNull(provider);
4142
}
4243

4344
[ContextProperty("ХешФункция", "HashFunction")]
@@ -63,6 +64,9 @@ public string HashString
6364
{
6465
get
6566
{
67+
if (!_calculated)
68+
return "0";
69+
6670
if (_crc32 != null)
6771
return _crc32.GetCurrentHashAsUInt32().ToString("X8");
6872

@@ -132,10 +136,10 @@ public void Append(BslValue toAdd, int count = 0)
132136
break;
133137
case IStreamWrapper wrapper:
134138
var stream = wrapper.GetUnderlyingStream();
135-
if (count == 0)
139+
if (count <= 0)
136140
AppendStream(stream);
137141
else
138-
AppendStream(stream, count);
142+
AppendStream(stream, count);
139143
break;
140144
case BinaryDataContext binaryData:
141145
AppendStream(binaryData.GetStream());
@@ -149,10 +153,10 @@ public void Append(BslValue toAdd, int count = 0)
149153
public void AppendFile(string path)
150154
{
151155
if (!File.Exists(path))
152-
throw RuntimeException.InvalidArgumentType();
153-
var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
154-
AppendStream(stream);
155-
stream.Close();
156+
throw RuntimeException.InvalidArgumentType();
157+
158+
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
159+
AppendStream(stream);
156160
}
157161

158162
[ScriptConstructor(Name = "По указанной хеш-функции")]

0 commit comments

Comments
 (0)