1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-11-27 04:34:42 +01:00
libtgvoip/BufferOutputStream.cpp
Grishka 697e250727 Finished moving things around, all classes are now in tgvoip
Replaced condition variables with semaphores
Audio device enumeration & selection on OS X and Windows
2017-04-28 14:17:56 +03:00

87 lines
2.1 KiB
C++

//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#include "BufferOutputStream.h"
#include <string.h>
using namespace tgvoip;
BufferOutputStream::BufferOutputStream(size_t size){
buffer=(unsigned char*) malloc(size);
offset=0;
this->size=size;
}
BufferOutputStream::~BufferOutputStream(){
free(buffer);
}
void BufferOutputStream::WriteByte(unsigned char byte){
this->ExpandBufferIfNeeded(1);
buffer[offset++]=byte;
}
void BufferOutputStream::WriteInt32(int32_t i){
this->ExpandBufferIfNeeded(4);
buffer[offset+3]=(unsigned char)((i >> 24) & 0xFF);
buffer[offset+2]=(unsigned char)((i >> 16) & 0xFF);
buffer[offset+1]=(unsigned char)((i >> 8) & 0xFF);
buffer[offset]=(unsigned char)(i & 0xFF);
offset+=4;
}
void BufferOutputStream::WriteInt64(int64_t i){
this->ExpandBufferIfNeeded(8);
buffer[offset+7]=(unsigned char)((i >> 56) & 0xFF);
buffer[offset+6]=(unsigned char)((i >> 48) & 0xFF);
buffer[offset+5]=(unsigned char)((i >> 40) & 0xFF);
buffer[offset+4]=(unsigned char)((i >> 32) & 0xFF);
buffer[offset+3]=(unsigned char)((i >> 24) & 0xFF);
buffer[offset+2]=(unsigned char)((i >> 16) & 0xFF);
buffer[offset+1]=(unsigned char)((i >> 8) & 0xFF);
buffer[offset]=(unsigned char)(i & 0xFF);
offset+=8;
}
void BufferOutputStream::WriteInt16(int16_t i){
this->ExpandBufferIfNeeded(2);
buffer[offset+1]=(unsigned char)((i >> 8) & 0xFF);
buffer[offset]=(unsigned char)(i & 0xFF);
offset+=2;
}
void BufferOutputStream::WriteBytes(unsigned char *bytes, size_t count){
this->ExpandBufferIfNeeded(count);
memcpy(buffer+offset, bytes, count);
offset+=count;
}
unsigned char *BufferOutputStream::GetBuffer(){
return buffer;
}
size_t BufferOutputStream::GetLength(){
return offset;
}
void BufferOutputStream::ExpandBufferIfNeeded(size_t need){
if(offset+need>size){
if(need<1024){
buffer=(unsigned char *) realloc(buffer, size+1024);
size+=1024;
}else{
buffer=(unsigned char *) realloc(buffer, size+need);
size+=need;
}
}
}
void BufferOutputStream::Reset(){
offset=0;
}