1 /**
2    ByteBuffer
3    ByteBuffer.h
4    Copyright 2011 - 2013 Ramsey Kant
5 
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9 
10        http://www.apache.org/licenses/LICENSE-2.0
11 
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 */
18 
19 #ifndef _ByteBuffer_H_
20 #define _ByteBuffer_H_
21 
22 // Default number of uint8_ts to allocate in the backing buffer if no size is provided
23 #define DEFAULT_SIZE 4096
24 
25 #include <string.h>
26 #include <stdint.h>
27 #include <vector>
28 
29 class ByteBuffer {
30 private:
31 	uint32_t rpos, wpos;
32 	std::vector<uint8_t> buf;
33 
read()34 	template <typename T> T read() {
35 		T data = read<T>(rpos);
36 		rpos += sizeof(T);
37 		return data;
38 	}
39 
read(uint32_t index)40 	template <typename T> T read(uint32_t index) const {
41 		if(index + sizeof(T) <= buf.size())
42 			return *((T*)&buf[index]);
43 		return 0;
44 	}
45 
append(T data)46 	template <typename T> void append(T data) {
47 		uint32_t s = sizeof(data);
48 
49 		if (size() < (wpos + s))
50 			buf.resize(wpos + s);
51 		memcpy(&buf[wpos], (uint8_t*)&data, s);
52 
53 		wpos += s;
54 	}
55 
insert(T data,uint32_t index)56 	template <typename T> void insert(T data, uint32_t index) {
57 		if((index + sizeof(data)) > size())
58 			return;
59 
60 		memcpy(&buf[index], (uint8_t*)&data, sizeof(data));
61 		wpos = index+sizeof(data);
62 	}
63 
64 public:
65 	ByteBuffer(uint32_t size = DEFAULT_SIZE);
66 	ByteBuffer(uint8_t* arr, uint32_t size);
67 	virtual ~ByteBuffer();
68 
69 	uint32_t bytesRemaining(); // Number of uint8_ts from the current read position till the end of the buffer
70 	void clear(); // Clear our the vector and reset read and write positions
71 	void free();
72 	ByteBuffer* clone(); // Return a new instance of a ByteBuffer with the exact same contents and the same state (rpos, wpos)
73 	bool equals(ByteBuffer* other); // Compare if the contents are equivalent
74 	void resize(uint32_t newSize);
75 	uint32_t size(); // Size of internal vector
76 
77 	// Read
78 
79 	uint8_t peek(); // Relative peek. Reads and returns the next uint8_t in the buffer from the current position but does not increment the read position
80 	uint8_t get(); // Relative get method. Reads the uint8_t at the buffers current position then increments the position
81 	uint8_t get(uint32_t index); // Absolute get method. Read uint8_t at index
82 	void getBytes(uint8_t* buf, uint32_t len); // Absolute read into array buf of length len
83 	char getChar(); // Relative
84 	char getChar(uint32_t index); // Absolute
85 	double getDouble();
86 	double getDouble(uint32_t index);
87 	float getFloat();
88 	float getFloat(uint32_t index);
89 	uint32_t getInt();
90 	uint32_t getInt(uint32_t index);
91 	uint64_t getLong();
92 	uint64_t getLong(uint32_t index);
93 	uint16_t getShort();
94 	uint16_t getShort(uint32_t index);
95 
96 	// Write
97 
98 	void put(ByteBuffer* src); // Relative write of the entire contents of another ByteBuffer (src)
99 	void put(uint8_t b); // Relative write
100 	void put(uint8_t b, uint32_t index); // Absolute write at index
101 	void putBytes(uint8_t* b, uint32_t len); // Relative write
102 	void putBytes(uint8_t* b, uint32_t len, uint32_t index); // Absolute write starting at index
103 	void putChar(char value); // Relative
104 	void putChar(char value, uint32_t index); // Absolute
105 	void putDouble(double value);
106 	void putDouble(double value, uint32_t index);
107 	void putFloat(float value);
108 	void putFloat(float value, uint32_t index);
109 	void putInt(uint32_t value);
110 	void putInt(uint32_t value, uint32_t index);
111 	void putLong(uint64_t value);
112 	void putLong(uint64_t value, uint32_t index);
113 	void putShort(uint16_t value);
114 	void putShort(uint16_t value, uint32_t index);
115 
116 	// Buffer Position Accessors & Mutators
117 
setReadPos(uint32_t r)118 	void setReadPos(uint32_t r) { rpos = r; }
getReadPos()119 	uint32_t getReadPos() const { return rpos; }
120 
setWritePos(uint32_t w)121 	void setWritePos(uint32_t w) { wpos = w; }
getWritePos()122 	uint32_t getWritePos() const{ return wpos; }
123 };
124 
125 #endif
126