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