{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/comments/44620778","id":44620778,"diff_hunk":"@@ -0,0 +1,682 @@\n+// Copyright (c) Microsoft. All rights reserved.\n+// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n+\n+using System.Runtime.InteropServices;\n+using System.Diagnostics.Contracts;\n+\n+namespace System.IO\n+{\n+    /// <summary>\n+    /// One of the design goals here is to prevent the buffer from getting in the way and slowing\n+    /// down underlying stream accesses when it is not needed. If you always read & write for sizes\n+    /// greater than the internal buffer size, then this class may not even allocate the internal buffer.\n+    /// See a large comment in Write for the details of the write buffer heuristic.\n+    /// \n+    /// This class buffers reads & writes in a shared buffer.\n+    /// (If you maintained two buffers separately, one operation would always trash the other buffer\n+    /// anyways, so we might as well use one buffer.) \n+    /// The assumption here is you will almost always be doing a series of reads or writes, but rarely\n+    /// alternate between the two of them on the same stream.\n+    ///\n+    /// Class Invariants:\n+    /// The class has one buffer, shared for reading & writing.\n+    /// It can only be used for one or the other at any point in time - not both.\n+    /// The following should be true:\n+    /// <![CDATA[\n+    ///   * 0 <= _readPos <= _readLen < _bufferSize\n+    ///   * 0 <= _writePos < _bufferSize\n+    ///   * _readPos == _readLen && _readPos > 0 implies the read buffer is valid, but we're at the end of the buffer.\n+    ///   * _readPos == _readLen == 0 means the read buffer contains garbage.\n+    ///   * Either _writePos can be greater than 0, or _readLen & _readPos can be greater than zero,\n+    ///     but neither can be greater than zero at the same time.\n+    ///  ]]>\n+    /// This class will never cache more bytes than the max specified buffer size.\n+    /// However, it may use a temporary buffer of up to twice the size in order to combine several IO operations on\n+    /// the underlying stream into a single operation. This is because we assume that memory copies are significantly\n+    /// faster than IO operations on the underlying stream (if this was not true, using buffering is never appropriate).\n+    /// The max size of this \"shadow\" buffer is limited as to not allocate it on the LOH.\n+    /// Shadowing is always transient. Even when using this technique, this class still guarantees that the number of\n+    /// bytes cached (not yet written to the target stream or not yet consumed by the user) is never larger than the \n+    /// actual specified buffer size.\n+    /// </summary>\n+    [ComVisible(true)]\n+    public sealed partial class BufferedStream : Stream\n+    {\n+        private const Int32 _DefaultBufferSize = 4096;\n+\n+\n+        private Stream _stream;                               // Underlying stream.  Close sets _stream to null.\n+\n+        private Byte[] _buffer;                               // Shared read/write buffer.  Alloc on first use.\n+\n+        private readonly Int32 _bufferSize;                   // Length of internal buffer (not counting the shadow buffer).\n+\n+        private Int32 _readPos;                               // Read pointer within shared buffer.\n+        private Int32 _readLen;                               // Number of bytes read in buffer from _stream.\n+        private Int32 _writePos;                              // Write pointer within shared buffer.\n+\n+        /// <summary><code>MaxShadowBufferSize</code> is chosed such that shadow buffers are not allocated on the Large Object Heap.\n+        /// Currently, an object is allocated on the LOH if it is larger than 85000 bytes. See LARGE_OBJECT_SIZE in ndp\\clr\\src\\vm\\gc.h\n+        /// We will go with exactly 80 KBytes, although this is somewhat arbitrary.</summary>\n+        private const Int32 MaxShadowBufferSize = 81920;  // Make sure not to get to the Large Object Heap.","path":"src/System.IO/src/System/IO/BufferedStream.cs","position":61,"original_position":61,"commit_id":"01c0823c1548740c8efbb4002b61663a9e7cec12","original_commit_id":"01c0823c1548740c8efbb4002b61663a9e7cec12","user":{"login":"stephentoub","id":2642209,"avatar_url":"https://avatars.githubusercontent.com/u/2642209?v=3","gravatar_id":"","url":"https://api.github.com/users/stephentoub","html_url":"https://github.com/stephentoub","followers_url":"https://api.github.com/users/stephentoub/followers","following_url":"https://api.github.com/users/stephentoub/following{/other_user}","gists_url":"https://api.github.com/users/stephentoub/gists{/gist_id}","starred_url":"https://api.github.com/users/stephentoub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stephentoub/subscriptions","organizations_url":"https://api.github.com/users/stephentoub/orgs","repos_url":"https://api.github.com/users/stephentoub/repos","events_url":"https://api.github.com/users/stephentoub/events{/privacy}","received_events_url":"https://api.github.com/users/stephentoub/received_events","type":"User","site_admin":false},"body":"Nit: Consider moving this const up to be with the other const","created_at":"2015-11-12T04:31:54Z","updated_at":"2015-11-12T04:31:54Z","html_url":"https://github.com/dotnet/corefx/pull/4456#discussion_r44620778","pull_request_url":"https://api.github.com/repos/dotnet/corefx/pulls/4456","_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments/44620778"},"html":{"href":"https://github.com/dotnet/corefx/pull/4456#discussion_r44620778"},"pull_request":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/4456"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/4456","id":50452560,"html_url":"https://github.com/dotnet/corefx/pull/4456","diff_url":"https://github.com/dotnet/corefx/pull/4456.diff","patch_url":"https://github.com/dotnet/corefx/pull/4456.patch","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/4456","number":4456,"state":"open","locked":false,"title":"Add BufferedStream to CoreFX","user":{"login":"ianhays","id":2606879,"avatar_url":"https://avatars.githubusercontent.com/u/2606879?v=3","gravatar_id":"","url":"https://api.github.com/users/ianhays","html_url":"https://github.com/ianhays","followers_url":"https://api.github.com/users/ianhays/followers","following_url":"https://api.github.com/users/ianhays/following{/other_user}","gists_url":"https://api.github.com/users/ianhays/gists{/gist_id}","starred_url":"https://api.github.com/users/ianhays/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ianhays/subscriptions","organizations_url":"https://api.github.com/users/ianhays/orgs","repos_url":"https://api.github.com/users/ianhays/repos","events_url":"https://api.github.com/users/ianhays/events{/privacy}","received_events_url":"https://api.github.com/users/ianhays/received_events","type":"User","site_admin":false},"body":"Adds source and tests to CoreFX for System.IO.BufferedStream.\r\n\r\nTests: I modified the existing System.IO tests that used MemoryStreams to instead be inheritable. I made a FakeStream that is an indirection around MemoryStream that enables BufferedStream(new FakeStream) to be used interchangeably with MemoryStream(). Though wrapping a MemoryStream with a BufferedStream isn't useful in the real world, it's an adequate method of ensuring the BufferedStream is calling the underlying stream functions correctly. Coverage is 89%/80% for BufferedStream with these additions.\r\n\r\nresolves #1793\r\n\r\nThe tests will fail CI as they will be compiled against the contract pulled in from nuget for System.IO version 4.0.10. For testing, I manually modified the reference assemblies to those produced by my System.IO/src build and all tests pass. I expect I need to do something on the TFS side of things to resolve this issue. \r\n\r\nThe N custom implementation in \\ndp\\FxCore\\src\\System.Runtime.WindowsRuntime\\System\\IO can also be removed in favor of this one.\r\n\r\n@stephentoub @mellinoe @manu-silicon","created_at":"2015-11-11T23:36:13Z","updated_at":"2015-11-12T04:31:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d2ef0817deb2a9ac1ba336f87f636241a4e96df4","assignee":{"login":"ianhays","id":2606879,"avatar_url":"https://avatars.githubusercontent.com/u/2606879?v=3","gravatar_id":"","url":"https://api.github.com/users/ianhays","html_url":"https://github.com/ianhays","followers_url":"https://api.github.com/users/ianhays/followers","following_url":"https://api.github.com/users/ianhays/following{/other_user}","gists_url":"https://api.github.com/users/ianhays/gists{/gist_id}","starred_url":"https://api.github.com/users/ianhays/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ianhays/subscriptions","organizations_url":"https://api.github.com/users/ianhays/orgs","repos_url":"https://api.github.com/users/ianhays/repos","events_url":"https://api.github.com/users/ianhays/events{/privacy}","received_events_url":"https://api.github.com/users/ianhays/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/dotnet/corefx/milestones/6","html_url":"https://github.com/dotnet/corefx/milestones/1.0.0-rc2","labels_url":"https://api.github.com/repos/dotnet/corefx/milestones/6/labels","id":1365455,"number":6,"title":"1.0.0-rc2","description":"","creator":{"login":"joshfree","id":1302850,"avatar_url":"https://avatars.githubusercontent.com/u/1302850?v=3","gravatar_id":"","url":"https://api.github.com/users/joshfree","html_url":"https://github.com/joshfree","followers_url":"https://api.github.com/users/joshfree/followers","following_url":"https://api.github.com/users/joshfree/following{/other_user}","gists_url":"https://api.github.com/users/joshfree/gists{/gist_id}","starred_url":"https://api.github.com/users/joshfree/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshfree/subscriptions","organizations_url":"https://api.github.com/users/joshfree/orgs","repos_url":"https://api.github.com/users/joshfree/repos","events_url":"https://api.github.com/users/joshfree/events{/privacy}","received_events_url":"https://api.github.com/users/joshfree/received_events","type":"User","site_admin":false},"open_issues":112,"closed_issues":59,"state":"open","created_at":"2015-10-20T04:29:15Z","updated_at":"2015-11-12T00:51:01Z","due_on":"2016-02-03T08:00:00Z","closed_at":null},"commits_url":"https://api.github.com/repos/dotnet/corefx/pulls/4456/commits","review_comments_url":"https://api.github.com/repos/dotnet/corefx/pulls/4456/comments","review_comment_url":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/4456/comments","statuses_url":"https://api.github.com/repos/dotnet/corefx/statuses/01c0823c1548740c8efbb4002b61663a9e7cec12","head":{"label":"ianhays:buffered","ref":"buffered","sha":"01c0823c1548740c8efbb4002b61663a9e7cec12","user":{"login":"ianhays","id":2606879,"avatar_url":"https://avatars.githubusercontent.com/u/2606879?v=3","gravatar_id":"","url":"https://api.github.com/users/ianhays","html_url":"https://github.com/ianhays","followers_url":"https://api.github.com/users/ianhays/followers","following_url":"https://api.github.com/users/ianhays/following{/other_user}","gists_url":"https://api.github.com/users/ianhays/gists{/gist_id}","starred_url":"https://api.github.com/users/ianhays/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ianhays/subscriptions","organizations_url":"https://api.github.com/users/ianhays/orgs","repos_url":"https://api.github.com/users/ianhays/repos","events_url":"https://api.github.com/users/ianhays/events{/privacy}","received_events_url":"https://api.github.com/users/ianhays/received_events","type":"User","site_admin":false},"repo":{"id":37934303,"name":"corefx","full_name":"ianhays/corefx","owner":{"login":"ianhays","id":2606879,"avatar_url":"https://avatars.githubusercontent.com/u/2606879?v=3","gravatar_id":"","url":"https://api.github.com/users/ianhays","html_url":"https://github.com/ianhays","followers_url":"https://api.github.com/users/ianhays/followers","following_url":"https://api.github.com/users/ianhays/following{/other_user}","gists_url":"https://api.github.com/users/ianhays/gists{/gist_id}","starred_url":"https://api.github.com/users/ianhays/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ianhays/subscriptions","organizations_url":"https://api.github.com/users/ianhays/orgs","repos_url":"https://api.github.com/users/ianhays/repos","events_url":"https://api.github.com/users/ianhays/events{/privacy}","received_events_url":"https://api.github.com/users/ianhays/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ianhays/corefx","description":"This repo contains the .NET Core foundational libraries, called CoreFX. It includes classes for collections, file systems, console, XML, async and many others.","fork":true,"url":"https://api.github.com/repos/ianhays/corefx","forks_url":"https://api.github.com/repos/ianhays/corefx/forks","keys_url":"https://api.github.com/repos/ianhays/corefx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ianhays/corefx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ianhays/corefx/teams","hooks_url":"https://api.github.com/repos/ianhays/corefx/hooks","issue_events_url":"https://api.github.com/repos/ianhays/corefx/issues/events{/number}","events_url":"https://api.github.com/repos/ianhays/corefx/events","assignees_url":"https://api.github.com/repos/ianhays/corefx/assignees{/user}","branches_url":"https://api.github.com/repos/ianhays/corefx/branches{/branch}","tags_url":"https://api.github.com/repos/ianhays/corefx/tags","blobs_url":"https://api.github.com/repos/ianhays/corefx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ianhays/corefx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ianhays/corefx/git/refs{/sha}","trees_url":"https://api.github.com/repos/ianhays/corefx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ianhays/corefx/statuses/{sha}","languages_url":"https://api.github.com/repos/ianhays/corefx/languages","stargazers_url":"https://api.github.com/repos/ianhays/corefx/stargazers","contributors_url":"https://api.github.com/repos/ianhays/corefx/contributors","subscribers_url":"https://api.github.com/repos/ianhays/corefx/subscribers","subscription_url":"https://api.github.com/repos/ianhays/corefx/subscription","commits_url":"https://api.github.com/repos/ianhays/corefx/commits{/sha}","git_commits_url":"https://api.github.com/repos/ianhays/corefx/git/commits{/sha}","comments_url":"https://api.github.com/repos/ianhays/corefx/comments{/number}","issue_comment_url":"https://api.github.com/repos/ianhays/corefx/issues/comments{/number}","contents_url":"https://api.github.com/repos/ianhays/corefx/contents/{+path}","compare_url":"https://api.github.com/repos/ianhays/corefx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ianhays/corefx/merges","archive_url":"https://api.github.com/repos/ianhays/corefx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ianhays/corefx/downloads","issues_url":"https://api.github.com/repos/ianhays/corefx/issues{/number}","pulls_url":"https://api.github.com/repos/ianhays/corefx/pulls{/number}","milestones_url":"https://api.github.com/repos/ianhays/corefx/milestones{/number}","notifications_url":"https://api.github.com/repos/ianhays/corefx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ianhays/corefx/labels{/name}","releases_url":"https://api.github.com/repos/ianhays/corefx/releases{/id}","created_at":"2015-06-23T17:36:59Z","updated_at":"2015-06-23T17:37:18Z","pushed_at":"2015-11-12T00:36:58Z","git_url":"git://github.com/ianhays/corefx.git","ssh_url":"git@github.com:ianhays/corefx.git","clone_url":"https://github.com/ianhays/corefx.git","svn_url":"https://github.com/ianhays/corefx","homepage":"http://dotnet.github.io/core/","size":39504,"stargazers_count":0,"watchers_count":0,"language":"C#","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"dotnet:master","ref":"master","sha":"441ed83a05e2ca76a5670b16c320fa9cce3491a2","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":26295345,"name":"corefx","full_name":"dotnet/corefx","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/corefx","description":"This repo contains the .NET Core foundational libraries, called CoreFX. It includes classes for collections, file systems, console, XML, async and many others.","fork":false,"url":"https://api.github.com/repos/dotnet/corefx","forks_url":"https://api.github.com/repos/dotnet/corefx/forks","keys_url":"https://api.github.com/repos/dotnet/corefx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/corefx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/corefx/teams","hooks_url":"https://api.github.com/repos/dotnet/corefx/hooks","issue_events_url":"https://api.github.com/repos/dotnet/corefx/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/corefx/events","assignees_url":"https://api.github.com/repos/dotnet/corefx/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/corefx/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/corefx/tags","blobs_url":"https://api.github.com/repos/dotnet/corefx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/corefx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/corefx/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/corefx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/corefx/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/corefx/languages","stargazers_url":"https://api.github.com/repos/dotnet/corefx/stargazers","contributors_url":"https://api.github.com/repos/dotnet/corefx/contributors","subscribers_url":"https://api.github.com/repos/dotnet/corefx/subscribers","subscription_url":"https://api.github.com/repos/dotnet/corefx/subscription","commits_url":"https://api.github.com/repos/dotnet/corefx/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/corefx/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/corefx/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/corefx/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/corefx/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/corefx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/corefx/merges","archive_url":"https://api.github.com/repos/dotnet/corefx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/corefx/downloads","issues_url":"https://api.github.com/repos/dotnet/corefx/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/corefx/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/corefx/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/corefx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/corefx/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/corefx/releases{/id}","created_at":"2014-11-06T23:42:48Z","updated_at":"2015-11-12T02:16:34Z","pushed_at":"2015-11-12T04:23:16Z","git_url":"git://github.com/dotnet/corefx.git","ssh_url":"git@github.com:dotnet/corefx.git","clone_url":"https://github.com/dotnet/corefx.git","svn_url":"https://github.com/dotnet/corefx","homepage":"http://dotnet.github.io/","size":94165,"stargazers_count":8578,"watchers_count":8578,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1913,"mirror_url":null,"open_issues_count":697,"forks":1913,"open_issues":697,"watchers":8578,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/4456"},"html":{"href":"https://github.com/dotnet/corefx/pull/4456"},"issue":{"href":"https://api.github.com/repos/dotnet/corefx/issues/4456"},"comments":{"href":"https://api.github.com/repos/dotnet/corefx/issues/4456/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/4456/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/4456/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/corefx/statuses/01c0823c1548740c8efbb4002b61663a9e7cec12"}}}}