1 #include "ByteBuffer.h"
4 ByteBuffer::ByteBuffer(Array<char> *array) :
9 void ByteBuffer::put(char c) {
10 buffer->set(offset++, c);
13 void ByteBuffer::putInt(int32_t l) {
14 buffer->set(offset++, (char)(l >> 24));
15 buffer->set(offset++, (char)((l >> 16) & 0xff));
16 buffer->set(offset++, (char)((l >> 8) & 0xff));
17 buffer->set(offset++, (char)(l & 0xff));
20 void ByteBuffer::putLong(int64_t l) {
21 buffer->set(offset++, (char)(l >> 56));
22 buffer->set(offset++, (char)((l >> 48) & 0xff));
23 buffer->set(offset++, (char)((l >> 40) & 0xff));
24 buffer->set(offset++, (char)((l >> 32) & 0xff));
25 buffer->set(offset++, (char)((l >> 24) & 0xff));
26 buffer->set(offset++, (char)((l >> 16) & 0xff));
27 buffer->set(offset++, (char)((l >> 8) & 0xff));
28 buffer->set(offset++, (char)(l & 0xff));
31 void ByteBuffer::put(Array<char> *array) {
32 memcpy(&buffer->internalArray()[offset], array->internalArray(), array->length());
33 offset += array->length();
36 int64_t ByteBuffer::getLong() {
37 char *array = &buffer->internalArray()[offset];
39 return (((int64_t)(unsigned char)array[0]) << 56) |
40 (((int64_t)(unsigned char)array[1]) << 48) |
41 (((int64_t)(unsigned char)array[2]) << 40) |
42 (((int64_t)(unsigned char)array[3]) << 32) |
43 (((int64_t)(unsigned char)array[4]) << 24) |
44 (((int64_t)(unsigned char)array[5]) << 16) |
45 (((int64_t)(unsigned char)array[6]) << 8) |
46 (((int64_t)(unsigned char)array[7]));
49 int32_t ByteBuffer::getInt() {
50 char *array = &buffer->internalArray()[offset];
52 return (((int32_t)(unsigned char)array[0]) << 24) |
53 (((int32_t)(unsigned char)array[1]) << 16) |
54 (((int32_t)(unsigned char)array[2]) << 8) |
55 (((int32_t)(unsigned char)array[3]));
58 char ByteBuffer::get() {
59 return buffer->get(offset++);
62 void ByteBuffer::get(Array<char> *array) {
63 memcpy(array->internalArray(), &buffer->internalArray()[offset], array->length());
64 offset += array->length();
67 void ByteBuffer::position(int32_t newPosition) {
71 Array<char> *ByteBuffer::array() {
75 ByteBuffer *ByteBuffer_wrap(Array<char> *array) {
76 return new ByteBuffer(array);
79 ByteBuffer *ByteBuffer_allocate(uint size) {
80 return new ByteBuffer(new Array<char>(size));