@@ -61,9 +61,9 @@ public class MessageUnpacker implements Closeable {
6161 private MessageBuffer secondaryBuffer = null ;
6262
6363 /**
64- * Extra buffer for string data at the buffer boundary. For most of the case having 8 byte buffer is sufficient.
64+ * Extra buffer for string data at the buffer boundary. Using 17- byte buffer (for FIXEXT16) is sufficient.
6565 */
66- private final MessageBuffer extraBuffer = MessageBuffer .wrap (new byte [8 ]);
66+ private final MessageBuffer extraBuffer = MessageBuffer .wrap (new byte [24 ]);
6767
6868 /**
6969 * True if no more data is available from the MessageBufferInput
@@ -185,73 +185,44 @@ private boolean ensure(int byteSizeToRead) throws IOException {
185185 return true ;
186186 }
187187
188- if (byteSizeToRead <= extraBuffer .size ()) {
189- // When the data is at the boundary and can fit to the extra buffer
190- /*
188+ // When the data is at the boundary
189+ /*
191190 |---(byte size to read) ----|
192191 -- current buffer --|
193192 |--- extra buffer (slice) --|----|
194193 |-------|---------- secondary buffer (slice) ----------------|
195194
196195 */
197196
198- // Copy the remaining buffer contents to the extra buffer
199- int firstHalfSize = buffer .size () - position ;
200- if (firstHalfSize > 0 )
201- buffer .copyTo (position , extraBuffer , 0 , firstHalfSize );
202-
203- // Read the last half contents from the next buffers
204- int cursor = firstHalfSize ;
205- while (cursor < byteSizeToRead ) {
206- secondaryBuffer = takeNextBuffer ();
207- if (secondaryBuffer == null )
208- return false ; // No more buffer to read
209-
210- // Copy the contents from the secondary buffer to the extra buffer
211- int copyLen = Math .min (byteSizeToRead - cursor , secondaryBuffer .size ());
212- secondaryBuffer .copyTo (0 , extraBuffer , cursor , copyLen );
213-
214- // Truncate the copied part from the secondaryBuffer
215- secondaryBuffer = copyLen == secondaryBuffer .size () ? null : secondaryBuffer .slice (copyLen , secondaryBuffer .size ()-copyLen );
216- cursor += copyLen ;
217- }
218-
219- // Replace the current buffer to the extra buffer
220- buffer = byteSizeToRead == extraBuffer .size () ? extraBuffer : extraBuffer .slice (0 , byteSizeToRead );
221- position = 0 ;
197+ // If the byte size to read fits within the extra buffer, use the extraBuffer
198+ MessageBuffer newBuffer = byteSizeToRead <= extraBuffer .size () ? extraBuffer : MessageBuffer .newBuffer (byteSizeToRead );
222199
223- return true ;
224- } else {
225- // When the data is at the boundary and exceeds the size of the extra buffer, create a new buffer
226- int remaining = buffer .size () - position ;
227- int bufferTotal = remaining ;
228- // Read next buffers
229- ArrayList <MessageBuffer > bufferList = new ArrayList <MessageBuffer >();
230- while (bufferTotal < byteSizeToRead ) {
231- MessageBuffer next = takeNextBuffer ();
232- if (next == null )
233- return false ;
234-
235- bufferTotal += next .size ();
236- bufferList .add (next );
237- }
200+ // Copy the remaining buffer contents to the new buffer
201+ int firstHalfSize = buffer .size () - position ;
202+ if (firstHalfSize > 0 )
203+ buffer .copyTo (position , newBuffer , 0 , firstHalfSize );
238204
239- // create a new buffer that is large enough to hold all entries
240- MessageBuffer newBuffer = MessageBuffer .newBuffer (bufferTotal );
205+ // Read the last half contents from the next buffers
206+ int cursor = firstHalfSize ;
207+ while (cursor < byteSizeToRead ) {
208+ secondaryBuffer = takeNextBuffer ();
209+ if (secondaryBuffer == null )
210+ return false ; // No more buffer to read
241211
242- // Copy the buffer contents to the new buffer
243- int p = 0 ;
244- buffer .copyTo (position , newBuffer , p , remaining );
245- p += remaining ;
246- for (MessageBuffer m : bufferList ) {
247- m .copyTo (0 , newBuffer , p , m .size ());
248- p += m .size ();
249- }
212+ // Copy the contents from the secondary buffer to the new buffer
213+ int copyLen = Math .min (byteSizeToRead - cursor , secondaryBuffer .size ());
214+ secondaryBuffer .copyTo (0 , newBuffer , cursor , copyLen );
250215
251- buffer = newBuffer ;
252- position = 0 ;
253- return true ;
216+ // Truncate the copied part from the secondaryBuffer
217+ secondaryBuffer = copyLen == secondaryBuffer . size () ? null : secondaryBuffer . slice ( copyLen , secondaryBuffer . size ()- copyLen ) ;
218+ cursor += copyLen ;
254219 }
220+
221+ // Replace the current buffer with the new buffer
222+ buffer = byteSizeToRead == newBuffer .size () ? newBuffer : newBuffer .slice (0 , byteSizeToRead );
223+ position = 0 ;
224+
225+ return true ;
255226 }
256227
257228 /**
@@ -495,7 +466,7 @@ public void skipValue() throws IOException {
495466 * @return
496467 * @throws MessageFormatException
497468 */
498- private static MessageTypeException unexpected (String expected , final byte b )
469+ private static MessageTypeException unexpected (String expected , byte b )
499470 throws MessageFormatException {
500471 ValueType type = ValueType .valueOf (b );
501472 return new MessageTypeException (String .format ("Expected %s, but got %s (%02x)" , expected , type .toTypeName (), b ));
@@ -998,17 +969,17 @@ public void readPayload(byte[] dst, int off, int len) throws IOException {
998969 }
999970
1000971
1001- int readNextLength8 () throws IOException {
972+ private int readNextLength8 () throws IOException {
1002973 byte u8 = readByte ();
1003974 return u8 & 0xff ;
1004975 }
1005976
1006- int readNextLength16 () throws IOException {
977+ private int readNextLength16 () throws IOException {
1007978 short u16 = readShort ();
1008979 return u16 & 0xffff ;
1009980 }
1010981
1011- int readNextLength32 () throws IOException {
982+ private int readNextLength32 () throws IOException {
1012983 int u32 = readInt ();
1013984 if (u32 < 0 ) {
1014985 throw overflowU32Size (u32 );
@@ -1021,43 +992,43 @@ public void close() throws IOException {
1021992 in .close ();
1022993 }
1023994
1024- private static MessageIntegerOverflowException overflowU8 (final byte u8 ) {
1025- final BigInteger bi = BigInteger .valueOf ((long ) (u8 & 0xff ));
995+ private static MessageIntegerOverflowException overflowU8 (byte u8 ) {
996+ BigInteger bi = BigInteger .valueOf ((long ) (u8 & 0xff ));
1026997 return new MessageIntegerOverflowException (bi );
1027998 }
1028999
1029- private static MessageIntegerOverflowException overflowU16 (final short u16 ) {
1030- final BigInteger bi = BigInteger .valueOf ((long ) (u16 & 0xffff ));
1000+ private static MessageIntegerOverflowException overflowU16 (short u16 ) {
1001+ BigInteger bi = BigInteger .valueOf ((long ) (u16 & 0xffff ));
10311002 return new MessageIntegerOverflowException (bi );
10321003 }
10331004
1034- private static MessageIntegerOverflowException overflowU32 (final int u32 ) {
1035- final BigInteger bi = BigInteger .valueOf ((long ) (u32 & 0x7fffffff ) + 0x80000000L );
1005+ private static MessageIntegerOverflowException overflowU32 (int u32 ) {
1006+ BigInteger bi = BigInteger .valueOf ((long ) (u32 & 0x7fffffff ) + 0x80000000L );
10361007 return new MessageIntegerOverflowException (bi );
10371008 }
10381009
1039- private static MessageIntegerOverflowException overflowU64 (final long u64 ) {
1040- final BigInteger bi = BigInteger .valueOf (u64 + Long .MAX_VALUE + 1L ).setBit (63 );
1010+ private static MessageIntegerOverflowException overflowU64 (long u64 ) {
1011+ BigInteger bi = BigInteger .valueOf (u64 + Long .MAX_VALUE + 1L ).setBit (63 );
10411012 return new MessageIntegerOverflowException (bi );
10421013 }
10431014
1044- private static MessageIntegerOverflowException overflowI16 (final short i16 ) {
1045- final BigInteger bi = BigInteger .valueOf ((long ) i16 );
1015+ private static MessageIntegerOverflowException overflowI16 (short i16 ) {
1016+ BigInteger bi = BigInteger .valueOf ((long ) i16 );
10461017 return new MessageIntegerOverflowException (bi );
10471018 }
10481019
1049- private static MessageIntegerOverflowException overflowI32 (final int i32 ) {
1050- final BigInteger bi = BigInteger .valueOf ((long ) i32 );
1020+ private static MessageIntegerOverflowException overflowI32 (int i32 ) {
1021+ BigInteger bi = BigInteger .valueOf ((long ) i32 );
10511022 return new MessageIntegerOverflowException (bi );
10521023 }
10531024
1054- private static MessageIntegerOverflowException overflowI64 (final long i64 ) {
1055- final BigInteger bi = BigInteger .valueOf (i64 );
1025+ private static MessageIntegerOverflowException overflowI64 (long i64 ) {
1026+ BigInteger bi = BigInteger .valueOf (i64 );
10561027 return new MessageIntegerOverflowException (bi );
10571028 }
10581029
1059- private static MessageSizeException overflowU32Size (final int u32 ) {
1060- final long lv = (long ) (u32 & 0x7fffffff ) + 0x80000000L ;
1030+ private static MessageSizeException overflowU32Size (int u32 ) {
1031+ long lv = (long ) (u32 & 0x7fffffff ) + 0x80000000L ;
10611032 return new MessageSizeException (lv );
10621033 }
10631034
0 commit comments