00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "LampBasic.h"
00026 #include "Core/InputOutput/MemoryOutputStream.h"
00027
00028 namespace Lamp{
00029
00030
00031
00032 MemoryOutputStream::MemoryOutputStream(int bufferInitialSize){
00033 bufferSize_ = bufferInitialSize;
00034 buffer_ = new char[bufferSize_];
00035 size_ = position_ = 0;
00036 }
00037
00038
00039 MemoryOutputStream::~MemoryOutputStream(){
00040 delete[] buffer_;
00041 }
00042
00043
00044 void MemoryOutputStream::bufferCheck(int size){
00045 int newPosition = position_ + size;
00046 if(newPosition > bufferSize_){
00047 while(true){
00048 bufferSize_ *= 2;
00049 if(newPosition <= bufferSize_){ break; }
00050 }
00051 char* newBuffer = new char[bufferSize_];
00052 std::memcpy(newBuffer, buffer_, size_);
00053 delete[] buffer_;
00054 buffer_ = newBuffer;
00055 }
00056 }
00057
00058
00059 void MemoryOutputStream::writeBytes(const void* data, int size){
00060 bufferCheck(size);
00061 std::memcpy(buffer_ + position_, data, size);
00062 position_ += size;
00063 if(position_ > size_){ size_ = position_; }
00064 }
00065
00066
00067 int MemoryOutputStream::getSize(){
00068 return size_;
00069 }
00070
00071
00072 void MemoryOutputStream::skip(int size){
00073 bufferCheck(size);
00074 std::memset(buffer_ + position_, 0, size);
00075 position_ += size;
00076 if(position_ > size_){ size_ = position_; }
00077 }
00078
00079
00080 int MemoryOutputStream::align(int alignSize){
00081 int size = position_ % alignSize;
00082 if(size== 0){ return size; }
00083 size = alignSize - size;
00084 bufferCheck(size);
00085 std::memset(buffer_ + position_, 0, size);
00086 position_ += size;
00087 if(position_ > size_){ size_ = position_; }
00088 return size;
00089 }
00090
00091
00092 int MemoryOutputStream::getPosition(){
00093 return position_;
00094 }
00095
00096
00097 void MemoryOutputStream::setPosition(int position){
00098 Assert(position >= 0);
00099 Assert(position <= size_);
00100 position_ = position;
00101 }
00102
00103
00104 void MemoryOutputStream::flush(){
00105 }
00106
00107 }
00108