|  | 
| 18 | 18 | import uk.co.real_logic.agrona.DirectBuffer; | 
| 19 | 19 | import uk.co.real_logic.sbe.PrimitiveType; | 
| 20 | 20 | import uk.co.real_logic.sbe.codec.java.CodecUtil; | 
|  | 21 | +import uk.co.real_logic.sbe.ir.Encoding; | 
| 21 | 22 | 
 | 
| 22 | 23 | import java.nio.ByteOrder; | 
| 23 | 24 | 
 | 
|  | 
| 27 | 28 | public class Util | 
| 28 | 29 | { | 
| 29 | 30 |  /** | 
| 30 |  | - * Get an integer value from a buffer at a given index. | 
|  | 31 | + * Get an integer value from a buffer at a given index for a {@link PrimitiveType}. | 
| 31 | 32 |  * | 
| 32 |  | - * @param buffer  from which to read. | 
| 33 |  | - * @param bufferIndex at which he integer should be read. | 
| 34 |  | - * @param type  of the integer encoded in the buffer. | 
| 35 |  | - * @param byteOrder  of the integer in the buffer. | 
|  | 33 | + * @param buffer from which to read. | 
|  | 34 | + * @param index  at which he integer should be read. | 
|  | 35 | + * @param type of the integer encoded in the buffer. | 
|  | 36 | + * @param byteOrder of the integer in the buffer. | 
| 36 | 37 |  * @return the value of the encoded integer. | 
| 37 | 38 |  */ | 
| 38 |  | - static int getInt(final DirectBuffer buffer, final int bufferIndex, final PrimitiveType type, final ByteOrder byteOrder) | 
|  | 39 | + public static int getInt(final DirectBuffer buffer, final int index, final PrimitiveType type, final ByteOrder byteOrder) | 
| 39 | 40 |  { | 
| 40 | 41 |  switch (type) | 
| 41 | 42 |  { | 
| 42 | 43 |  case INT8: | 
| 43 |  | - return CodecUtil.int8Get(buffer, bufferIndex); | 
|  | 44 | + return buffer.getByte(index); | 
| 44 | 45 | 
 | 
| 45 | 46 |  case UINT8: | 
| 46 |  | - return CodecUtil.uint8Get(buffer, bufferIndex); | 
|  | 47 | + return (short)(buffer.getByte(index) & 0xFF); | 
| 47 | 48 | 
 | 
| 48 | 49 |  case INT16: | 
| 49 |  | - return CodecUtil.int16Get(buffer, bufferIndex, byteOrder); | 
|  | 50 | + return buffer.getShort(index, byteOrder); | 
| 50 | 51 | 
 | 
| 51 | 52 |  case UINT16: | 
| 52 |  | - return CodecUtil.uint16Get(buffer, bufferIndex, byteOrder); | 
|  | 53 | + return buffer.getShort(index, byteOrder) & 0xFFFF; | 
| 53 | 54 | 
 | 
| 54 | 55 |  case INT32: | 
| 55 |  | - return CodecUtil.int32Get(buffer, bufferIndex, byteOrder); | 
|  | 56 | + return buffer.getInt(index, byteOrder); | 
| 56 | 57 | 
 | 
| 57 | 58 |  default: | 
| 58 | 59 |  throw new IllegalArgumentException("Unsupported type: " + type); | 
| 59 | 60 |  } | 
| 60 | 61 |  } | 
|  | 62 | + | 
|  | 63 | + /** | 
|  | 64 | + * Get a long value from a buffer at a given index for a given {@link Encoding}. | 
|  | 65 | + * | 
|  | 66 | + * @param buffer from which to read. | 
|  | 67 | + * @param index at which he integer should be read. | 
|  | 68 | + * @param encoding of the value. | 
|  | 69 | + * @return the value of the encoded long. | 
|  | 70 | + */ | 
|  | 71 | + public static long getLong(final DirectBuffer buffer, final int index, final Encoding encoding) | 
|  | 72 | + { | 
|  | 73 | + switch (encoding.primitiveType()) | 
|  | 74 | + { | 
|  | 75 | + case CHAR: | 
|  | 76 | + return buffer.getByte(index); | 
|  | 77 | + | 
|  | 78 | + case INT8: | 
|  | 79 | + return buffer.getByte(index); | 
|  | 80 | + | 
|  | 81 | + case INT16: | 
|  | 82 | + return buffer.getShort(index, encoding.byteOrder()); | 
|  | 83 | + | 
|  | 84 | + case INT32: | 
|  | 85 | + return buffer.getInt(index, encoding.byteOrder()); | 
|  | 86 | + | 
|  | 87 | + case INT64: | 
|  | 88 | + return buffer.getLong(index, encoding.byteOrder()); | 
|  | 89 | + | 
|  | 90 | + case UINT8: | 
|  | 91 | + return (short)(buffer.getByte(index) & 0xFF); | 
|  | 92 | + | 
|  | 93 | + case UINT16: | 
|  | 94 | + return buffer.getShort(index, encoding.byteOrder()) & 0xFFFF; | 
|  | 95 | + | 
|  | 96 | + case UINT32: | 
|  | 97 | + return buffer.getInt(index, encoding.byteOrder()) & 0xFFFF_FFFFL; | 
|  | 98 | + | 
|  | 99 | + case UINT64: | 
|  | 100 | + return buffer.getLong(index, encoding.byteOrder()); | 
|  | 101 | + | 
|  | 102 | + default: | 
|  | 103 | + throw new IllegalArgumentException("Unsupported type for long: " + encoding.primitiveType()); | 
|  | 104 | + } | 
|  | 105 | + } | 
| 61 | 106 | } | 
0 commit comments