00001
00002
00003
00004
00005 #include "dataIStream.h"
00006
00007 #include "log.h"
00008 #include <eq/base/debug.h>
00009
00010 #include <string.h>
00011
00012 using namespace std;
00013
00014 namespace eq
00015 {
00016 namespace net
00017 {
00018 DataIStream::DataIStream()
00019 : _input( 0 )
00020 , _inputSize( 0 )
00021 , _position( 0 )
00022 {}
00023
00024 DataIStream::~DataIStream()
00025 {
00026 reset();
00027 }
00028
00029 void DataIStream::reset()
00030 {
00031 _input = 0;
00032 _inputSize = 0;
00033 _position = 0;
00034 }
00035
00036 void DataIStream::read( void* data, uint64_t size )
00037 {
00038 if( !_checkBuffer( ))
00039 {
00040 EQERROR << "No more input data" << endl;
00041 return;
00042 }
00043
00044 EQASSERT( _input );
00045
00046 if( _position + size > _inputSize )
00047 {
00048 EQERROR << "Not enough data in input buffer" << endl;
00049 return;
00050 }
00051
00052 memcpy( data, _input + _position, size );
00053 _position += size;
00054 }
00055
00056 const void* DataIStream::getRemainingBuffer()
00057 {
00058 if( !_checkBuffer( ))
00059 return 0;
00060
00061 return _input + _position;
00062 }
00063
00064 uint64_t DataIStream::getRemainingBufferSize()
00065 {
00066 if( !_checkBuffer( ))
00067 return 0;
00068
00069 return _inputSize - _position;
00070 }
00071
00072 void DataIStream::advanceBuffer( const uint64_t offset )
00073 {
00074 EQASSERT( _position + offset <= _inputSize );
00075 _position += offset;
00076 }
00077
00078 bool DataIStream::_checkBuffer()
00079 {
00080 if( _position < _inputSize )
00081 return true;
00082
00083 if( !getNextBuffer( &_input, &_inputSize ))
00084 {
00085 return false;
00086 }
00087 _position = 0;
00088 return true;
00089 }
00090
00091 }
00092 }
00093