public void RereaderTest() { var n = 10000; var q = new Queue <byte[]>(n); var r = new Rereader(); for (int i = 0; i < n; i++) { var b = RandBytes(100); using (var ms = new MemoryStream(b)) { r.Reread(ms, b.Length); } q.Enqueue(b); } for (var i = 0; i < n; i++) { var raw = q.Dequeue(); var b = new byte[raw.Length]; var offset = 0; var remind = raw.Length; while (remind > 0) { var size = rand.Next(remind + 1); if (size == 0) { continue; } r.Pull(b, offset, size); offset = offset + size; remind = remind - size; } Assert.True(BytesEquals(raw, b)); } }
public override int Read(byte[] buffer, int offset, int size) { _ReadLock.WaitOne(); _ReconnLock.AcquireReaderLock(-1); int n = 0; try { for (;;) { n = _Rereader.Pull(buffer, offset, size); if (n > 0) { return(n); } try { n = _BaseStream.Read(buffer, offset + n, size); if (n == 0) { if (!tryReconn()) { throw new IOException(); } continue; } } catch { if (!tryReconn()) { throw; } continue; } break; } } finally { if (n > 0 && _EnableCrypt) { _ReadCipher.XORKeyStream(buffer, offset, buffer, offset, n); } _ReadCount += (ulong)n; _ReconnLock.ReleaseReaderLock(); _ReadLock.ReleaseMutex(); } return(n); }