public void ResetShouldAllowStreamReuse() { int initialOffset = 0; var manager = new BufferManageMock(); using (var stream = new BufferManagerStream(manager, initialOffset, 4, 1024)) { stream.Write(_input, 0, 4); manager.ReturnBuffer(stream.GetArrayAndClear().Array); manager.AssertEverythingIsReturned(); initialOffset = 1; stream.Reset(manager, initialOffset, 4); Assert.AreEqual(stream.Position, 0, "Position should be 0 after reset"); byte[] otherInput = new byte[] { 3, 2, 1 }; stream.Write(otherInput, 0, otherInput.Length); var array = stream.GetArrayAndClear(); VerifyBufferContents(array, initialOffset, new ArraySegment <byte>(otherInput)); manager.ReturnBuffer(array.Array); manager.AssertEverythingIsReturned(); } }
private ArraySegment <byte> VerifyStreamContents(BufferManagerStream stream, int expectedOffset, int count, BufferManageMock manager, byte[] expectedContents = null) { Assert.AreEqual(count, stream.Position, "Stream position should equal count"); var buffer = stream.GetArrayAndClear(); VerifyBufferContents(buffer, expectedOffset, new ArraySegment <byte>(expectedContents ?? _input, 0, count)); // By returning buffer we also ensure that it was allocated through the buffer manager manager.ReturnBuffer(buffer.Array); manager.AssertEverythingIsReturned(); return(buffer); }