PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/WCFWebApi/src/Microsoft.ServiceModel.Internal/Microsoft/ServiceModel/Channels/BufferManagerOutputStream.cs

#
C# | 95 lines | 66 code | 17 blank | 12 comment | 2 complexity | 3c7db6f631f378a55c94bd43a463cccd MD5 | raw file
 1// <copyright>
 2//   Copyright (c) Microsoft Corporation.  All rights reserved.
 3// </copyright>
 4
 5namespace Microsoft.ServiceModel.Channels
 6{
 7    using System;
 8    using System.Globalization;
 9    using System.ServiceModel; // for QuotaExceededException
10    using System.ServiceModel.Channels;
11    using Microsoft.Server.Common;
12
13    internal class BufferManagerOutputStream : BufferedOutputStream
14    {
15        private string quotaExceededString;
16
17        public BufferManagerOutputStream(string quotaExceededString)
18            : base()
19        {
20            this.quotaExceededString = quotaExceededString;
21        }
22
23        public BufferManagerOutputStream(string quotaExceededString, int maxSize)
24            : base(maxSize)
25        {
26            this.quotaExceededString = quotaExceededString;
27        }
28
29        // ALTERED_FOR_PORT:
30        // We're not getting the internal buffer manager as we do in the framework but just wrapping the bufferManager
31        public BufferManagerOutputStream(string quotaExceededString, int initialSize, int maxSize, BufferManager bufferManager)
32            : base(initialSize, maxSize, GetInternalBufferManager(bufferManager))
33        {
34            this.quotaExceededString = quotaExceededString;
35        }
36
37        public void Init(int initialSize, int maxSizeQuota, BufferManager bufferManager)
38        {
39            this.Init(initialSize, maxSizeQuota, maxSizeQuota, bufferManager);
40        }
41
42        public void Init(int initialSize, int maxSizeQuota, int effectiveMaxSize, BufferManager bufferManager)
43        {
44            // ALTERED_FOR_PORT:
45            // We're not getting the internal buffer manager as we do in the framework but just wrapping the bufferManager
46            this.Reinitialize(initialSize, maxSizeQuota, effectiveMaxSize, GetInternalBufferManager(bufferManager));
47        }
48
49        protected override Exception CreateQuotaExceededException(int maxSizeQuota)
50        {
51            string excMsg = string.Format(CultureInfo.CurrentCulture, this.quotaExceededString, maxSizeQuota);
52
53            // TODO: CSDMAIN 205175 -- reactivate when tracing and logging are available:
54            //// if (TD.MaxSentMessageSizeExceededIsEnabled())
55            //// {
56            ////     TD.MaxSentMessageSizeExceeded(excMsg);
57            //// }
58            return new QuotaExceededException(excMsg);
59        }
60
61        private static InternalBufferManager GetInternalBufferManager(BufferManager bufferManager)
62        {
63            Fx.Assert(bufferManager != null, "The 'bufferManager' parameter should not be null.");
64
65            return new WrappingInternalBufferManager(bufferManager);
66        }
67
68        private class WrappingInternalBufferManager : InternalBufferManager
69        {
70            private BufferManager bufferManager;
71
72            public WrappingInternalBufferManager(BufferManager bufferManager)
73            {
74                Fx.Assert(bufferManager != null, "The 'bufferManager' parameter should not be null.");
75
76                this.bufferManager = bufferManager;
77            }
78
79            public override byte[] TakeBuffer(int bufferSize)
80            {
81                return this.bufferManager.TakeBuffer(bufferSize);
82            }
83
84            public override void ReturnBuffer(byte[] buffer)
85            {
86                this.bufferManager.ReturnBuffer(buffer);
87            }
88
89            public override void Clear()
90            {
91                this.bufferManager.Clear();
92            }
93        }
94    }
95}