00001 /* 00002 // $Id: //open/dev/fennel/common/ByteInputStream.h#14 $ 00003 // Fennel is a library of data storage and processing components. 00004 // Copyright (C) 2005-2009 The Eigenbase Project 00005 // Copyright (C) 2005-2009 SQLstream, Inc. 00006 // Copyright (C) 2005-2009 LucidEra, Inc. 00007 // Portions Copyright (C) 1999-2009 John V. Sichi 00008 // 00009 // This program is free software; you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by the Free 00011 // Software Foundation; either version 2 of the License, or (at your option) 00012 // any later version approved by The Eigenbase Project. 00013 // 00014 // This program is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with this program; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 */ 00023 00024 #ifndef Fennel_ByteInputStream_Included 00025 #define Fennel_ByteInputStream_Included 00026 00027 #include "fennel/common/ByteStream.h" 00028 00029 FENNEL_BEGIN_NAMESPACE 00030 00034 class FENNEL_COMMON_EXPORT ByteInputStream : virtual public ByteStream 00035 { 00039 PConstBuffer pFirstByte; 00040 00044 PConstBuffer pNextByte; 00045 00049 PConstBuffer pEndByte; 00050 00051 protected: 00052 explicit ByteInputStream(); 00053 00058 virtual void readNextBuffer() = 0; 00059 00064 virtual void readPrevBuffer(); 00065 00073 void setBuffer( 00074 PConstBuffer pBuffer, 00075 uint cbBuffer); 00076 00080 void nullifyBuffer(); 00081 00085 uint getBytesAvailable() const; 00086 00090 uint getBytesConsumed() const; 00091 00092 public: 00102 uint readBytes(void *pData,uint cbRequested); 00103 00111 template<class T> 00112 uint readValue(T &value) 00113 { 00114 return readBytes(&value,sizeof(value)); 00115 } 00116 00143 PConstBuffer getReadPointer(uint cbRequested,uint *pcbActual = NULL); 00144 00151 void consumeReadPointer(uint cbUsed); 00152 00158 void seekForward(uint cb); 00159 00166 void seekBackward(uint cb); 00167 00168 00175 virtual SharedByteStreamMarker newMarker(); 00176 00186 virtual void mark(ByteStreamMarker &marker); 00187 00196 virtual void reset(ByteStreamMarker const &marker); 00197 }; 00198 00199 inline uint ByteInputStream::getBytesAvailable() const 00200 { 00201 return static_cast<uint>(pEndByte - pNextByte); 00202 } 00203 00204 inline uint ByteInputStream::getBytesConsumed() const 00205 { 00206 return static_cast<uint>(pNextByte - pFirstByte); 00207 } 00208 00209 inline PConstBuffer ByteInputStream::getReadPointer( 00210 uint cbRequested,uint *pcbActual) 00211 { 00212 if (getBytesAvailable() < cbRequested) { 00213 assert(pNextByte == pEndByte); 00214 readNextBuffer(); 00215 if (pNextByte == pEndByte) { 00216 if (pcbActual) { 00217 *pcbActual = 0; 00218 } 00219 return NULL; 00220 } 00221 } 00222 if (pcbActual) { 00223 *pcbActual = getBytesAvailable(); 00224 } 00225 return pNextByte; 00226 } 00227 00228 inline void ByteInputStream::seekForward(uint cb) 00229 { 00230 uint cbActual = readBytes(NULL,cb); 00231 assert(cbActual == cb); 00232 } 00233 00234 inline void ByteInputStream::consumeReadPointer(uint cbUsed) 00235 { 00236 assert(cbUsed <= getBytesAvailable()); 00237 pNextByte += cbUsed; 00238 cbOffset += cbUsed; 00239 } 00240 00241 inline void ByteInputStream::setBuffer( 00242 PConstBuffer pBuffer, 00243 uint cbBuffer) 00244 { 00245 pFirstByte = pBuffer; 00246 pEndByte = pBuffer + cbBuffer; 00247 pNextByte = pFirstByte; 00248 } 00249 00250 inline void ByteInputStream::nullifyBuffer() 00251 { 00252 setBuffer(NULL,0); 00253 } 00254 00255 FENNEL_END_NAMESPACE 00256 00257 #endif 00258 00259 // End ByteInputStream.h