@@ -208,36 +208,44 @@ 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- }
217211 else {
218- // When the default buffer doesn't contain the whole length
219-
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-
229- // TODO loop this method until castBuffer is filled
230- MessageBuffer next = getNextBuffer ();
212+ // When the default buffer doesn't contain the whole length,
213+ // fill the temporary buffer from the current data fragment and
214+ // next fragment(s).
215+
216+ // TODO buffer.array() doesn't work if MessageBuffer is allocated by
217+ // newDirectBuffer. dd copy method to MessageBuffer to solve this issue.
218+
219+ int off = 0 ;
220+ if (remaining > 0 ) {
221+ numberBuffer .putBytes (0 ,
222+ buffer .array (), buffer .arrayOffset () + position ,
223+ remaining );
224+ readLength -= remaining ;
225+ off += remaining ;
226+ }
231227
232- numberBuffer .putBytes (remaining ,
233- next .array (), next .arrayOffset (),
234- readLength - remaining );
228+ while (true ) {
229+ nextBuffer ();
230+ int nextSize = buffer .size ();
231+ if (nextSize >= readLength ) {
232+ numberBuffer .putBytes (off ,
233+ buffer .array (), buffer .arrayOffset (),
234+ readLength );
235+ position = readLength ;
236+ break ;
237+ }
238+ else {
239+ numberBuffer .putBytes (off ,
240+ buffer .array (), buffer .arrayOffset (),
241+ nextSize );
242+ readLength -= nextSize ;
243+ off += nextSize ;
244+ }
245+ }
235246
236- buffer = next ;
237- position = readLength - remaining ;
238247 nextReadPosition = 0 ;
239-
240- return numberBuffer ; // Return the numberBuffer
248+ return numberBuffer ;
241249 }
242250 }
243251
0 commit comments