@@ -42,13 +42,15 @@ namespace MsgPack
4242
4343 partial class ByteArrayPackerWriter
4444 {
45+ private const int MaximumUtf8Length = 4 ;
46+
4547 public override void WriteBytes ( byte header , byte value )
4648 {
4749 var currentBuffer = this . _currentBuffer ;
4850 var currentBufferOffset = this . _currentBufferOffset ;
4951 var currentBufferLimit = this . _currentBufferLimit ;
5052 var currentBufferIndex = this . _currentBufferIndex ;
51- if ( ! this . ShiftBufferIfNeeded ( sizeof ( byte ) + 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
53+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( byte ) + 1 , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
5254 {
5355 this . ThrowEofException ( sizeof ( byte ) ) ;
5456 }
@@ -75,7 +77,7 @@ public override void WriteBytes( byte header, byte value )
7577
7678 private void WriteBytesSlow ( byte value , ref int currentBufferIndex , ref byte [ ] currentBuffer , ref int currentBufferOffset , ref int currentBufferLimit )
7779 {
78- if ( ! this . ShiftBufferIfNeeded ( sizeof ( byte ) , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
80+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( byte ) , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
7981 {
8082 this . ThrowEofException ( sizeof ( byte ) ) ;
8183 }
@@ -92,7 +94,7 @@ private void WriteBytesSlow( byte value, ref int currentBufferIndex, ref byte[]
9294
9395 currentBufferOffset += currentWritten ;
9496
95- if ( ! this . ShiftBufferIfNeeded ( sizeof ( byte ) - totalWritten , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
97+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( byte ) - totalWritten , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
9698 {
9799 this . ThrowEofException ( sizeof ( byte ) ) ;
98100 }
@@ -107,7 +109,7 @@ public override void WriteBytes( byte header, ushort value )
107109 var currentBufferOffset = this . _currentBufferOffset ;
108110 var currentBufferLimit = this . _currentBufferLimit ;
109111 var currentBufferIndex = this . _currentBufferIndex ;
110- if ( ! this . ShiftBufferIfNeeded ( sizeof ( ushort ) + 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
112+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( ushort ) + 1 , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
111113 {
112114 this . ThrowEofException ( sizeof ( ushort ) ) ;
113115 }
@@ -135,7 +137,7 @@ public override void WriteBytes( byte header, ushort value )
135137
136138 private void WriteBytesSlow ( ushort value , ref int currentBufferIndex , ref byte [ ] currentBuffer , ref int currentBufferOffset , ref int currentBufferLimit )
137139 {
138- if ( ! this . ShiftBufferIfNeeded ( sizeof ( ushort ) , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
140+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( ushort ) , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
139141 {
140142 this . ThrowEofException ( sizeof ( ushort ) ) ;
141143 }
@@ -152,7 +154,7 @@ private void WriteBytesSlow( ushort value, ref int currentBufferIndex, ref byte[
152154
153155 currentBufferOffset += currentWritten ;
154156
155- if ( ! this . ShiftBufferIfNeeded ( sizeof ( ushort ) - totalWritten , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
157+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( ushort ) - totalWritten , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
156158 {
157159 this . ThrowEofException ( sizeof ( ushort ) ) ;
158160 }
@@ -167,7 +169,7 @@ public override void WriteBytes( byte header, uint value )
167169 var currentBufferOffset = this . _currentBufferOffset ;
168170 var currentBufferLimit = this . _currentBufferLimit ;
169171 var currentBufferIndex = this . _currentBufferIndex ;
170- if ( ! this . ShiftBufferIfNeeded ( sizeof ( uint ) + 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
172+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( uint ) + 1 , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
171173 {
172174 this . ThrowEofException ( sizeof ( uint ) ) ;
173175 }
@@ -197,7 +199,7 @@ public override void WriteBytes( byte header, uint value )
197199
198200 private void WriteBytesSlow ( uint value , ref int currentBufferIndex , ref byte [ ] currentBuffer , ref int currentBufferOffset , ref int currentBufferLimit )
199201 {
200- if ( ! this . ShiftBufferIfNeeded ( sizeof ( uint ) , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
202+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( uint ) , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
201203 {
202204 this . ThrowEofException ( sizeof ( uint ) ) ;
203205 }
@@ -214,7 +216,7 @@ private void WriteBytesSlow( uint value, ref int currentBufferIndex, ref byte[]
214216
215217 currentBufferOffset += currentWritten ;
216218
217- if ( ! this . ShiftBufferIfNeeded ( sizeof ( uint ) - totalWritten , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
219+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( uint ) - totalWritten , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
218220 {
219221 this . ThrowEofException ( sizeof ( uint ) ) ;
220222 }
@@ -229,7 +231,7 @@ public override void WriteBytes( byte header, ulong value )
229231 var currentBufferOffset = this . _currentBufferOffset ;
230232 var currentBufferLimit = this . _currentBufferLimit ;
231233 var currentBufferIndex = this . _currentBufferIndex ;
232- if ( ! this . ShiftBufferIfNeeded ( sizeof ( ulong ) + 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
234+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( ulong ) + 1 , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
233235 {
234236 this . ThrowEofException ( sizeof ( ulong ) ) ;
235237 }
@@ -263,7 +265,7 @@ public override void WriteBytes( byte header, ulong value )
263265
264266 private void WriteBytesSlow ( ulong value , ref int currentBufferIndex , ref byte [ ] currentBuffer , ref int currentBufferOffset , ref int currentBufferLimit )
265267 {
266- if ( ! this . ShiftBufferIfNeeded ( sizeof ( ulong ) , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
268+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( ulong ) , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
267269 {
268270 this . ThrowEofException ( sizeof ( ulong ) ) ;
269271 }
@@ -280,7 +282,7 @@ private void WriteBytesSlow( ulong value, ref int currentBufferIndex, ref byte[]
280282
281283 currentBufferOffset += currentWritten ;
282284
283- if ( ! this . ShiftBufferIfNeeded ( sizeof ( ulong ) - totalWritten , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
285+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( ulong ) - totalWritten , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
284286 {
285287 this . ThrowEofException ( sizeof ( ulong ) ) ;
286288 }
@@ -296,7 +298,7 @@ public override void WriteBytes( byte header, float value )
296298 var currentBufferOffset = this . _currentBufferOffset ;
297299 var currentBufferLimit = this . _currentBufferLimit ;
298300 var currentBufferIndex = this . _currentBufferIndex ;
299- if ( ! this . ShiftBufferIfNeeded ( sizeof ( float ) + 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
301+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( float ) + 1 , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
300302 {
301303 this . ThrowEofException ( sizeof ( float ) ) ;
302304 }
@@ -326,7 +328,7 @@ public override void WriteBytes( byte header, float value )
326328
327329 private void WriteBytesSlow ( int value , ref int currentBufferIndex , ref byte [ ] currentBuffer , ref int currentBufferOffset , ref int currentBufferLimit )
328330 {
329- if ( ! this . ShiftBufferIfNeeded ( sizeof ( float ) , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
331+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( float ) , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
330332 {
331333 this . ThrowEofException ( sizeof ( float ) ) ;
332334 }
@@ -343,7 +345,7 @@ private void WriteBytesSlow( int value, ref int currentBufferIndex, ref byte[] c
343345
344346 currentBufferOffset += currentWritten ;
345347
346- if ( ! this . ShiftBufferIfNeeded ( sizeof ( float ) - totalWritten , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
348+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( float ) - totalWritten , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
347349 {
348350 this . ThrowEofException ( sizeof ( float ) ) ;
349351 }
@@ -359,7 +361,7 @@ public override void WriteBytes( byte header, double value )
359361 var currentBufferOffset = this . _currentBufferOffset ;
360362 var currentBufferLimit = this . _currentBufferLimit ;
361363 var currentBufferIndex = this . _currentBufferIndex ;
362- if ( ! this . ShiftBufferIfNeeded ( sizeof ( double ) + 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
364+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( double ) + 1 , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
363365 {
364366 this . ThrowEofException ( sizeof ( double ) ) ;
365367 }
@@ -393,7 +395,7 @@ public override void WriteBytes( byte header, double value )
393395
394396 private void WriteBytesSlow ( long value , ref int currentBufferIndex , ref byte [ ] currentBuffer , ref int currentBufferOffset , ref int currentBufferLimit )
395397 {
396- if ( ! this . ShiftBufferIfNeeded ( sizeof ( double ) , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
398+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( double ) , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
397399 {
398400 this . ThrowEofException ( sizeof ( double ) ) ;
399401 }
@@ -410,7 +412,7 @@ private void WriteBytesSlow( long value, ref int currentBufferIndex, ref byte[]
410412
411413 currentBufferOffset += currentWritten ;
412414
413- if ( ! this . ShiftBufferIfNeeded ( sizeof ( double ) - totalWritten , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
415+ if ( ! this . ShiftBufferIfNeeded ( sizeof ( double ) - totalWritten , 1 , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
414416 {
415417 this . ThrowEofException ( sizeof ( double ) ) ;
416418 }
@@ -436,7 +438,7 @@ public override void WriteBytes( string value, bool allowStr8 )
436438 }
437439 else
438440 {
439- this . WriteStringBody ( value ) ;
441+ this . WriteStringBody ( value , encodedLength ) ;
440442 }
441443 }
442444
@@ -509,7 +511,7 @@ private void WriteStringHeader( int bytesLength, bool allowStr8 )
509511 }
510512
511513#if ! FEATURE_POINTER_CONVERSION
512- private void WriteStringBody ( string value )
514+ private void WriteStringBody ( string value , int encodedLength )
513515 {
514516 var chars = BufferManager . NewCharBuffer ( value . Length ) ;
515517 int offset = 0 ;
@@ -518,16 +520,16 @@ private void WriteStringBody( string value )
518520 {
519521 int copying = Math . Min ( value . Length - offset , chars . Length ) ;
520522 value . CopyTo ( offset , chars , 0 , copying ) ;
521- this . WriteStringBody ( chars , copying ) ;
523+ this . WriteStringBody ( chars , copying , encodedLength ) ;
522524 offset += copying ;
523525 }
524526 }
525527
526- private void WriteStringBody ( char [ ] value , int remainingCharsLength )
528+ private void WriteStringBody ( char [ ] value , int remainingCharsLength , int encodedLength )
527529 {
528530 var charsOffset = 0 ;
529531#else
530- private unsafe void WriteStringBody ( string value )
532+ private unsafe void WriteStringBody ( string value , int encodedLength )
531533 {
532534 fixed ( char * pValue = value )
533535#endif // !FEATURE_POINTER_CONVERSION
@@ -541,12 +543,13 @@ private unsafe void WriteStringBody( string value )
541543 var pChars = pValue ;
542544 var remainingCharsLength = value . Length ;
543545#endif // FEATURE_POINTER_CONVERSION
546+ var remainingBytesLength = encodedLength ;
544547 var isCompleted = false ;
545548 do
546549 {
547- if ( ! this . ShiftBufferIfNeeded ( remainingCharsLength * sizeof ( char ) , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
550+ if ( ! this . ShiftBufferIfNeeded ( remainingBytesLength , MaximumUtf8Length , ref currentBuffer , ref currentBufferOffset , ref currentBufferLimit , ref currentBufferIndex ) )
548551 {
549- this . ThrowEofExceptionForString ( ( value . Length - remainingCharsLength ) * sizeof ( char ) ) ;
552+ this . ThrowEofExceptionForString ( remainingBytesLength ) ;
550553 }
551554
552555 int charsUsed , bytesUsed ;
@@ -567,6 +570,7 @@ private unsafe void WriteStringBody( string value )
567570 charsOffset += charsUsed ;
568571#endif // FEATURE_POINTER_CONVERSION
569572 remainingCharsLength -= charsUsed ;
573+ remainingBytesLength -= bytesUsed ;
570574 currentBufferOffset += bytesUsed ;
571575 } while ( remainingCharsLength > 0 ) ;
572576#if DEBUG
0 commit comments