| 6 |
| * Copyright Nikolai Kudashov, 2015-2018. |
| 7 | */ |
| 8 |
| 9 | #ifndef NATIVEBYTEBUFFER_H |
| 10 | #define NATIVEBYTEBUFFER_H |
| 11 |
| 12 | #include <stdint.h> |
| 13 | #include <string> |
| 14 |
| 15 | #ifdef ANDROID |
| 16 | #include <jni.h> |
| 17 | #endif |
| 18 |
| 19 | class ByteArray; |
| 20 |
| 21 | class NativeByteBuffer { |
| 22 |
| 23 | public: |
| 24 | NativeByteBuffer(uint32_t size); |
| 25 | NativeByteBuffer(bool calculate); |
| 26 | NativeByteBuffer(uint8_t *buff, uint32_t length); |
| 27 | ~NativeByteBuffer(); |
| 28 |
| 29 | uint32_t position(); |
| 30 | void position(uint32_t position); |
| 31 | uint32_t limit(); |
| 32 | void limit(uint32_t limit); |
| 33 | uint32_t capacity(); |
| 34 | uint32_t remaining(); |
| 35 | bool hasRemaining(); |
| 36 | void rewind(); |
| 37 | void compact(); |
| 38 | void flip(); |
| 39 | void clear(); |
| 40 | void skip(uint32_t length); |
| 41 | void clearCapacity(); |
| 42 | uint8_t *bytes(); |
| 43 |
| 44 | void writeInt32(int32_t x, bool *error); |
| 45 | void writeInt64(int64_t x, bool *error); |
| 46 | void writeBool(bool value, bool *error); |
| 47 | void writeBytes(uint8_t *b, uint32_t length, bool *error); |
| 48 | void writeBytes(uint8_t *b, uint32_t offset, uint32_t length, bool *error); |
| 49 | void writeBytes(ByteArray *b, bool *error); |
| 50 | void writeBytes(NativeByteBuffer *b, bool *error); |
| 51 | void writeByte(uint8_t i, bool *error); |
| 52 | void writeString(std::string s, bool *error); |
| 53 | void writeByteArray(uint8_t *b, uint32_t offset, uint32_t length, bool *error); |
| 54 | void writeByteArray(uint8_t *b, uint32_t length, bool *error); |
| 55 | void writeByteArray(NativeByteBuffer *b, bool *error); |
| 56 | void writeByteArray(ByteArray *b, bool *error); |
| 57 | void writeDouble(double d, bool *error); |
| 58 | void writeInt32(int32_t x); |
| 59 | void writeInt64(int64_t x); |
| 60 | void writeBool(bool value); |
| 61 | void writeBytes(uint8_t *b, uint32_t length); |
| 62 | void writeBytes(uint8_t *b, uint32_t offset, uint32_t length); |
| 63 | void writeBytes(ByteArray *b); |
| 64 | void writeBytes(NativeByteBuffer *b); |
| 65 | void writeByte(uint8_t i); |
| 66 | void writeString(std::string s); |
| 67 | void writeByteArray(uint8_t *b, uint32_t offset, uint32_t length); |
| 68 | void writeByteArray(uint8_t *b, uint32_t length); |
| 69 | void writeByteArray(NativeByteBuffer *b); |
| 70 | void writeByteArray(ByteArray *b); |
| 71 | void writeDouble(double d); |
| 72 |
| 73 | uint32_t readUint32(bool *error); |
| 74 | uint64_t readUint64(bool *error); |
| 75 | int32_t readInt32(bool *error); |
| 76 | int32_t readBigInt32(bool *error); |
| 77 | int64_t readInt64(bool *error); |
| 78 | uint8_t readByte(bool *error); |
| 79 | bool readBool(bool *error); |
| 80 | void readBytes(uint8_t *b, uint32_t length, bool *error); |
| 81 | ByteArray *readBytes(uint32_t length, bool *error); |
| 82 | std::string readString(bool *error); |
| 83 | ByteArray *readByteArray(bool *error); |
| 84 | NativeByteBuffer *readByteBuffer(bool copy, bool *error); |
| 85 | double readDouble(bool *error); |
| 86 |
| 87 | void reuse(); |
| 88 | #ifdef ANDROID |
| 89 | jobject getJavaByteBuffer(); |
| 90 | #endif |
| 91 |
| 92 | private: |
| 93 | void writeBytesInternal(uint8_t *b, uint32_t offset, uint32_t length); |
| 94 |
| 95 | uint8_t *buffer = nullptr; |
| 96 | bool calculateSizeOnly = false; |
| 97 | bool sliced = false; |
| 98 | uint32_t _position = 0; |
| 99 | uint32_t _limit = 0; |
| 100 | uint32_t _capacity = 0; |
| 101 | bool bufferOwner = true; |
| 102 | #ifdef ANDROID |
| 103 | jobject javaByteBuffer = nullptr; |
| 104 | #endif |
| 105 | }; |
| 106 |
| 107 | #endif |
| 108 |