1717
1818import org .msgpack .core .MessagePack .Code ;
1919import org .msgpack .core .buffer .MessageBuffer ;
20- import org .msgpack .core .buffer .MessageBufferReader ;
2120import org .msgpack .core .buffer .MessageBufferInput ;
2221import org .msgpack .value .ImmutableValue ;
2322import org .msgpack .value .Value ;
@@ -186,7 +185,7 @@ private byte getHeadByte()
186185 if (b == HEAD_BYTE_REQUIRED ) {
187186 b = headByte = readByte ();
188187 if (b == HEAD_BYTE_REQUIRED ) {
189- throw new MessageNeverUsedFormatException ("Encountered 0xC1 NEVER_USED byte" );
188+ throw new MessageNeverUsedFormatException ("Encountered 0xC1 \" NEVER_USED\" byte" );
190189 }
191190 }
192191 return b ;
@@ -205,7 +204,9 @@ private void nextBuffer()
205204 throw new MessageInsufficientBufferException ();
206205 }
207206 totalReadBytes += buffer .size ();
208- in .release (buffer );
207+ if (buffer != EMPTY_BUFFER ) {
208+ in .release (buffer );
209+ }
209210 buffer = next ;
210211 position = 0 ;
211212 }
@@ -232,7 +233,9 @@ private MessageBuffer readCastBuffer(int length)
232233 castBuffer .putBytes (remaining , next .getArray (), next .offset (), length - remaining );
233234
234235 totalReadBytes += buffer .size ();
235- in .release (buffer );
236+ if (buffer != EMPTY_BUFFER ) {
237+ in .release (buffer );
238+ }
236239
237240 buffer = next ;
238241 position = length - remaining ;
@@ -244,8 +247,7 @@ private MessageBuffer readCastBuffer(int length)
244247
245248 private static int utf8MultibyteCharacterSize (byte firstByte )
246249 {
247- System .out .println ("first byte: " +(firstByte & 0xff ));
248- return Integer .numberOfLeadingZeros (~(firstByte & 0xff ));
250+ return Integer .numberOfLeadingZeros (~(firstByte & 0xff ) << 24 );
249251 }
250252
251253 /**
@@ -264,7 +266,9 @@ public boolean hasNext()
264266 return false ;
265267 }
266268 totalReadBytes += buffer .size ();
267- in .release (buffer );
269+ if (buffer != EMPTY_BUFFER ) {
270+ in .release (buffer );
271+ }
268272 buffer = next ;
269273 position = 0 ;
270274 }
@@ -490,7 +494,7 @@ public ImmutableValue unpackValue()
490494 MessageFormat mf = getNextFormat ();
491495 switch (mf .getValueType ()) {
492496 case NIL :
493- unpackNil ();
497+ readByte ();
494498 return ValueFactory .newNil ();
495499 case BOOLEAN :
496500 return ValueFactory .newBoolean (unpackBoolean ());
@@ -1070,7 +1074,7 @@ else if (bufferRemaining == 0) {
10701074 if (cr .isError ()) {
10711075 handleCoderError (cr );
10721076 }
1073- if (cr .isUnderflow () || cr .isOverflow ( )) {
1077+ if (cr .isOverflow () || ( cr .isUnderflow () && multiByteBuffer . position () < multiByteBuffer . limit () )) {
10741078 // isOverflow or isOverflow must not happen. if happened, throw exception
10751079 try {
10761080 cr .throwException ();
@@ -1106,7 +1110,7 @@ private String decodeStringFastPath(int length)
11061110 if (config .actionOnMalFormedInput == CodingErrorAction .REPLACE &&
11071111 config .actionOnUnmappableCharacter == CodingErrorAction .REPLACE &&
11081112 buffer .hasArray ()) {
1109- String s = new String (buffer .getArray (), position , length , MessagePack .UTF8 );
1113+ String s = new String (buffer .getArray (), buffer . offset () + position , length , MessagePack .UTF8 );
11101114 position += length ;
11111115 return s ;
11121116 }
@@ -1425,6 +1429,11 @@ private int readNextLength32()
14251429 public void close ()
14261430 throws IOException
14271431 {
1432+ if (buffer != EMPTY_BUFFER ) {
1433+ in .release (buffer );
1434+ buffer = EMPTY_BUFFER ;
1435+ position = 0 ;
1436+ }
14281437 in .close ();
14291438 }
14301439
0 commit comments