@@ -86,17 +86,18 @@ namespace MsgPack
8686#>
8787 var currentBuffer = this._currentBuffer;
8888 var currentBufferOffset = this._currentBufferOffset;
89- var currentBufferLimit = this._currentBufferLimit ;
89+ var currentBufferRemains = this._currentBufferRemains ;
9090 var currentBufferIndex = this._currentBufferIndex;
91- if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ) + 1, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit , ref currentBufferIndex ) )
91+ if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ) + 1, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains , ref currentBufferIndex ) )
9292 {
9393 this.ThrowEofException( sizeof( <#= type #> ) );
9494 }
9595
9696 currentBuffer[ currentBufferOffset ] = header;
9797 currentBufferOffset += 1;
98+ currentBufferRemains -= 1;
9899
99- if ( currentBufferLimit - currentBufferOffset >= sizeof( <#= type #> ) )
100+ if ( currentBufferRemains >= sizeof( <#= type #> ) )
100101 {
101102 // Fast path
102103<#
@@ -109,16 +110,17 @@ namespace MsgPack
109110 }
110111#>
111112 currentBufferOffset += sizeof( <#= type #> );
113+ currentBufferRemains -= sizeof( <#= type #> );
112114 }
113115 else
114116 {
115- this.WriteBytesSlow( <#= bits #>, ref currentBufferIndex, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit );
117+ this.WriteBytesSlow( <#= bits #>, ref currentBufferIndex, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains );
116118 }
117119
118120 this._currentBufferIndex = currentBufferIndex;
119121 this._currentBuffer = currentBuffer;
120122 this._currentBufferOffset = currentBufferOffset;
121- this._currentBufferLimit = currentBufferLimit ;
123+ this._currentBufferRemains = currentBufferRemains ;
122124<#
123125 } // if isAsync
124126#>
@@ -128,14 +130,14 @@ namespace MsgPack
128130 if ( !isAsync )
129131 {
130132#>
131- private void WriteBytesSlow( <#= this.ToBitsType( type ) #> value, ref int currentBufferIndex, ref byte[] currentBuffer, ref int currentBufferOffset, ref int currentBufferLimit )
133+ private void WriteBytesSlow( <#= this.ToBitsType( type ) #> value, ref int currentBufferIndex, ref byte[] currentBuffer, ref int currentBufferOffset, ref int currentBufferRemains )
132134 {
133- if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ), 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit , ref currentBufferIndex ) )
135+ if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ), 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains , ref currentBufferIndex ) )
134136 {
135137 this.ThrowEofException( sizeof( <#= type #> ) );
136138 }
137139
138- var bufferRemaining = currentBufferLimit - currentBufferOffset ;
140+ var bufferRemaining = currentBufferRemains ;
139141
140142 for ( var totalWritten = 0; totalWritten < sizeof( <#= type #> ); )
141143 {
@@ -146,13 +148,14 @@ namespace MsgPack
146148 }
147149
148150 currentBufferOffset += currentWritten;
151+ currentBufferRemains -= currentWritten;
149152
150- if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ) - totalWritten, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit , ref currentBufferIndex ) )
153+ if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ) - totalWritten, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains , ref currentBufferIndex ) )
151154 {
152155 this.ThrowEofException( sizeof( <#= type #> ) );
153156 }
154157
155- bufferRemaining = currentBufferLimit - currentBufferOffset ;
158+ bufferRemaining = currentBufferRemains ;
156159 }
157160 }
158161
@@ -180,11 +183,12 @@ namespace MsgPack
180183 return;
181184 }
182185
183- if ( encodedLength <= this._currentBufferLimit - this._currentBufferOffset )
186+ if ( encodedLength <= this._currentBufferRemains )
184187 {
185188 // Fast path
186189 Encoding.UTF8.GetBytes( value, 0, value.Length, this._currentBuffer, this._currentBufferOffset );
187190 this._currentBufferOffset += encodedLength;
191+ this._currentBufferRemains -= encodedLength;
188192 }
189193 else
190194 {
@@ -254,7 +258,7 @@ namespace MsgPack
254258 {
255259 var currentBuffer = this._currentBuffer;
256260 var currentBufferOffset = this._currentBufferOffset;
257- var currentBufferLimit = this._currentBufferLimit ;
261+ var currentBufferRemains = this._currentBufferRemains ;
258262 var currentBufferIndex = this._currentBufferIndex;
259263 var encoder = Encoding.UTF8.GetEncoder();
260264#if FEATURE_POINTER_CONVERSION
@@ -265,7 +269,7 @@ namespace MsgPack
265269 var isCompleted = false;
266270 do
267271 {
268- if ( !this.ShiftBufferIfNeeded( remainingBytesLength, MaximumUtf8Length, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit , ref currentBufferIndex ) )
272+ if ( !this.ShiftBufferIfNeeded( remainingBytesLength, MaximumUtf8Length, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains , ref currentBufferIndex ) )
269273 {
270274 this.ThrowEofExceptionForString( remainingBytesLength );
271275 }
@@ -276,9 +280,9 @@ namespace MsgPack
276280#endif // FEATURE_POINTER_CONVERSION
277281 {
278282#if FEATURE_POINTER_CONVERSION
279- isCompleted = encoder.EncodeString( pChars, remainingCharsLength, pBuffer + currentBufferOffset, currentBufferLimit - currentBufferOffset , out charsUsed, out bytesUsed );
283+ isCompleted = encoder.EncodeString( pChars, remainingCharsLength, pBuffer + currentBufferOffset, currentBufferRemains , out charsUsed, out bytesUsed );
280284#else
281- isCompleted = encoder.EncodeString( value, charsOffset, remainingCharsLength, currentBuffer, currentBufferOffset, currentBufferLimit - currentBufferOffset , out charsUsed, out bytesUsed );
285+ isCompleted = encoder.EncodeString( value, charsOffset, remainingCharsLength, currentBuffer, currentBufferOffset, currentBufferRemains , out charsUsed, out bytesUsed );
282286#endif // FEATURE_POINTER_CONVERSION
283287 }
284288
@@ -290,6 +294,7 @@ namespace MsgPack
290294 remainingCharsLength -= charsUsed;
291295 remainingBytesLength -= bytesUsed;
292296 currentBufferOffset += bytesUsed;
297+ currentBufferRemains -= bytesUsed;
293298 } while ( remainingCharsLength > 0 );
294299#if DEBUG
295300 Contract.Assert( isCompleted, "Encoding is not completed!" );
@@ -298,7 +303,7 @@ namespace MsgPack
298303 this._currentBufferIndex = currentBufferIndex;
299304 this._currentBuffer = currentBuffer;
300305 this._currentBufferOffset = currentBufferOffset;
301- this._currentBufferLimit = currentBufferLimit ;
306+ this._currentBufferRemains = currentBufferRemains ;
302307 }
303308 }
304309 }
0 commit comments