public override void Write(byte[] buffer, int offset, int size) { if (size == 0) { return; } _WriteLock.WaitOne(); _ReconnLock.AcquireReaderLock(-1); try { if (_EnableCrypt) { _WriteCipher.XORKeyStream(buffer, offset, buffer, offset, size); } _Rewriter.Push(buffer, offset, size); _WriterCount += (ulong)size; try { _BaseStream.Write(buffer, offset, size); } catch { if (!tryReconn()) { throw; } } } finally { _ReconnLock.ReleaseReaderLock(); _WriteLock.ReleaseMutex(); } }
public void RewriterTest() { ulong writeCount = 0; ulong readCount = 0; var w = new Rewriter(100); for (var i = 0; i < 100000; i++) { var a = RandBytes(100); var b = new byte[a.Length]; w.Push(a, 0, a.Length); writeCount += (ulong)a.Length; var remind = a.Length; var offset = 0; while (remind > 0) { var size = rand.Next(remind) + 1; using (MemoryStream ms = new MemoryStream(b, offset, b.Length - offset)) { Assert.True(w.Rewrite(ms, writeCount, readCount)); } readCount += (ulong)size; offset += size; remind -= size; } Assert.True(BytesEquals(a, b)); } }