00001 #ifndef FDO_IO_OBJECTSTREAMREADER_H
00002 #define FDO_IO_OBJECTSTREAMREADER_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <FdoCommon.h>
00023 #include <Common/IStreamReaderTmpl.h>
00024
00025
00026
00027
00028
00029 template <typename T> class FdoIoObjectStreamReader : public FdoIStreamReaderTmpl<T>
00030 {
00031 public:
00032
00033
00034
00035
00036
00037
00038 FDO_API_COMMON virtual FdoInt64 GetLength()
00039 {
00040 FdoInt64 byteCount = mStream->GetLength();
00041
00042
00043
00044 return( byteCount / sizeof(T) );
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054 FDO_API_COMMON virtual void Skip(const FdoInt32 offset)
00055 {
00056 mStream->Skip( offset * sizeof(T) );
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066 FDO_API_COMMON virtual FdoInt64 GetIndex()
00067 {
00068 FdoInt64 byteIndex = mStream->GetIndex();
00069
00070 if ( (byteIndex % sizeof(T)) != 0 )
00071 throw FdoException::Create(
00072 FdoException::NLSGetMessage(
00073 FDO_NLSID(FDO_36_STREAMINDEXALIGN),
00074 "%1$s: Current byte position %2$d does not align with size of item being read ($3$d)",
00075 "FdoIoObjectStreamReader::GetIndex",
00076 byteIndex,
00077 sizeof(T)
00078 )
00079 );
00080
00081 return( byteIndex / sizeof(T) );
00082 }
00083
00084
00085
00086
00087
00088 FDO_API_COMMON virtual void Reset()
00089 {
00090 mStream->Reset();
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 FDO_API_COMMON virtual FdoInt32 ReadNext( T* buffer,
00113 const FdoInt32 offset = 0,
00114 const FdoInt32 count = -1)
00115 {
00116 FdoInt32 objectsRead = 0;
00117 FdoInt32 totalObjectsRead = 0;
00118 FdoInt32 bytesRead = 0;
00119
00120 if ( offset < 0 )
00121 throw FdoException::Create(
00122 FdoException::NLSGetMessage(
00123 FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)
00124 )
00125 );
00126
00127 if ( count < -1 )
00128 throw FdoException::Create(
00129 FdoException::NLSGetMessage(
00130 FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)
00131 )
00132 );
00133
00134 if ( count == -1 ) {
00135 for ( ; ; ) {
00136 objectsRead = ReadNext( buffer, offset + totalObjectsRead, mBlockSize );
00137 if ( objectsRead == 0 )
00138 break;
00139 totalObjectsRead += objectsRead;
00140 }
00141
00142 return(totalObjectsRead);
00143 }
00144 else {
00145 FdoInt32 lCount = count;
00146
00147
00148
00149 if ( mStream->HasContext() ) {
00150 FdoInt64 length = GetLength();
00151
00152 if ( length > -1 ) {
00153 FdoInt64 pos = GetIndex();
00154 FdoInt64 remaining = ( length - pos ) / sizeof(T);
00155
00156
00157
00158 lCount = lCount < remaining ? lCount : (FdoInt32) remaining;
00159 }
00160 }
00161
00162 bytesRead = (FdoInt32) mStream->Read( &buffer[offset], lCount * sizeof(T));
00163
00164 return( bytesRead / sizeof(T) );
00165 }
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 FDO_API_COMMON virtual FdoInt32 ReadNext( FdoArray<T> * &buffer,
00201 const FdoInt32 offset = 0,
00202 const FdoInt32 count = -1 )
00203 {
00204 FdoInt32 lCount = count;
00205
00206 FdoInt32 lCount2 = 0;
00207 FdoInt64 byteRemaining = 0;
00208 FdoInt64 remaining = 0;
00209
00210 if ( offset < 0 )
00211 throw FdoException::Create(
00212 FdoException::NLSGetMessage(
00213 FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)
00214 )
00215 );
00216
00217 if ( count < -1 )
00218 throw FdoException::Create(
00219 FdoException::NLSGetMessage(
00220 FDO_NLSID(FDO_5_INDEXOUTOFBOUNDS)
00221 )
00222 );
00223
00224 if ( count == -1 ) {
00225 byteRemaining = mStream->GetLength() - mStream->GetIndex();
00226 remaining = byteRemaining / sizeof(T);
00227
00228 if ( remaining > LONG_MAX )
00229 throw FdoException::Create(
00230 FdoException::NLSGetMessage(
00231 FDO_NLSID(FDO_46_REMAININGSTREAMSIZE),
00232 "%1$s: Cannot read remainder of stream, there are more than %$2d items remaining.",
00233 "FdoIoObjectStreamReader::ReadNext(FdoArray<T>,FdoInt32,FdoInt32)",
00234 LONG_MAX
00235 )
00236 );
00237
00238 lCount = (FdoInt32) remaining;
00239 }
00240 T* tmpBuffer = buffer->GetData();
00241
00242 lCount2 = ReadNext( tmpBuffer, offset, lCount );
00243
00244 return(lCount2);
00245 }
00246
00247 protected:
00248
00249 FdoIoObjectStreamReader() {}
00250
00251 FdoIoObjectStreamReader( FdoIoStream* stream )
00252 {
00253 FDO_SAFE_ADDREF(stream);
00254 mStream = stream;
00255 }
00256
00257 ~FdoIoObjectStreamReader()
00258 {
00259 }
00260
00261
00262 private:
00263 FdoIoStreamP mStream;
00264 static const FdoInt32 mBlockSize = 4096;
00265 };
00266
00267 #endif
00268
00269