11 #ifndef EVIO_6_0_BYTEBUFFER_H
12 #define EVIO_6_0_BYTEBUFFER_H
48 mutable size_t pos = 0;
51 mutable size_t lim = 0;
54 mutable ssize_t mrk = -1;
74 std::shared_ptr<uint8_t> buf =
nullptr;
80 bool isHostEndian =
false;
83 bool isLittleEndian =
false;
88 bool isMappedMemory =
false;
96 ByteBuffer(
char* byteArray,
size_t len,
bool isMappedMem =
false);
97 ByteBuffer(uint8_t* byteArray,
size_t len,
bool isMappedMem =
false);
110 static std::shared_ptr<ByteBuffer>
copyBuffer(
const std::shared_ptr<const ByteBuffer> & srcBuf);
111 void copyData(
const std::shared_ptr<const ByteBuffer> & srcBuf,
size_t pos,
size_t limit);
113 void copy(
const std::shared_ptr<const ByteBuffer> & srcBuf);
115 void expand(
size_t newSize);
122 uint8_t *
array()
const;
123 std::shared_ptr<uint8_t>
getData()
const;
128 size_t limit()
const;
141 std::shared_ptr<ByteBuffer> &
duplicate(std::shared_ptr<ByteBuffer> & destBuf);
144 std::shared_ptr<ByteBuffer> &
slice(std::shared_ptr<ByteBuffer> & destBuf);
145 std::shared_ptr<ByteBuffer>
slice();
150 const ByteBuffer &
getBytes(std::vector<uint8_t> & dst,
size_t offset,
size_t length)
const;
152 uint8_t
peek()
const;
154 uint8_t
getByte(
size_t index)
const;
157 wchar_t getChar(
size_t index)
const;
160 int16_t
getShort(
size_t index)
const;
165 int32_t
getInt(
size_t index)
const;
167 uint32_t
getUInt(
size_t index)
const;
170 int64_t
getLong(
size_t index)
const;
172 uint64_t
getULong(
size_t index)
const;
185 ByteBuffer &
put(
const std::vector<uint8_t> & src,
size_t offset,
size_t length);
209 void printBytes(
size_t offset,
size_t bytes, std::string
const & label);
215 template<
typename T> T read()
const {
216 T data = read<T>(pos);
222 template<
typename T> T read(
size_t index)
const {
223 if (index +
sizeof(T) <= lim) {
224 return *((T *) &(buf.get())[index + off]);
227 throw std::underflow_error(
"buffer underflow");
231 template<
typename T>
void write(T & data) {
232 size_t s =
sizeof(data);
234 if (lim < (pos + s)) {
236 throw std::overflow_error(
"buffer overflow");
238 memcpy((
void *) (&(buf.get())[pos + off]), (
void *) (&data), s);
244 template<
typename T>
void write(T & data,
size_t index) {
245 size_t s =
sizeof(data);
246 if ((index + s) > lim) {
247 throw std::overflow_error(
"buffer overflow");
250 memcpy((
void *) (&(buf.get())[index + off]), (
void *) (&data), s);
260 void write(uint8_t & data) {
261 if (lim < (pos + 1)) {
262 throw std::overflow_error(
"buffer overflow");
264 buf.get()[off + pos++] = data;
272 void write(
char & data) {
273 if (lim < (pos + 1)) {
274 throw std::overflow_error(
"buffer overflow");
276 buf.get()[off + pos++] = data;
279 void write(uint8_t & data,
size_t index) {
280 if ((index + 1) > lim) {
281 throw std::overflow_error(
"buffer overflow");
283 buf.get()[index + off] = data;
286 void write(
char & data,
size_t index) {
287 if ((index + 1) > lim) {
288 throw std::overflow_error(
"buffer overflow");
290 buf.get()[index + off] = data;
294 uint8_t read()
const {
295 if (pos + 1 <= lim) {
296 return buf.get()[pos + off];
298 throw std::underflow_error(
"buffer underflow");
301 uint8_t read(
size_t index)
const {
302 if (index + 1 <= lim) {
303 return buf.get()[index + off];
305 throw std::underflow_error(
"buffer underflow");
315 #endif //EVIO_6_0_BYTEBUFFER_H
ByteBuffer & putShort(uint16_t val)
Relative put method for writing a short value.
Definition: ByteBuffer.cpp:1525
This class is copied from one of the same name in the Java programming language.
Definition: ByteBuffer.h:42
void expand(size_t newSize)
This method expands the size of this buffer if it's less than the given size.
Definition: ByteBuffer.cpp:386
const ByteOrder & order() const
Get the byte order of the data.
Definition: ByteBuffer.cpp:466
bool isDirect() const
This method has no relevance to this C++ library.
Definition: ByteBuffer.cpp:436
ByteBuffer & rewind()
Rewinds this buffer.
Definition: ByteBuffer.cpp:576
ByteBuffer & put(const ByteBuffer &src)
Relative bulk put method.
Definition: ByteBuffer.cpp:1325
size_t remaining() const
Returns the number of bytes from the current position to the end of the data.
Definition: ByteBuffer.cpp:497
Numerical values associated with endian byte order.
Definition: ByteOrder.h:53
ByteBuffer & putLong(uint64_t val)
Relative put method for writing a long long value.
Definition: ByteBuffer.cpp:1615
ByteBuffer & operator=(ByteBuffer &&other) noexcept
Move assignment operator.
Definition: ByteBuffer.cpp:121
void printBytes(size_t offset, size_t bytes, std::string const &label)
This method prints out the desired number of data bytes starting from the given index without regard ...
Definition: ByteBuffer.cpp:1753
std::string toString() const
Obtain a string representation of the buffer.
Definition: ByteBuffer.cpp:1777
ByteBuffer & flip()
Flips this buffer.
Definition: ByteBuffer.cpp:549
uint32_t getUInt() const
Relative get method for reading an unsigned int value.
Definition: ByteBuffer.cpp:1115
ByteBuffer & putInt(uint32_t val)
Relative put method for writing an int value.
Definition: ByteBuffer.cpp:1570
bool equals(const ByteBuffer &other)
This method tests for data equivilancy.
Definition: ByteBuffer.cpp:411
bool operator!=(const ByteBuffer &rhs) noexcept
Inequality operator.
Definition: ByteBuffer.cpp:191
const ByteBuffer & getBytes(uint8_t *dst, size_t length) const
Relative bulk get method.
Definition: ByteBuffer.cpp:877
ByteBuffer & putDouble(double val)
Relative put method for writing a double value.
Definition: ByteBuffer.cpp:1705
std::shared_ptr< uint8_t > getData() const
Get a shared pointer to this buffer's backing array which contains the data.
Definition: ByteBuffer.cpp:483
size_t capacity() const
Returns the total available bytes in this buffer.
Definition: ByteBuffer.cpp:504
ByteBuffer & mark()
Sets the buffer's mark at its position.
Definition: ByteBuffer.cpp:525
bool hasRemaining() const
Returns whether there are bytes remaining between the position and limit, i.e.
Definition: ByteBuffer.cpp:452
static std::shared_ptr< ByteBuffer > copyBuffer(const std::shared_ptr< const ByteBuffer > &srcBuf)
Copy the given buffer into a new buffer which is accessed thru shared pointer.
Definition: ByteBuffer.cpp:257
size_t arrayOffset() const
Get the offset within this buffer's backing array of the first element of the buffer.
Definition: ByteBuffer.cpp:490
bool operator==(const ByteBuffer &rhs) noexcept
Equality operator.
Definition: ByteBuffer.cpp:180
std::shared_ptr< ByteBuffer > slice()
Creates a new byte buffer whose content is a shared subsequence of this buffer's content.
Definition: ByteBuffer.cpp:770
wchar_t getChar() const
Relative get method for reading a 16-bit unicode character value which is by nature unsigned...
Definition: ByteBuffer.cpp:961
int64_t getLong() const
Relative get method for reading a long long value.
Definition: ByteBuffer.cpp:1156
int32_t getInt() const
Relative get method for reading an int value.
Definition: ByteBuffer.cpp:1074
ByteBuffer()
Default constructor, size of 4096 bytes.
Definition: ByteBuffer.cpp:22
uint8_t & operator[](size_t index)
Subscript operator for absolute access to data bytes without limit check.
Definition: ByteBuffer.cpp:202
void copy(const ByteBuffer &srcBuf)
Copy data and everything else from arg.
Definition: ByteBuffer.cpp:222
size_t position() const
Returns the position of the buffer.
Definition: ByteBuffer.cpp:518
bool hasArray() const
Tells whether or not this buffer is backed by an array.
Definition: ByteBuffer.cpp:444
ByteBuffer & putChar(wchar_t val)
Relative put method for writing a wide char value.
Definition: ByteBuffer.cpp:1480
uint8_t peek() const
Relative get method.
Definition: ByteBuffer.cpp:926
ByteBuffer & reset()
Resets this buffer's position to the previously-marked position.
Definition: ByteBuffer.cpp:562
ByteBuffer & zero()
This method writes zeroes into the buffer memory (from pos = 0 to capacity).
Definition: ByteBuffer.cpp:319
ByteBuffer & putFloat(float val)
Relative put method for writing a float value.
Definition: ByteBuffer.cpp:1660
ByteBuffer & compact()
This method compacts this buffer.
Definition: ByteBuffer.cpp:339
uint8_t * array() const
Get a pointer to this buffer's backing array which contains the data.
Definition: ByteBuffer.cpp:475
uint8_t getByte() const
Relative get method.
Definition: ByteBuffer.cpp:936
uint16_t getUShort() const
Relative get method for reading an unsigned short value.
Definition: ByteBuffer.cpp:1032
double getDouble() const
Relative get method for reading a double value.
Definition: ByteBuffer.cpp:1273
std::shared_ptr< ByteBuffer > duplicate()
Returns a byte buffer that shares this buffer's content.
Definition: ByteBuffer.cpp:737
void copyData(const std::shared_ptr< const ByteBuffer > &srcBuf, size_t pos, size_t limit)
Copy the source buffer's data (position to limit) into this buffer starting at local position 0...
Definition: ByteBuffer.cpp:283
ByteBuffer & clear()
Clears this buffer.
Definition: ByteBuffer.cpp:536
size_t limit() const
Returns the limit, the position of the last valid data byte.
Definition: ByteBuffer.cpp:511
int16_t getShort() const
Relative get method for reading a short value.
Definition: ByteBuffer.cpp:989
float getFloat() const
Relative get method for reading a float value.
Definition: ByteBuffer.cpp:1238
uint64_t getULong() const
Relative get method for reading an unsigned long long value.
Definition: ByteBuffer.cpp:1197
static const ByteOrder ENDIAN_LOCAL
Local host's byte order.
Definition: ByteOrder.h:61
~ByteBuffer()
Destructor.
Definition: ByteBuffer.cpp:108
bool isReadOnly() const
Returns whether this buffer is read only.
Definition: ByteBuffer.cpp:459