@@ -100,15 +100,15 @@ public class MessageUnpacker
100100 private long totalReadBytes ;
101101
102102 /**
103- * Extra buffer for fixed-length data at the buffer boundary.
103+ * An extra buffer for reading a small number value across the input buffer boundary.
104104 * At most 8-byte buffer (for readLong used by uint 64 and UTF-8 character decoding) is required.
105105 */
106- private final MessageBuffer castBuffer = MessageBuffer .allocate (8 );
106+ private final MessageBuffer numberBuffer = MessageBuffer .allocate (8 );
107107
108108 /**
109- * Variable by ensureHeader method. Caller of the method should use this variable to read from returned MessageBuffer.
109+ * After calling prepareNumberBuffer(), the caller should use this variable to read from the returned MessageBuffer.
110110 */
111- private int readCastBufferPosition ;
111+ private int nextReadPosition ;
112112
113113 /**
114114 * For decoding String in unpackString.
@@ -169,52 +169,70 @@ public long getTotalReadBytes()
169169 return totalReadBytes + position ;
170170 }
171171
172- private void nextBuffer ()
172+ /**
173+ * Get the next buffer without changing the position
174+ * @return
175+ * @throws IOException
176+ */
177+ private MessageBuffer getNextBuffer ()
173178 throws IOException
174179 {
175180 MessageBuffer next = in .next ();
176181 if (next == null ) {
177182 throw new MessageInsufficientBufferException ();
178183 }
179184 totalReadBytes += buffer .size ();
180- buffer = next ;
185+ return next ;
186+ }
187+
188+ private void nextBuffer ()
189+ throws IOException
190+ {
191+ buffer = getNextBuffer ();
181192 position = 0 ;
182193 }
183194
184- private MessageBuffer readCastBuffer (int length )
195+ /**
196+ * Returns a short size buffer (upto 8 bytes) to read a number value
197+ * @param readLength
198+ * @return
199+ * @throws IOException
200+ * @throws MessageInsufficientBufferException If no more buffer can be acquired from the input source for reading the specified data length
201+ */
202+ private MessageBuffer prepareNumberBuffer (int readLength )
185203 throws IOException
186204 {
187205 int remaining = buffer .size () - position ;
188- if (remaining >= length ) {
189- readCastBufferPosition = position ;
190- position += length ; // here assumes following buffer.getXxx never throws exception
191- return buffer ;
206+ if (remaining >= readLength ) {
207+ // When the data is contained inside the default buffer
208+ nextReadPosition = position ;
209+ position += readLength ; // here assumes following buffer.getXxx never throws exception
210+ return buffer ; // Return the default buffer
192211 }
193212 else {
194- // TODO loop this method until castBuffer is filled
195- MessageBuffer next = in .next ();
196- if (next == null ) {
197- throw new MessageInsufficientBufferException ();
198- }
213+ // When the default buffer doesn't contain the whole length
199214
200- totalReadBytes += buffer .size ();
215+ // TODO loop this method until castBuffer is filled
216+ MessageBuffer next = getNextBuffer ();
201217
202218 if (remaining > 0 ) {
203- // TODO this doesn't work if MessageBuffer is allocated by newDirectBuffer.
204- // add copy method to MessageBuffer to solve this issue.
205- castBuffer .putBytes (0 , buffer .array (), buffer .arrayOffset () + position , remaining );
206- castBuffer .putBytes (remaining , next .array (), next .arrayOffset (), length - remaining );
219+ // TODO This doesn't work if MessageBuffer is allocated by newDirectBuffer.
220+ // Add copy method to MessageBuffer to solve this issue.
221+
222+ // Copy the data fragment from the current buffer
223+ numberBuffer .putBytes (0 , buffer .array (), buffer .arrayOffset () + position , remaining );
224+ numberBuffer .putBytes (remaining , next .array (), next .arrayOffset (), readLength - remaining );
207225
208226 buffer = next ;
209- position = length - remaining ;
210- readCastBufferPosition = 0 ;
227+ position = readLength - remaining ;
228+ nextReadPosition = 0 ;
211229
212- return castBuffer ;
230+ return numberBuffer ; // Return the numberBuffer
213231 }
214232 else {
215233 buffer = next ;
216- position = length ;
217- readCastBufferPosition = 0 ;
234+ position = readLength ;
235+ nextReadPosition = 0 ;
218236 return buffer ;
219237 }
220238 }
@@ -296,36 +314,36 @@ private byte readByte()
296314 private short readShort ()
297315 throws IOException
298316 {
299- MessageBuffer castBuffer = readCastBuffer (2 );
300- return castBuffer .getShort (readCastBufferPosition );
317+ MessageBuffer numberBuffer = prepareNumberBuffer (2 );
318+ return numberBuffer .getShort (nextReadPosition );
301319 }
302320
303321 private int readInt ()
304322 throws IOException
305323 {
306- MessageBuffer castBuffer = readCastBuffer (4 );
307- return castBuffer .getInt (readCastBufferPosition );
324+ MessageBuffer numberBuffer = prepareNumberBuffer (4 );
325+ return numberBuffer .getInt (nextReadPosition );
308326 }
309327
310328 private long readLong ()
311329 throws IOException
312330 {
313- MessageBuffer castBuffer = readCastBuffer (8 );
314- return castBuffer .getLong (readCastBufferPosition );
331+ MessageBuffer numberBuffer = prepareNumberBuffer (8 );
332+ return numberBuffer .getLong (nextReadPosition );
315333 }
316334
317335 private float readFloat ()
318336 throws IOException
319337 {
320- MessageBuffer castBuffer = readCastBuffer (4 );
321- return castBuffer .getFloat (readCastBufferPosition );
338+ MessageBuffer numberBuffer = prepareNumberBuffer (4 );
339+ return numberBuffer .getFloat (nextReadPosition );
322340 }
323341
324342 private double readDouble ()
325343 throws IOException
326344 {
327- MessageBuffer castBuffer = readCastBuffer (8 );
328- return castBuffer .getDouble (readCastBufferPosition );
345+ MessageBuffer numberBuffer = prepareNumberBuffer (8 );
346+ return numberBuffer .getDouble (nextReadPosition );
329347 }
330348
331349 /**
@@ -1079,27 +1097,27 @@ public ExtensionTypeHeader unpackExtensionTypeHeader()
10791097 return new ExtensionTypeHeader (type , 16 );
10801098 }
10811099 case Code .EXT8 : {
1082- MessageBuffer castBuffer = readCastBuffer (2 );
1083- int u8 = castBuffer .getByte (readCastBufferPosition );
1100+ MessageBuffer numberBuffer = prepareNumberBuffer (2 );
1101+ int u8 = numberBuffer .getByte (nextReadPosition );
10841102 int length = u8 & 0xff ;
1085- byte type = castBuffer .getByte (readCastBufferPosition + 1 );
1103+ byte type = numberBuffer .getByte (nextReadPosition + 1 );
10861104 return new ExtensionTypeHeader (type , length );
10871105 }
10881106 case Code .EXT16 : {
1089- MessageBuffer castBuffer = readCastBuffer (3 );
1090- int u16 = castBuffer .getShort (readCastBufferPosition );
1107+ MessageBuffer numberBuffer = prepareNumberBuffer (3 );
1108+ int u16 = numberBuffer .getShort (nextReadPosition );
10911109 int length = u16 & 0xffff ;
1092- byte type = castBuffer .getByte (readCastBufferPosition + 2 );
1110+ byte type = numberBuffer .getByte (nextReadPosition + 2 );
10931111 return new ExtensionTypeHeader (type , length );
10941112 }
10951113 case Code .EXT32 : {
1096- MessageBuffer castBuffer = readCastBuffer (5 );
1097- int u32 = castBuffer .getInt (readCastBufferPosition );
1114+ MessageBuffer numberBuffer = prepareNumberBuffer (5 );
1115+ int u32 = numberBuffer .getInt (nextReadPosition );
10981116 if (u32 < 0 ) {
10991117 throw overflowU32Size (u32 );
11001118 }
11011119 int length = u32 ;
1102- byte type = castBuffer .getByte (readCastBufferPosition + 4 );
1120+ byte type = numberBuffer .getByte (nextReadPosition + 4 );
11031121 return new ExtensionTypeHeader (type , length );
11041122 }
11051123 }
0 commit comments