|
| 1 | +/** |
| 2 | + ByteBuffer |
| 3 | + ByteBuffer.cpp |
| 4 | + Copyright 2011 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 | +#include "ByteBuffer.h" |
| 20 | + |
| 21 | +/** |
| 22 | + * ByteBuffer constructor |
| 23 | + * Reserves specified size in internal vector |
| 24 | + * |
| 25 | + * @param size Size (in bytes) of space to preallocate internally. Default is set in DEFAULT_SIZE |
| 26 | + */ |
| 27 | +ByteBuffer::ByteBuffer(unsigned int size) { |
| 28 | + buf.reserve(size); |
| 29 | + clear(); |
| 30 | +#ifdef BB_UTILITY |
| 31 | + name = ""; |
| 32 | +#endif |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * ByteBuffer constructor |
| 37 | + * Consume an entire byte array of length len in the ByteBuffer |
| 38 | + * |
| 39 | + * @param arr byte array of data (should be of length len) |
| 40 | + * @param size Size of space to allocate |
| 41 | + */ |
| 42 | +ByteBuffer::ByteBuffer(byte* arr, unsigned int size) { |
| 43 | + // If the provided array is NULL, allocate a blank buffer of the provided size |
| 44 | + if(arr == NULL) { |
| 45 | + buf.reserve(size); |
| 46 | + clear(); |
| 47 | + } else { // Consume the provided array |
| 48 | + buf.reserve(size); |
| 49 | + clear(); |
| 50 | + putBytes(arr, size); |
| 51 | + } |
| 52 | + |
| 53 | +#ifdef BB_UTILITY |
| 54 | + name = ""; |
| 55 | +#endif |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * ByteBuffer Deconstructor |
| 60 | + * |
| 61 | + */ |
| 62 | +ByteBuffer::~ByteBuffer() { |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Bytes Remaining |
| 67 | + * Returns the number of bytes from the current read position till the end of the buffer |
| 68 | + * |
| 69 | + * @return Number of bytes from rpos to the end (size()) |
| 70 | + */ |
| 71 | +unsigned int ByteBuffer::bytesRemaining() { |
| 72 | + return size()-rpos; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Clear |
| 77 | + * Clears out all data from the internal vector (original preallocated size remains), resets the positions to 0 |
| 78 | + */ |
| 79 | +void ByteBuffer::clear() { |
| 80 | + rpos = 0; |
| 81 | + wpos = 0; |
| 82 | + buf.clear(); |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Clone |
| 87 | + * Allocate an exact copy of the ByteBuffer on the heap and return a pointer |
| 88 | + * |
| 89 | + * @return A pointer to the newly cloned ByteBuffer. NULL if no more memory available |
| 90 | + */ |
| 91 | +ByteBuffer* ByteBuffer::clone() { |
| 92 | + ByteBuffer* ret = new ByteBuffer(buf.size()); |
| 93 | + |
| 94 | + // Copy data |
| 95 | + for(unsigned int i = 0; i < buf.size(); i++) { |
| 96 | + ret->put(i, (byte)get(i)); |
| 97 | + } |
| 98 | + |
| 99 | + // Reset positions |
| 100 | + ret->setReadPos(0); |
| 101 | + ret->setWritePos(0); |
| 102 | + |
| 103 | + return ret; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Equals, test for data equivilancy |
| 108 | + * Compare this ByteBuffer to another by looking at each byte in the internal buffers and making sure they are the same |
| 109 | + * |
| 110 | + * @param other A pointer to a ByteBuffer to compare to this one |
| 111 | + * @return True if the internal buffers match. False if otherwise |
| 112 | + */ |
| 113 | +bool ByteBuffer::equals(ByteBuffer* other) { |
| 114 | + // If sizes aren't equal, they can't be equal |
| 115 | + if(size() != other->size()) |
| 116 | + return false; |
| 117 | + |
| 118 | + // Compare byte by byte |
| 119 | + unsigned int len = size(); |
| 120 | + for(unsigned int i = 0; i < len; i++) { |
| 121 | + if((byte)get(i) != (byte)other->get(i)) |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + return true; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Resize |
| 130 | + * Reallocates memory for the internal buffer of size newSize. Read and write positions will also be reset |
| 131 | + * |
| 132 | + * @param newSize The amount of memory to allocate |
| 133 | + */ |
| 134 | +void ByteBuffer::resize(unsigned int newSize) { |
| 135 | + buf.resize(newSize); |
| 136 | + rpos = 0; |
| 137 | + wpos = 0; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Size |
| 142 | + * Returns the size of the internal buffer...not necessarily the length of bytes used as data! |
| 143 | + * |
| 144 | + * @return size of the internal buffer |
| 145 | + */ |
| 146 | +unsigned int ByteBuffer::size() { |
| 147 | + return buf.size(); |
| 148 | +} |
| 149 | + |
| 150 | +// Replacement |
| 151 | + |
| 152 | +/** |
| 153 | + * Replace |
| 154 | + * Replace occurance of a particular byte, key, with the byte rep |
| 155 | + * |
| 156 | + * @param key Byte to find for replacement |
| 157 | + * @param rep Byte to replace the found key with |
| 158 | + * @param start Index to start from. By default, start is 0 |
| 159 | + * @param firstOccuranceOnly If true, only replace the first occurance of the key. If false, replace all occurances. False by default |
| 160 | + */ |
| 161 | +void ByteBuffer::replace(byte key, byte rep, unsigned int start, bool firstOccuranceOnly) { |
| 162 | + unsigned int len = buf.size(); |
| 163 | + for(unsigned int i = start; i < len; i++) { |
| 164 | + byte data = read<byte>(i); |
| 165 | + // Wasn't actually found, bounds of buffer were exceeded |
| 166 | + if((key != 0) && (data == 0)) |
| 167 | + break; |
| 168 | + |
| 169 | + // Key was found in array, perform replacement |
| 170 | + if(data == key) { |
| 171 | + buf[i] = rep; |
| 172 | + if(firstOccuranceOnly) |
| 173 | + return; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +// Read Functions |
| 179 | + |
| 180 | +byte ByteBuffer::peek() { |
| 181 | + return read<byte>(rpos); |
| 182 | +} |
| 183 | + |
| 184 | +byte ByteBuffer::get() { |
| 185 | + return read<byte>(); |
| 186 | +} |
| 187 | + |
| 188 | +byte ByteBuffer::get(unsigned int index) { |
| 189 | + return read<byte>(index); |
| 190 | +} |
| 191 | + |
| 192 | +void ByteBuffer::getBytes(byte* buf, unsigned int len) { |
| 193 | + for(unsigned int i = 0; i < len; i++) { |
| 194 | + buf[i] = read<byte>(); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +char ByteBuffer::getChar() { |
| 199 | + return read<char>(); |
| 200 | +} |
| 201 | + |
| 202 | +char ByteBuffer::getChar(unsigned int index) { |
| 203 | + return read<char>(index); |
| 204 | +} |
| 205 | + |
| 206 | +double ByteBuffer::getDouble() { |
| 207 | + return read<double>(); |
| 208 | +} |
| 209 | + |
| 210 | +double ByteBuffer::getDouble(unsigned int index) { |
| 211 | + return read<double>(index); |
| 212 | +} |
| 213 | + |
| 214 | +float ByteBuffer::getFloat() { |
| 215 | + return read<float>(); |
| 216 | +} |
| 217 | + |
| 218 | +float ByteBuffer::getFloat(unsigned int index) { |
| 219 | + return read<float>(index); |
| 220 | +} |
| 221 | + |
| 222 | +int ByteBuffer::getInt() { |
| 223 | + return read<int>(); |
| 224 | +} |
| 225 | + |
| 226 | +int ByteBuffer::getInt(unsigned int index) { |
| 227 | + return read<int>(index); |
| 228 | +} |
| 229 | + |
| 230 | +long ByteBuffer::getLong() { |
| 231 | + return read<long>(); |
| 232 | +} |
| 233 | + |
| 234 | +long ByteBuffer::getLong(unsigned int index) { |
| 235 | + return read<long>(index); |
| 236 | +} |
| 237 | + |
| 238 | +short ByteBuffer::getShort() { |
| 239 | + return read<short>(); |
| 240 | +} |
| 241 | + |
| 242 | +short ByteBuffer::getShort(unsigned int index) { |
| 243 | + return read<short>(index); |
| 244 | +} |
| 245 | + |
| 246 | + |
| 247 | +// Write Functions |
| 248 | + |
| 249 | +void ByteBuffer::put(ByteBuffer* src) { |
| 250 | + int len = src->size(); |
| 251 | + for(int i = 0; i < len; i++) |
| 252 | + append<byte>(src->get(i)); |
| 253 | +} |
| 254 | + |
| 255 | +void ByteBuffer::put(byte b) { |
| 256 | + append<byte>(b); |
| 257 | +} |
| 258 | + |
| 259 | +void ByteBuffer::put(byte b, unsigned int index) { |
| 260 | + insert<byte>(b, index); |
| 261 | +} |
| 262 | + |
| 263 | +void ByteBuffer::putBytes(byte* b, unsigned int len) { |
| 264 | + // Insert the data one byte at a time into the internal buffer at position i+starting index |
| 265 | + for(unsigned int i = 0; i < len; i++) |
| 266 | + append<byte>(b[i]); |
| 267 | +} |
| 268 | + |
| 269 | +void ByteBuffer::putBytes(byte* b, unsigned int len, unsigned int index) { |
| 270 | + wpos = index; |
| 271 | + |
| 272 | + // Insert the data one byte at a time into the internal buffer at position i+starting index |
| 273 | + for(unsigned int i = 0; i < len; i++) |
| 274 | + append<byte>(b[i]); |
| 275 | +} |
| 276 | + |
| 277 | +void ByteBuffer::putChar(char value) { |
| 278 | + append<char>(value); |
| 279 | +} |
| 280 | + |
| 281 | +void ByteBuffer::putChar(char value, unsigned int index) { |
| 282 | + insert<char>(value, index); |
| 283 | +} |
| 284 | + |
| 285 | +void ByteBuffer::putDouble(double value) { |
| 286 | + append<double>(value); |
| 287 | +} |
| 288 | + |
| 289 | +void ByteBuffer::putDouble(double value, unsigned int index) { |
| 290 | + insert<double>(value, index); |
| 291 | +} |
| 292 | +void ByteBuffer::putFloat(float value) { |
| 293 | + append<float>(value); |
| 294 | +} |
| 295 | + |
| 296 | +void ByteBuffer::putFloat(float value, unsigned int index) { |
| 297 | + insert<float>(value, index); |
| 298 | +} |
| 299 | + |
| 300 | +void ByteBuffer::putInt(int value) { |
| 301 | + append<int>(value); |
| 302 | +} |
| 303 | + |
| 304 | +void ByteBuffer::putInt(int value, unsigned int index) { |
| 305 | + insert<int>(value, index); |
| 306 | +} |
| 307 | + |
| 308 | +void ByteBuffer::putLong(long value) { |
| 309 | + append<long>(value); |
| 310 | +} |
| 311 | + |
| 312 | +void ByteBuffer::putLong(long value, unsigned int index) { |
| 313 | + insert<long>(value, index); |
| 314 | +} |
| 315 | + |
| 316 | +void ByteBuffer::putShort(short value) { |
| 317 | + append<short>(value); |
| 318 | +} |
| 319 | + |
| 320 | +void ByteBuffer::putShort(short value, unsigned int index) { |
| 321 | + insert<short>(value, index); |
| 322 | +} |
| 323 | + |
| 324 | +// Utility Functions |
| 325 | +#ifdef BB_UTILITY |
| 326 | +void ByteBuffer::setName(std::string n) { |
| 327 | + name = n; |
| 328 | +} |
| 329 | + |
| 330 | +std::string ByteBuffer::getName() { |
| 331 | + return name; |
| 332 | +} |
| 333 | + |
| 334 | +void ByteBuffer::printInfo() { |
| 335 | + unsigned int length = buf.size(); |
| 336 | + std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Info Print" << std::endl; |
| 337 | +} |
| 338 | + |
| 339 | +void ByteBuffer::printAH() { |
| 340 | + unsigned int length = buf.size(); |
| 341 | + std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII & Hex Print" << std::endl; |
| 342 | + for(unsigned int i = 0; i < length; i++) { |
| 343 | + printf("0x%02x ", buf[i]); |
| 344 | + } |
| 345 | + printf("\n"); |
| 346 | + for(unsigned int i = 0; i < length; i++) { |
| 347 | + printf("%c ", buf[i]); |
| 348 | + } |
| 349 | + printf("\n"); |
| 350 | +} |
| 351 | + |
| 352 | +void ByteBuffer::printAscii() { |
| 353 | + unsigned int length = buf.size(); |
| 354 | + std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII Print" << std::endl; |
| 355 | + for(unsigned int i = 0; i < length; i++) { |
| 356 | + printf("%c ", buf[i]); |
| 357 | + } |
| 358 | + printf("\n"); |
| 359 | +} |
| 360 | + |
| 361 | +void ByteBuffer::printHex() { |
| 362 | + unsigned int length = buf.size(); |
| 363 | + std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Hex Print" << std::endl; |
| 364 | + for(unsigned int i = 0; i < length; i++) { |
| 365 | + printf("0x%02x ", buf[i]); |
| 366 | + } |
| 367 | + printf("\n"); |
| 368 | +} |
| 369 | + |
| 370 | +void ByteBuffer::printPosition() { |
| 371 | + unsigned int length = buf.size(); |
| 372 | + std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << " Read Pos: " << rpos << ". Write Pos: " << wpos << std::endl; |
| 373 | +} |
| 374 | +#endif |
0 commit comments