@@ -208,32 +208,36 @@ private MessageBuffer prepareNumberBuffer(int readLength)
208208 position += readLength ; // here assumes following buffer.getXxx never throws exception
209209 return buffer ; // Return the default buffer
210210 }
211+ else if (remaining == 0 ) {
212+ buffer = getNextBuffer ();
213+ position = readLength ;
214+ nextReadPosition = 0 ;
215+ return buffer ;
216+ }
211217 else {
212218 // When the default buffer doesn't contain the whole length
213219
220+ // TODO This doesn't work if MessageBuffer is allocated by newDirectBuffer.
221+ // Add copy method to MessageBuffer to solve this issue.
222+
223+ // Copy the data fragment from the current buffer
224+
225+ numberBuffer .putBytes (0 ,
226+ buffer .array (), buffer .arrayOffset () + position ,
227+ remaining );
228+
214229 // TODO loop this method until castBuffer is filled
215230 MessageBuffer next = getNextBuffer ();
216231
217- if (remaining > 0 ) {
218- // TODO This doesn't work if MessageBuffer is allocated by newDirectBuffer.
219- // Add copy method to MessageBuffer to solve this issue.
220-
221- // Copy the data fragment from the current buffer
222- numberBuffer .putBytes (0 , buffer .array (), buffer .arrayOffset () + position , remaining );
223- numberBuffer .putBytes (remaining , next .array (), next .arrayOffset (), readLength - remaining );
232+ numberBuffer .putBytes (remaining ,
233+ next .array (), next .arrayOffset (),
234+ readLength - remaining );
224235
225- buffer = next ;
226- position = readLength - remaining ;
227- nextReadPosition = 0 ;
236+ buffer = next ;
237+ position = readLength - remaining ;
238+ nextReadPosition = 0 ;
228239
229- return numberBuffer ; // Return the numberBuffer
230- }
231- else {
232- buffer = next ;
233- position = readLength ;
234- nextReadPosition = 0 ;
235- return buffer ;
236- }
240+ return numberBuffer ; // Return the numberBuffer
237241 }
238242 }
239243
0 commit comments