20#ifndef GEODE_DATAINPUT_H_
21#define GEODE_DATAINPUT_H_
29#include "ExceptionTypes.hpp"
30#include "internal/DSCode.hpp"
31#include "internal/geode_globals.hpp"
37#define _GEODE_CHECK_BUFFER_SIZE(x) _checkBufferSize(x, __LINE__)
47class SerializationRegistry;
49class DataInputInternal;
67 _GEODE_CHECK_BUFFER_SIZE(1);
75 _GEODE_CHECK_BUFFER_SIZE(1);
76 return *(m_buf++) == 1 ?
true :
false;
91 _GEODE_CHECK_BUFFER_SIZE(len);
92 std::memcpy(buffer, m_buf, len);
109 _GEODE_CHECK_BUFFER_SIZE(len);
110 std::memcpy(buffer, m_buf, len);
126 auto length = readArrayLength();
128 uint8_t* buffer =
nullptr;
130 _GEODE_CHECK_BUFFER_SIZE(length);
131 _GEODE_NEW(buffer, uint8_t[length]);
132 std::memcpy(buffer, m_buf, length);
149 auto length = readArrayLength();
151 int8_t* buffer =
nullptr;
153 _GEODE_CHECK_BUFFER_SIZE(length);
154 _GEODE_NEW(buffer, int8_t[length]);
155 std::memcpy(buffer, m_buf, length);
167 _GEODE_CHECK_BUFFER_SIZE(2);
168 return readInt16NoCheck();
175 _GEODE_CHECK_BUFFER_SIZE(4);
176 int32_t tmp = *(m_buf++);
177 tmp = (tmp << 8) | *(m_buf++);
178 tmp = (tmp << 8) | *(m_buf++);
179 tmp = (tmp << 8) | *(m_buf++);
187 _GEODE_CHECK_BUFFER_SIZE(8);
190 tmp = (tmp << 8) | *(m_buf++);
191 tmp = (tmp << 8) | *(m_buf++);
192 tmp = (tmp << 8) | *(m_buf++);
193 tmp = (tmp << 8) | *(m_buf++);
194 tmp = (tmp << 8) | *(m_buf++);
195 tmp = (tmp << 8) | *(m_buf++);
196 tmp = (tmp << 8) | *(m_buf++);
206 const uint8_t code = read();
210 int32_t result = code;
213 uint16_t val = readInt16();
215 }
else if (code == 0xFD) {
216 uint32_t val = readInt32();
236 const auto b = read();
237 result |=
static_cast<int64_t
>(b & 0x7F) << shift;
238 if ((b & 0x80) == 0) {
250 _GEODE_CHECK_BUFFER_SIZE(4);
251 union float_uint32_t {
263 _GEODE_CHECK_BUFFER_SIZE(8);
264 union double_uint64_t {
272 template <
class CharT = char,
class... Tail>
273 inline std::basic_string<CharT, Tail...> readUTF() {
274 std::basic_string<CharT, Tail...> value;
275 readJavaModifiedUtf8(value);
279 template <
class CharT = char,
class... Tail>
280 inline std::basic_string<CharT, Tail...> readString() {
281 std::basic_string<CharT, Tail...> value;
282 auto type =
static_cast<internal::DSCode
>(read());
283 if (type == internal::DSCode::CacheableString) {
284 readJavaModifiedUtf8(value);
285 }
else if (type == internal::DSCode::CacheableStringHuge) {
286 readUtf16Huge(value);
287 }
else if (type == internal::DSCode::CacheableASCIIString) {
289 }
else if (type == internal::DSCode::CacheableASCIIStringHuge) {
290 readAsciiHuge(value);
295 inline bool readNativeBool() {
297 return readBoolean();
300 inline int32_t readNativeInt32() {
305 inline std::shared_ptr<Serializable> readDirectObject(int8_t typeId = -1) {
306 return readObjectInternal(typeId);
315 return readObjectInternal();
323 ptr = readObjectInternal();
326 inline void readObject(
char16_t* value) { *value = readInt16(); }
328 inline void readObject(
bool* value) { *value = readBoolean(); }
330 inline void readObject(int8_t* value) { *value = read(); }
332 inline void readObject(int16_t* value) { *value = readInt16(); }
334 inline void readObject(int32_t* value) { *value = readInt32(); }
336 inline void readObject(int64_t* value) { *value = readInt64(); }
338 inline void readObject(
float* value) { *value = readFloat(); }
340 inline void readObject(
double* value) { *value = readDouble(); }
342 inline std::vector<char16_t> readCharArray() {
return readArray<char16_t>(); }
344 inline std::vector<bool> readBooleanArray() {
return readArray<bool>(); }
346 inline std::vector<int8_t> readByteArray() {
return readArray<int8_t>(); }
348 inline std::vector<int16_t> readShortArray() {
return readArray<int16_t>(); }
350 inline std::vector<int32_t> readIntArray() {
return readArray<int32_t>(); }
352 inline std::vector<int64_t> readLongArray() {
return readArray<int64_t>(); }
354 inline std::vector<float> readFloatArray() {
return readArray<float>(); }
356 inline std::vector<double> readDoubleArray() {
return readArray<double>(); }
358 inline std::vector<std::string> readStringArray() {
359 std::vector<std::string> value;
361 auto arrLen = readArrayLength();
363 value.reserve(arrLen);
364 for (
int i = 0; i < arrLen; i++) {
365 value.push_back(readString());
372 inline void readArrayOfByteArrays(int8_t*** arrayofBytearr,
373 int32_t& arrayLength,
374 int32_t** elementLength) {
375 auto arrLen = readArrayLength();
376 arrayLength = arrLen;
379 *arrayofBytearr =
nullptr;
383 int32_t* tmpLengtharr;
384 _GEODE_NEW(tmpArray, int8_t* [arrLen]);
385 _GEODE_NEW(tmpLengtharr, int32_t[arrLen]);
386 for (
int i = 0; i < arrLen; i++) {
387 readBytes(&tmpArray[i], &tmpLengtharr[i]);
389 *arrayofBytearr = tmpArray;
390 *elementLength = tmpLengtharr;
406 return (m_bufLength - getBytesRead());
416 inline void reset() { m_buf = m_bufHead; }
418 inline void setBuffer() {
419 m_buf = currentBufferPosition();
420 m_bufLength = getBytesRemaining();
423 inline void resetPdx(
size_t offset) { m_buf = m_bufHead + offset; }
425 inline size_t getPdxBytes()
const {
return m_bufLength; }
427 static uint8_t* getBufferCopy(
const uint8_t* from,
size_t length) {
429 _GEODE_NEW(result, uint8_t[length]);
430 std::memcpy(result, from, length);
435 inline void reset(
size_t offset) { m_buf = m_bufHead + offset; }
437 uint8_t* getBufferCopyFrom(
const uint8_t* from,
size_t length) {
439 _GEODE_NEW(result, uint8_t[length]);
440 std::memcpy(result, from, length);
445 virtual Cache* getCache()
const;
447 DataInput() =
delete;
448 virtual ~DataInput() noexcept = default;
449 DataInput(const DataInput&) = delete;
450 DataInput& operator=(const DataInput&) = delete;
451 DataInput(DataInput&&) = default;
452 DataInput& operator=(DataInput&&) = default;
456 DataInput(const uint8_t* buffer,
size_t len, const CacheImpl* cache,
459 virtual const SerializationRegistry& getSerializationRegistry() const;
462 const uint8_t* m_buf;
463 const uint8_t* m_bufHead;
466 const CacheImpl* m_cache;
468 std::shared_ptr<
Serializable> readObjectInternal(int8_t typeId = -1);
470 template <typename mType>
471 void readObject(mType** value, int32_t& length) {
472 auto arrayLen = readArrayLength();
476 objArray =
new mType[arrayLen];
478 for (i = 0; i < arrayLen; i++) {
487 template <
typename T>
488 std::vector<T> readArray() {
489 auto arrayLen = readArrayLength();
490 std::vector<T> objArray;
492 objArray.reserve(arrayLen);
494 for (i = 0; i < arrayLen; i++) {
497 objArray.push_back(tmp);
503 inline char readPdxChar() {
return static_cast<char>(readInt16()); }
505 inline void _checkBufferSize(
size_t size, int32_t line) {
506 if ((m_bufLength - (m_buf - m_bufHead)) < size) {
507 throw OutOfRangeException(
508 "DataInput: attempt to read beyond buffer at line " +
509 std::to_string(line) +
": available buffer size " +
510 std::to_string(m_bufLength - (m_buf - m_bufHead)) +
511 ", attempted read of size " + std::to_string(size));
515 inline int8_t readNoCheck() {
return *(m_buf++); }
517 inline int16_t readInt16NoCheck() {
518 int16_t tmp = *(m_buf++);
519 tmp =
static_cast<int16_t
>((tmp << 8) | *(m_buf++));
523 template <
class CharT,
class... Tail>
524 inline void readAscii(std::basic_string<CharT, Tail...>& value,
526 _GEODE_CHECK_BUFFER_SIZE(length);
527 value.reserve(length);
528 while (length-- > 0) {
530 value += readNoCheck() & 0x7F;
534 template <
class CharT,
class... Tail>
535 inline void readAscii(std::basic_string<CharT, Tail...>& value) {
536 readAscii(value,
static_cast<uint16_t
>(readInt16()));
539 template <
class CharT,
class... Tail>
540 inline void readAsciiHuge(std::basic_string<CharT, Tail...>& value) {
541 readAscii(value,
static_cast<uint32_t
>(readInt32()));
544 template <
class _CharT,
class _Traits,
class _Allocator>
545 void readJavaModifiedUtf8(
546 std::basic_string<_CharT, _Traits, _Allocator>& value);
548 template <
class _Traits,
class _Allocator>
549 void readJavaModifiedUtf8(
550 std::basic_string<char16_t, _Traits, _Allocator>& value);
552 template <
class _Traits,
class _Allocator>
553 void readJavaModifiedUtf8(
554 std::basic_string<char, _Traits, _Allocator>& value);
556 template <
class _Traits,
class _Allocator>
557 void readJavaModifiedUtf8(
558 std::basic_string<char32_t, _Traits, _Allocator>& value);
560 template <
class _Traits,
class _Allocator>
561 inline void readJavaModifiedUtf8(
562 std::basic_string<wchar_t, _Traits, _Allocator>& value) {
564 typedef std::conditional<
565 sizeof(wchar_t) ==
sizeof(
char16_t), char16_t,
566 std::conditional<
sizeof(wchar_t) ==
sizeof(
char32_t), char32_t,
567 char>::type>::type _WcharT;
569 auto tmp = std::basic_string<_WcharT>();
570 readJavaModifiedUtf8(tmp);
571 value.assign(
reinterpret_cast<const wchar_t*
>(tmp.data()), tmp.length());
574 template <
class _CharT,
class _Traits,
class _Allocator>
575 void readUtf16Huge(std::basic_string<_CharT, _Traits, _Allocator>& value);
577 template <
class _Traits,
class _Allocator>
578 inline void readUtf16Huge(
579 std::basic_string<char16_t, _Traits, _Allocator>& value) {
580 uint32_t length = readInt32();
581 _GEODE_CHECK_BUFFER_SIZE(length);
582 value.reserve(length);
583 while (length-- > 0) {
584 value += readInt16NoCheck();
588 template <
class _Traits,
class _Allocator>
589 void readUtf16Huge(std::basic_string<char, _Traits, _Allocator>& value);
591 template <
class _Traits,
class _Allocator>
592 void readUtf16Huge(std::basic_string<char32_t, _Traits, _Allocator>& value);
594 template <
class _Traits,
class _Allocator>
595 inline void readUtf16Huge(
596 std::basic_string<wchar_t, _Traits, _Allocator>& value) {
598 typedef std::conditional<
599 sizeof(wchar_t) ==
sizeof(
char16_t), char16_t,
600 std::conditional<
sizeof(wchar_t) ==
sizeof(
char32_t), char32_t,
601 char>::type>::type _WcharT;
603 auto tmp = std::basic_string<_WcharT>();
605 value.assign(
reinterpret_cast<const wchar_t*
>(tmp.data()), tmp.length());
608 Pool* getPool()
const {
return m_pool; }
612 friend DataInputInternal;
613 friend CacheableString;
Provide operations for reading primitive data values, byte arrays, strings, Serializable objects from...
Definition: DataInput.hpp:59
void readBytes(uint8_t **bytes, int32_t *len)
Read an array of unsigned bytes from the DataInput expecting to find the length of array in the strea...
Definition: DataInput.hpp:125
void rewindCursor(size_t offset)
rewind the cursor by given offset
Definition: DataInput.hpp:413
void readBytesOnly(int8_t *buffer, size_t len)
Read the given number of signed bytes from the DataInput.
Definition: DataInput.hpp:107
int64_t readUnsignedVL()
Decode a 64 bit integer as a variable length array.
Definition: DataInput.hpp:232
void readBytesOnly(uint8_t *buffer, size_t len)
Read the given number of unsigned bytes from the DataInput.
Definition: DataInput.hpp:89
int32_t readArrayLength()
Read a 32-bit signed integer array length value from the DataInput in a manner compatible with java s...
Definition: DataInput.hpp:205
void readBytes(int8_t **bytes, int32_t *len)
Read an array of signed bytes from the DataInput expecting to find the length of array in the stream ...
Definition: DataInput.hpp:148
int64_t readInt64()
Read a 64-bit signed integer from the DataInput.
Definition: DataInput.hpp:186
int16_t readInt16()
Read a 16-bit signed integer from the DataInput.
Definition: DataInput.hpp:166
bool readBoolean()
Read a boolean value from the DataInput.
Definition: DataInput.hpp:74
const uint8_t * currentBufferPosition() const
Get the pointer to current buffer position.
Definition: DataInput.hpp:399
void advanceCursor(size_t offset)
advance the cursor by given offset
Definition: DataInput.hpp:410
size_t getBytesRead() const
get the number of bytes read in the buffer
Definition: DataInput.hpp:402
double readDouble()
Read a double precision number from the DataInput.
Definition: DataInput.hpp:262
void readObject(std::shared_ptr< Serializable > &ptr)
Read a Serializable object from the DataInput.
Definition: DataInput.hpp:322
std::shared_ptr< Serializable > readObject()
Read a Serializable object from the DataInput.
Definition: DataInput.hpp:314
size_t getBytesRemaining() const
get the number of bytes remaining to be read in the buffer
Definition: DataInput.hpp:405
int8_t read()
Read a signed byte from the DataInput.
Definition: DataInput.hpp:66
float readFloat()
Read a float from the DataInput.
Definition: DataInput.hpp:249
void reset()
reset the cursor to the start of buffer
Definition: DataInput.hpp:416
int32_t readInt32()
Read a 32-bit signed integer from the DataInput.g.
Definition: DataInput.hpp:174
Thrown when the state of cache is manipulated to be illegal.
Definition: ExceptionTypes.hpp:64
A pool of connections to connect from a client to a set of Geode Cache Servers.
Definition: Pool.hpp:59
This base class is the superclass of all user objects in the cache that can be serialized.
Definition: Serializable.hpp:53