00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_Stream_hxx
00024 #define __ccxx_Stream_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/Buffer.h++>
00028 #include <commonc++/IOException.h++>
00029 #include <commonc++/MemoryBlock.h++>
00030
00031 namespace ccxx {
00032
00033 class AsyncIOTask;
00034
00037 enum IOMode { IORead, IOWrite, IOReadWrite };
00038
00039 enum SeekMode { SeekAbsolute, SeekRelative, SeekEnd };
00040
00046 class COMMONCPP_API Stream
00047 {
00048 friend class Process;
00049
00050 public:
00051
00055 static const uint_t MAX_IOBLOCK_COUNT;
00056
00057 protected:
00058
00060 Stream();
00061
00069 Stream(FileHandle handle, bool seekable = true, bool readable = true,
00070 bool writable = true);
00071
00072 public:
00073
00075 virtual ~Stream() throw();
00076
00084 virtual size_t read(ByteBuffer& buffer) throw(IOException);
00085
00093 virtual size_t read(CharBuffer& buffer) throw(IOException);
00094
00116 template<typename T> size_t read(Buffer<T> &buffer, size_t &partial)
00117 throw(IOException)
00118 {
00119 size_t n = _readElem(reinterpret_cast<byte_t *>(buffer.getPointer()),
00120 sizeof(T), buffer.getRemaining(), partial);
00121
00122 if(n > 0)
00123 buffer.bump(n);
00124
00125 return(n);
00126 }
00127
00136 virtual size_t read(byte_t *buffer, size_t buflen) throw(IOException);
00137
00148 size_t read(byte_t *buffer, size_t buflen, int64_t offset,
00149 AsyncIOTask &task) throw(IOException);
00150
00161 size_t read(ByteBuffer &buffer, int64_t offset, AsyncIOTask &task)
00162 throw(IOException);
00163
00174 size_t write(const byte_t *buffer, size_t buflen, int64_t offset,
00175 AsyncIOTask &task) throw(IOException);
00176
00187 size_t write(ByteBuffer &buffer, int64_t offset, AsyncIOTask &task)
00188 throw(IOException);
00189
00201 virtual size_t readFully(ByteBuffer &buffer, size_t count = 0)
00202 throw(IOException);
00203
00215 virtual size_t readFully(CharBuffer &buffer, size_t count = 0)
00216 throw(IOException);
00217
00240 template<typename T> size_t readFully(Buffer<T> &buffer, size_t &partial)
00241 throw(IOException)
00242 {
00243 size_t n = _readElemFully(reinterpret_cast<byte_t *>(buffer.getPointer()),
00244 sizeof(T), buffer.getRemaining(), partial);
00245
00246 if(n > 0)
00247 buffer.bump(n);
00248
00249 return(n);
00250 }
00251
00261 virtual size_t readFully(byte_t *buffer, size_t buflen) throw(IOException);
00262
00273 virtual size_t read(MemoryBlock *vec, uint_t count) throw(IOException);
00274
00281 virtual size_t write(ByteBuffer& buffer) throw(IOException);
00282
00289 virtual size_t write(CharBuffer& buffer) throw(IOException);
00290
00311 template<typename T> size_t write(Buffer<T> &buffer, size_t &partial)
00312 throw(IOException)
00313 {
00314 size_t n = _writeElem(
00315 reinterpret_cast<const byte_t *>(buffer.getPointer()),
00316 sizeof(T), buffer.getRemaining(), partial);
00317
00318 if(n > 0)
00319 buffer.bump(n);
00320
00321 return(n);
00322 }
00323
00332 virtual size_t write(const byte_t *buffer, size_t buflen)
00333 throw(IOException);
00334
00342 virtual size_t writeFully(ByteBuffer& buffer) throw(IOException);
00343
00351 virtual size_t writeFully(CharBuffer& buffer) throw(IOException);
00352
00374 template<typename T> size_t writeFully(Buffer<T> &buffer, size_t &partial)
00375 throw(IOException)
00376 {
00377 size_t n = _writeElemFully(
00378 reinterpret_cast<const byte_t *>(buffer.getPointer()),
00379 sizeof(T), buffer.getRemaining(), partial);
00380
00381 if(n > 0)
00382 buffer.bump(n);
00383
00384 return(n);
00385 }
00386
00394 virtual size_t writeFully(const byte_t *buffer, size_t buflen)
00395 throw(IOException);
00396
00406 virtual size_t write(const MemoryBlock *vec, uint_t count)
00407 throw(IOException);
00408
00417 virtual int64_t seek(int64_t offset, SeekMode mode = SeekAbsolute)
00418 throw(IOException);
00419
00426 virtual int64_t tell() throw(IOException);
00427
00429 inline bool isOpen() const throw()
00430 { return(_canRead || _canWrite); }
00431
00433 inline bool isSeekable() const throw()
00434 { return(_seekable); }
00435
00437 inline bool isReadable() const throw()
00438 { return(_canRead); }
00439
00441 inline bool isWritable() const throw()
00442 { return(_canWrite); }
00443
00447 inline bool isFullDuplex() const throw()
00448 { return(_canRead && _canWrite); }
00449
00453 inline bool isHalfDuplex() const throw()
00454 { return(! _canRead || ! _canWrite); }
00455
00460 virtual void close(IOMode mode = IOReadWrite) throw();
00461
00466 virtual void setTimeout(timespan_ms_t timeout) throw(IOException);
00467
00469 inline timespan_ms_t getTimeout() const throw()
00470 { return(_timeout); }
00471
00472 protected:
00473
00481 void _init(FileHandle handle, bool seekable, bool readable, bool writable);
00482
00484 FileHandle _handle;
00485 bool _seekable;
00486 bool _canRead;
00487 bool _canWrite;
00488 timespan_ms_t _timeout;
00491 private:
00492
00493 size_t _readAsync(byte_t *buffer, size_t buflen, int64_t offset,
00494 AsyncIOTask &task) throw(IOException);
00495 size_t _writeAsync(const byte_t *buffer, size_t buflen, int64_t offset,
00496 AsyncIOTask &task) throw(IOException);
00497
00498 size_t _readElem(byte_t *buffer, size_t size, size_t nelem,
00499 size_t &partial) throw(IOException);
00500 size_t _readElemFully(byte_t *buffer, size_t size, size_t nelem,
00501 size_t &partial) throw(IOException);
00502
00503 size_t _writeElem(const byte_t *buffer, size_t size, size_t nelem,
00504 size_t &partial) throw(IOException);
00505 size_t _writeElemFully(const byte_t *buffer, size_t size, size_t nelem,
00506 size_t &partial) throw(IOException);
00507
00508 CCXX_COPY_DECLS(Stream);
00509 };
00510
00511 };
00512
00513 #endif // __ccxx_Stream_hxx
00514
00515