-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMurmurOutputStream.cs
65 lines (55 loc) · 2.37 KB
/
MurmurOutputStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.IO;
using System.Security.Cryptography;
namespace Murmur
{
/// <summary>
/// Exposes the murmur algorithm as a pass through stream that computes the hash incrementally.
/// </summary>
public class MurmurOutputStream : Stream
{
static readonly byte[] DEFAULT_FINAL_TRANFORM = new byte[0];
readonly Stream UnderlyingStream;
readonly HashAlgorithm Algorithm;
public MurmurOutputStream(Stream underlyingStream, uint seed = 0, bool managed = true, AlgorithmType type = AlgorithmType.Murmur128, AlgorithmPreference preference = AlgorithmPreference.Auto)
{
UnderlyingStream = underlyingStream;
Algorithm = type == AlgorithmType.Murmur32
? (HashAlgorithm)MurmurHash.Create32(seed, managed)
: (HashAlgorithm)MurmurHash.Create128(seed, managed, preference);
}
public byte[] Hash { get { Algorithm.TransformFinalBlock(DEFAULT_FINAL_TRANFORM, 0, 0); return Algorithm.Hash; } }
public override bool CanRead { get { return false; } }
public override bool CanSeek { get { return false; } }
public override bool CanWrite { get { return true; } }
public override long Length { get { return UnderlyingStream.Length; } }
public override long Position { get { return UnderlyingStream.Position; } set { throw new NotSupportedException(); } }
public override void Flush()
{
UnderlyingStream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException("This stream does not support reading.");
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException("This stream does not support seeking, it is forward-only.");
}
public override void SetLength(long value)
{
UnderlyingStream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
Algorithm.TransformBlock(buffer, offset, count, null, 0);
UnderlyingStream.Write(buffer, offset, count);
}
protected override void Dispose(bool disposing)
{
if (disposing)
Algorithm.Dispose();
base.Dispose(disposing);
}
}
}