OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ossim::BlockStreamBuffer Class Reference

This is the BlockStreamBuffer class and derives from stream buf. More...

#include <BlockStreamBuffer.h>

Inheritance diagram for ossim::BlockStreamBuffer:

Public Member Functions

 BlockStreamBuffer (ossim::istream *adaptStream=0, ossim_uint64 blockSize=0)
 
virtual ~BlockStreamBuffer ()
 

Protected Member Functions

virtual std::streambuf * setbuf (char *s, std::streamsize n)
 This is a virtual method that can be overriden. More...
 
void setgPtrs ()
 setgPtrs calls setg and sets the eback egptr and gptr. More...
 
void loadBlock ()
 loadBlock will load data into the current block and call the setgPtrs to adjust the internal pointers tha the base streambuf may use More...
 
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)
 this is a protected method overriden from streambuf base. More...
 
virtual pos_type seekpos (pos_type pos, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
 seekpos is overriden from the base streambuf class. More...
 
virtual std::streamsize xsgetn (char_type *s, std::streamsize n)
 xsgetn is a protected virtual method that we override from the base streambuf. More...
 
virtual int underflow ()
 underflow is overriden from the base streambuf. More...
 
void syncCurrentPosition ()
 syncCurrentPosition is a utility method that we call internally in the overriden protected methods that will sync the location of the gptr to the absolute byte offset variable we are using internally. More...
 

Protected Attributes

std::vector< char > m_blockBuffer
 The block buffer that we set the buf pointers to. More...
 
ossim_int64 m_currentPosValue
 holds the current absolute byte position More...
 
BlockBufInfo m_blockInfo
 Holds the information about the block. More...
 
ossim::istreamm_adaptStream
 The stream we are adapting. More...
 

Detailed Description

This is the BlockStreamBuffer class and derives from stream buf.

This class

Definition at line 168 of file BlockStreamBuffer.h.

Constructor & Destructor Documentation

◆ BlockStreamBuffer()

ossim::BlockStreamBuffer::BlockStreamBuffer ( ossim::istream adaptStream = 0,
ossim_uint64  blockSize = 0 
)

Definition at line 7 of file BlockStreamBuffer.cpp.

References m_blockBuffer, ossim::BlockBufInfo::m_blockBufferPtr, m_blockInfo, and ossim::BlockBufInfo::m_blockSize.

8  : m_blockBuffer(),
10  m_blockInfo(),
11  m_adaptStream(adaptStream)
12 {
13  m_blockInfo.m_blockSize = blockSize;
15  {
18  }
19  setg(0, 0, 0);
20 }
std::vector< char > m_blockBuffer
The block buffer that we set the buf pointers to.
ossim_int64 m_blockSize
Is the size of the buffer.
char * m_blockBufferPtr
Starting address of the block.
ossim_int64 m_currentPosValue
holds the current absolute byte position
ossim::istream * m_adaptStream
The stream we are adapting.
BlockBufInfo m_blockInfo
Holds the information about the block.

◆ ~BlockStreamBuffer()

virtual ossim::BlockStreamBuffer::~BlockStreamBuffer ( )
inlinevirtual

Definition at line 171 of file BlockStreamBuffer.h.

171  {
172  m_adaptStream=0;
173  }
ossim::istream * m_adaptStream
The stream we are adapting.

Member Function Documentation

◆ loadBlock()

void ossim::BlockStreamBuffer::loadBlock ( )
protected

loadBlock will load data into the current block and call the setgPtrs to adjust the internal pointers tha the base streambuf may use

Definition at line 49 of file BlockStreamBuffer.cpp.

50 {
51  m_blockInfo.m_blockLoaded = false;
53  if(m_adaptStream)
54  {
58  if(!m_adaptStream->good()) m_adaptStream->clear();
59  if(m_blockInfo.m_startByte != m_adaptStream->tellg())
60  {
62  }
65  ossim_int64 bytesRead = m_adaptStream->gcount();
66  if(!m_adaptStream->bad()&&(bytesRead>0))
67  {
69  m_blockInfo.m_validSize = bytesRead;
71  {
72  m_blockInfo.m_blockLoaded = false;
73  }
74  }
75  }
76  setgPtrs();
77 }
ossim_int64 m_blockSize
Is the size of the buffer.
ossim_int64 m_startByte
Is the starting absolute byte offset for the buffer.
bool m_blockLoaded
Variable used to invalidate a block or specify whether the block is loaded.
char * m_blockBufferPtr
Starting address of the block.
ossim_int64 m_currentPosValue
holds the current absolute byte position
ossim_int64 m_validSize
is the valid size of the buffer.
ossim_int64 getBlockIndex(ossim_int64 pos)
Returns the index of a block.
bool isWithinValidWindow(ossim_int64 pos) const
This just tests if the given abolute position is within a valid window.
ossim::istream * m_adaptStream
The stream we are adapting.
long long ossim_int64
void setgPtrs()
setgPtrs calls setg and sets the eback egptr and gptr.
BlockBufInfo m_blockInfo
Holds the information about the block.

◆ seekoff()

std::streambuf::pos_type ossim::BlockStreamBuffer::seekoff ( off_type  offset,
std::ios_base::seekdir  dir,
std::ios_base::openmode  mode = std::ios_base::in | std::ios_base::out 
)
protectedvirtual

this is a protected method overriden from streambuf base.

we will convert the offset byte to an absolute if we can and then call the seek pos for the absolute seek

Definition at line 79 of file BlockStreamBuffer.cpp.

81 {
82  // make sure we are in synch with current pos
83  // gptr can be updated by other means
85  pos_type result = pos_type(off_type(-1));
86  if(m_adaptStream)
87  {
89  switch(dir)
90  {
91  case std::ios_base::beg:
92  {
93  pos = offset;
94  result = seekpos(pos, std::ios_base::in);
95  break;
96  }
97  case std::ios_base::cur:
98  {
99  pos += offset;
100  result = seekpos(pos, std::ios_base::in);
101  break;
102  }
103  case std::ios_base::end:
104  {
105  // at this point in time of implementation
106  // we do not know the offset to the end of the stream
107  // we are adapting
108  //
109  // we have no choice but to call the adapted stream
110  // implementation
111  result = m_adaptStream->rdbuf()->pubseekoff(pos, dir);
112  m_currentPosValue = result;
113  setgPtrs();
114 
115  break;
116  }
117  default:
118  {
119  break;
120  }
121  }
122  }
123  return result;
124 }
ossim_int64 m_currentPosValue
holds the current absolute byte position
void syncCurrentPosition()
syncCurrentPosition is a utility method that we call internally in the overriden protected methods th...
ossim::istream * m_adaptStream
The stream we are adapting.
long long ossim_int64
void setgPtrs()
setgPtrs calls setg and sets the eback egptr and gptr.
virtual pos_type seekpos(pos_type pos, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
seekpos is overriden from the base streambuf class.

◆ seekpos()

std::streambuf::pos_type ossim::BlockStreamBuffer::seekpos ( pos_type  pos,
std::ios_base::openmode  mode = std::ios_base::in | std::ios_base::out 
)
protectedvirtual

seekpos is overriden from the base streambuf class.

This is the seek of the aboslute position. We will check to see if the new position resides in the block and if so we just update our pointers and return the pos. If bnot then we use the adapted stream call to seek to the position and update our internal pointers.

Parameters
posThe absolute position to seek to the mode used. Should be in
Returns
The absolute position if successful or EOF if not.

Definition at line 126 of file BlockStreamBuffer.cpp.

128 {
129  if(traceDebug())
130  {
132  << "BlockStreamBuffer::seekpos DEBUG: entered with absolute position: " << pos << "\n";
133  }
134  pos_type result = pos_type(off_type(-1));
135 
136  if(m_adaptStream)
137  {
138  if(pos == m_currentPosValue)
139  {
140  result = pos;
141  }
143  {
144  result = pos;
145  m_currentPosValue = result;
146  }
147  else
148  {
149  result = m_adaptStream->rdbuf()->pubseekpos(pos, mode);
150  m_currentPosValue = result;
151  }
152  setgPtrs();
153  }
154 
155  if(traceDebug())
156  {
158  << "BlockStreamBuffer::seekpos DEBUG: leaving\n";
159 
160  }
161  return result;
162 
163 }
ossim_int64 m_currentPosValue
holds the current absolute byte position
bool isWithinValidWindow(ossim_int64 pos) const
This just tests if the given abolute position is within a valid window.
ossim::istream * m_adaptStream
The stream we are adapting.
void setgPtrs()
setgPtrs calls setg and sets the eback egptr and gptr.
BlockBufInfo m_blockInfo
Holds the information about the block.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)

◆ setbuf()

std::streambuf * ossim::BlockStreamBuffer::setbuf ( char *  s,
std::streamsize  n 
)
protectedvirtual

This is a virtual method that can be overriden.

pubsetbuf can be called and calls this protected method to set a buffer.

Parameters
sThe starting address of a byte buffer
nThe size of the buffer

Definition at line 22 of file BlockStreamBuffer.cpp.

References n.

24 {
25  if(n>0)
26  {
27  m_blockBuffer.clear();
29  setg(0, 0, 0);
30  }
31  return this;
32 }
std::vector< char > m_blockBuffer
The block buffer that we set the buf pointers to.
os2<< "> n<< " > nendobj n
void setBuffer(char *bufPtr, ossim_uint32 blockSize)
Will set the buffer and then reset the loaded flag to be false.
BlockBufInfo m_blockInfo
Holds the information about the block.

◆ setgPtrs()

void ossim::BlockStreamBuffer::setgPtrs ( )
protected

setgPtrs calls setg and sets the eback egptr and gptr.

to the managed buffer's valid window

Definition at line 34 of file BlockStreamBuffer.cpp.

35 {
36  if(m_blockInfo.isLoaded())
37  {
38  setg(m_blockInfo.getBuffer(),
41  }
42  else
43  {
44  setg(0,0,0);
45  }
46 
47 }
ossim_int64 m_startByte
Is the starting absolute byte offset for the buffer.
ossim_int64 m_currentPosValue
holds the current absolute byte position
ossim_int64 m_validSize
is the valid size of the buffer.
const char * getBuffer() const
BlockBufInfo m_blockInfo
Holds the information about the block.

◆ syncCurrentPosition()

void ossim::BlockStreamBuffer::syncCurrentPosition ( )
protected

syncCurrentPosition is a utility method that we call internally in the overriden protected methods that will sync the location of the gptr to the absolute byte offset variable we are using internally.

The problem is, when things like ignore(...) peek(...) and other options are used on an input stream the base might adjust the gptr location. When this is adjusted outside our control the offsets might get out of sync this is called to ensure this does not happen

Definition at line 261 of file BlockStreamBuffer.cpp.

262 {
263  if(m_blockInfo.isLoaded()&&gptr())
264  {
265  m_currentPosValue = (m_blockInfo.m_startByte+(gptr()-eback()));
266  }
267 }
ossim_int64 m_startByte
Is the starting absolute byte offset for the buffer.
ossim_int64 m_currentPosValue
holds the current absolute byte position
BlockBufInfo m_blockInfo
Holds the information about the block.

◆ underflow()

int ossim::BlockStreamBuffer::underflow ( )
protectedvirtual

underflow is overriden from the base streambuf.

It check to see if the current block is loaded and if not load the block. The method returns what is currently pointed to by the absolute offset or basically return *gptr().

Returns
the current byte we are pointing to.

Definition at line 231 of file BlockStreamBuffer.cpp.

232 {
233  if(traceDebug())
234  {
236  << "BlockStreamBuffer::underflow DEBUG: entered ......\n";
237 
238  }
239  if(!m_adaptStream) return EOF;
240  else{
243  {
244  loadBlock();
245  }
246 
247  if(!m_blockInfo.isLoaded())
248  {
249  return EOF;
250  }
251  }
252  if(traceDebug())
253  {
255  << "BlockStreamBuffer::underflow DEBUG: leaving ......\n";
256 
257  }
258  return (int)static_cast<ossim_uint8>(*gptr());
259 }
ossim_int64 m_currentPosValue
holds the current absolute byte position
void loadBlock()
loadBlock will load data into the current block and call the setgPtrs to adjust the internal pointers...
void syncCurrentPosition()
syncCurrentPosition is a utility method that we call internally in the overriden protected methods th...
bool isWithinValidWindow(ossim_int64 pos) const
This just tests if the given abolute position is within a valid window.
ossim::istream * m_adaptStream
The stream we are adapting.
BlockBufInfo m_blockInfo
Holds the information about the block.
unsigned char ossim_uint8
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)

◆ xsgetn()

std::streamsize ossim::BlockStreamBuffer::xsgetn ( char_type *  s,
std::streamsize  n 
)
protectedvirtual

xsgetn is a protected virtual method that we override from the base streambuf.

The method will load blocks of data to service the requested buffer to fill. If the request covers several block it will iterate and load each block of data until the request is satisfied.

Parameters
sThe destination buffer large enough to hold the characters being requested
nThe number of characters to request from the input stream
Returns
The number of bytes read or -1 if failed

Definition at line 165 of file BlockStreamBuffer.cpp.

167 {
168  if(traceDebug())
169  {
171  << "BlockStreamBuffer::xsgetn DEBUG: entered ......"<<n<<"\n";
172 
173  }
174  std::streamsize result = EOF;
175 
176  if(m_adaptStream)
177  {
178  ossim_int64 bytesNeedToRead = n;
179  ossim_int64 bytesRead = 0;
181 
182  while(bytesNeedToRead>0)
183  {
185  {
186  loadBlock();
187  }
188 
189  if(m_blockInfo.isLoaded())
190  {
193  if(delta <= bytesNeedToRead)
194  {
195  std::memcpy(s+bytesRead,
197  delta);
198  bytesRead+=delta;
199  bytesNeedToRead-=delta;
200  m_currentPosValue+=delta;
201  }
202  else
203  {
204  std::memcpy(s+bytesRead,
206  bytesNeedToRead);
207  m_currentPosValue+=bytesNeedToRead;
208  bytesRead+=bytesNeedToRead;
209  bytesNeedToRead=0;
210  }
211  }
212  else
213  {
214  bytesNeedToRead=0;
215  }
216  }
217  // specify the current absolute position after read
218  // so we are in sync.
219  setgPtrs();
220  result = bytesRead;
221  }
222  if(traceDebug())
223  {
225  << "BlockStreamBuffer::xsgetn DEBUG: leaving ......\n";
226 
227  }
228  return result;
229 }
ossim_int64 m_currentPosValue
holds the current absolute byte position
void loadBlock()
loadBlock will load data into the current block and call the setgPtrs to adjust the internal pointers...
void syncCurrentPosition()
syncCurrentPosition is a utility method that we call internally in the overriden protected methods th...
os2<< "> n<< " > nendobj n
ossim_int64 getEndByte() const
Convenient method to get the absolute byte position of the end byte.
bool isWithinValidWindow(ossim_int64 pos) const
This just tests if the given abolute position is within a valid window.
const char * getBufferStart(ossim_int64 absolutePosition) const
Get the starting address in the buffer of the absolute position.
ossim::istream * m_adaptStream
The stream we are adapting.
long long ossim_int64
void setgPtrs()
setgPtrs calls setg and sets the eback egptr and gptr.
BlockBufInfo m_blockInfo
Holds the information about the block.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)

Member Data Documentation

◆ m_adaptStream

ossim::istream* ossim::BlockStreamBuffer::m_adaptStream
protected

The stream we are adapting.

Definition at line 195 of file BlockStreamBuffer.h.

◆ m_blockBuffer

std::vector<char> ossim::BlockStreamBuffer::m_blockBuffer
protected

The block buffer that we set the buf pointers to.

Definition at line 178 of file BlockStreamBuffer.h.

Referenced by BlockStreamBuffer().

◆ m_blockInfo

BlockBufInfo ossim::BlockStreamBuffer::m_blockInfo
protected

Holds the information about the block.

It tells us if the block is currently loaded and what the valid size is

Definition at line 190 of file BlockStreamBuffer.h.

Referenced by BlockStreamBuffer().

◆ m_currentPosValue

ossim_int64 ossim::BlockStreamBuffer::m_currentPosValue
protected

holds the current absolute byte position

Definition at line 183 of file BlockStreamBuffer.h.


The documentation for this class was generated from the following files: