00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef BUFFERED_SINK_H
00030 #define BUFFERED_SINK_H
00031
00032 #ifndef __cplusplus
00033 #error This is a C++ include file
00034 #endif
00035
00036
00037
00038
00039 #include "Ref.h"
00040 #include "Reporter.h"
00041 #include "Sink.h"
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00061 class BufferedSink : public Sink, public virtual Reporter
00062 {
00063 private:
00064
00068 unsigned char * buffer;
00069
00073 unsigned char * bufferEnd;
00074
00078 unsigned int bufferSize;
00079
00083 unsigned int peak;
00084
00089 unsigned int chunkSize;
00090
00095 unsigned int misalignment;
00096
00100 unsigned char * inp;
00101
00105 unsigned char * outp;
00106
00107
00111 Ref<Sink> sink;
00112
00121 void
00122 init ( Sink * sink,
00123 unsigned int size,
00124 unsigned int chunkSize ) throw ( Exception );
00125
00131 void
00132 strip ( void ) throw ( Exception );
00133
00142 inline unsigned char *
00143 slidePointer (
00144 unsigned char * p,
00145 unsigned int offset ) throw ()
00146 {
00147 p += offset;
00148 while ( p >= bufferEnd ) {
00149 p -= bufferSize;
00150 }
00151
00152 return p;
00153 }
00154
00160 inline void
00161 updatePeak ( void ) throw ()
00162 {
00163 unsigned int u;
00164
00165 u = outp <= inp ? inp - outp : (bufferEnd - outp) + (inp - buffer);
00166 if ( peak < u ) {
00167 peak = u;
00168 reportEvent( 4, "BufferedSink, new peak:", peak);
00169 reportEvent( 4, "BufferedSink, remaining:", bufferSize - peak);
00170 }
00171 }
00172
00180 inline bool
00181 align ( void ) throw ( Exception )
00182 {
00183 char b[] = { 0 };
00184
00185 while ( misalignment ) {
00186 if ( sink->canWrite( 0, 0) ) {
00187 unsigned int ret;
00188
00189 if ( !(ret = sink->write( b, 1)) ) {
00190 return false;
00191 }
00192 --misalignment;
00193
00194 } else {
00195 return false;
00196 }
00197 }
00198
00199 return true;
00200 }
00201
00202
00203 protected:
00204
00210 inline
00211 BufferedSink ( void ) throw ( Exception )
00212 {
00213 throw Exception( __FILE__, __LINE__);
00214 }
00215
00221 inline unsigned int
00222 getSize ( void ) const throw ()
00223 {
00224 return bufferSize;
00225 }
00226
00236 unsigned int
00237 store ( const void * buffer,
00238 unsigned int bufferSize ) throw ( Exception );
00239
00240
00241 public:
00242
00252 inline
00253 BufferedSink ( Sink * sink,
00254 unsigned int size,
00255 unsigned int chunkSize = 1 ) throw ( Exception )
00256 {
00257 init( sink, size, chunkSize);
00258 }
00259
00266 BufferedSink ( const BufferedSink & buffer ) throw ( Exception );
00267
00273 inline virtual
00274 ~BufferedSink ( void ) throw ( Exception )
00275 {
00276 strip();
00277 }
00278
00286 virtual BufferedSink &
00287 operator= ( const BufferedSink & bs ) throw ( Exception );
00288
00294 inline unsigned int
00295 getPeak ( void ) const throw ()
00296 {
00297 return peak;
00298 }
00299
00306 inline virtual bool
00307 open ( void ) throw ( Exception )
00308 {
00309 return sink->open();
00310 }
00311
00317 inline virtual bool
00318 isOpen ( void ) const throw ()
00319 {
00320 return sink->isOpen();
00321 }
00322
00332 inline virtual bool
00333 canWrite ( unsigned int sec,
00334 unsigned int usec ) throw ( Exception )
00335 {
00336 return true;
00337 }
00338
00351 virtual unsigned int
00352 write ( const void * buf,
00353 unsigned int len ) throw ( Exception );
00354
00361 inline virtual void
00362 flush ( void ) throw ( Exception )
00363 {
00364 unsigned char b[1];
00365
00366 write( b, 0);
00367 }
00368
00374 virtual void
00375 close ( void ) throw ( Exception );
00376 };
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 #endif
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421