29 static ossimTrace traceDebug(
"ossimCurlStreamBuffer:debug");
36 m_bufferActualDataSize(0),
37 m_currentBlockPosition(-1),
54 blockNumber = byteOffset/m_buffer.size();
72 blockOffset = byteOffset%m_buffer.size();
86 startRange = blockIndex*m_buffer.size();
87 endRange = startRange + m_buffer.size()-1;
102 ossim_int64 blockIndex = getBlockIndex(absolutePosition);
104 if(absolutePosition<0)
return false;
105 if((absolutePosition < 0) || (absolutePosition > (
ossim_int64)m_fileSize))
109 if(getBlockRangeInBytes(blockIndex, startRange, endRange))
111 m_curlHttpRequest.clearHeaderOptions();
112 stringStream <<
"bytes=" << startRange <<
"-" << endRange;
113 m_curlHttpRequest.addHeaderOption(
"Range", stringStream.str().c_str());
125 if((code >=200) && (code < 300))
129 m_bufferPtr = &m_buffer.front();
135 m_bufferActualDataSize = contentLen;
137 m_bufferPtr = &m_buffer.front();
138 setg(m_bufferPtr, m_bufferPtr + delta, m_bufferPtr+m_bufferActualDataSize);
139 m_currentBlockPosition = startRange;
145 m_bufferActualDataSize = 0;
183 std::ios_base::openmode m)
185 std::string temp(connectionString);
186 return open(temp, options, m);
191 std::ios_base::openmode )
196 <<
"ossim::CurlStreamBuffer::open DEBUG: entered..... with connection " << connectionString << std::endl;
209 m_curlHttpRequest.set(url, header);
212 m_fileSize = filesize;
214 m_currentBlockPosition = 0;
219 m_fileSize = m_curlHttpRequest.getContentLength();
220 m_currentBlockPosition = 0;
237 <<
"ossim::CurlStreamBuffer::open DEBUG: Took " << delta <<
" seconds to open" << std::endl;
239 <<
"ossim::CurlStreamBuffer::open DEBUG: leaving....." << std::endl;
242 if(m_opened)
return this;
255 m_currentBlockPosition = 0;
265 else if( !withinWindow() )
267 ossim_int64 absolutePosition = getAbsoluteByteOffset();
268 if(absolutePosition < 0)
273 if(!loadBlock(absolutePosition))
290 std::ios_base::seekdir dir,
291 std::ios_base::openmode mode)
296 pos_type result = pos_type(off_type(-1));
298 if((mode & std::ios_base::in)&&
299 (mode & std::ios_base::out))
305 case std::ios_base::beg:
307 ossim_int64 absolutePosition = getAbsoluteByteOffset();
314 if(!loadBlock(offset))
322 result = pos_type(offset);
326 if(!loadBlock(result))
331 else if(mode & std::ios_base::in)
333 absolutePosition = getAbsoluteByteOffset();
335 setg(eback(), gptr()+delta, egptr());
341 case std::ios_base::cur:
351 result = getAbsoluteByteOffset();
355 result = getAbsoluteByteOffset();
361 setg(eback(), gptr()+offset, egptr());
368 case std::ios_base::end:
370 ossim_int64 absolutePosition = m_fileSize + offset;
373 if(absolutePosition==m_fileSize)
375 if(!loadBlock(absolutePosition))
382 if(!loadBlock(absolutePosition))
388 ossim_int64 currentAbsolutePosition = getAbsoluteByteOffset();
389 ossim_int64 delta = absolutePosition-currentAbsolutePosition;
393 if(mode & std::ios_base::in )
395 setg(eback(), gptr()+delta, egptr());
398 result = absolutePosition;
415 pos_type result = pos_type(off_type(-1));
420 if(!loadBlock(tempPos))
425 ossim_int64 absoluteLocation = getAbsoluteByteOffset();
426 if(mode & std::ios_base::in)
433 setg(eback(), gptr()+delta, egptr());
446 if(!is_open())
return EOF;
449 if((!withinWindow())&&is_open())
458 else if(!loadBlock(getAbsoluteByteOffset()))
466 ossim_int64 currentAbsolutePosition = getAbsoluteByteOffset();
468 if(currentAbsolutePosition >= (
ossim_int64)m_fileSize)
472 else if((currentAbsolutePosition + bytesNeedToRead)>(
ossim_int64)m_fileSize)
474 bytesNeedToRead = (m_fileSize - currentAbsolutePosition);
477 while(bytesNeedToRead > 0)
479 currentAbsolutePosition = getAbsoluteByteOffset();
482 if(!loadBlock(currentAbsolutePosition))
486 currentAbsolutePosition = getAbsoluteByteOffset();
490 if(currentAbsolutePosition>=0)
496 if(delta <= bytesNeedToRead)
498 std::memcpy(s+bytesRead, gptr(), delta);
502 bytesNeedToRead-=delta;
504 setg(eback(), gptr()+delta, egptr());
509 std::memcpy(s+bytesRead, gptr(), bytesNeedToRead);
510 setg(eback(), gptr()+bytesNeedToRead, egptr());
513 bytesRead+=bytesNeedToRead;
522 return std::streamsize(bytesRead);
529 if(m_currentBlockPosition >= 0)
531 result = m_currentBlockPosition;
534 result += (gptr()-eback());
544 if(!gptr())
return false;
545 return ((gptr()>=eback()) && (gptr()<egptr()));
555 return m_buffer.size();
ossim_uint64 getBlockSize() const
ossim_int64 getAbsoluteByteOffset() const
std::basic_stringstream< char > stringstream
Class for char mixed input and output memory streams.
virtual std::streamsize xsgetn(char_type *s, std::streamsize n)
ossim_int64 getBlockIndex(ossim_int64 byteOffset) const
ossim_uint32 getStatusCode() const
unsigned long long Timer_t
Represents serializable keyword/value map.
ossim_int64 getContentLength() const
ossimKeywordlist & headerKwl()
bool withinWindow() const
virtual pos_type seekpos(pos_type pos, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
static ossimTimer * instance()
os2<< "> n<< " > nendobj n
unsigned long long ossim_uint64
const ossimString & getProtocol() const
virtual pos_type seekoff(off_type offset, std::ios_base::seekdir dir, std::ios_base::openmode __mode=std::ios_base::in|std::ios_base::out)
CurlStreamBuffer(ossim_int64 blockSize=ossim::CurlStreamDefaults::m_readBlocksize)
bool getBlockRangeInBytes(ossim_int64 blockIndex, ossim_int64 &startRange, ossim_int64 &endRange) const
void copyAllDataFromInputStream(ByteBuffer &buffer)
Timer_t tick() const
Get the timers tick value.
double delta_s(Timer_t t1, Timer_t t2) const
Get the time in seconds between timer ticks t1 and t2.
void convertHeaderStreamToKeywordlist()
This will parse out the response code from the status line and initialize the header variables into a...
bool loadBlock(ossim_int64 absolutePosition)
unsigned char ossim_uint8
CurlStreamBuffer * open(const char *connectionString, const ossimKeywordlist &options, std::ios_base::openmode mode)
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
ossim_int64 getFileSize() const
ossim_int64 getBlockOffset(ossim_int64 byteOffset) const