|
#ifndef UTIL_STREAM_IO_H |
|
#define UTIL_STREAM_IO_H |
|
|
|
#include "../exception.hh" |
|
#include "../file.hh" |
|
|
|
namespace util { |
|
namespace stream { |
|
|
|
class ChainPosition; |
|
|
|
class ReadSizeException : public util::Exception { |
|
public: |
|
ReadSizeException() throw(); |
|
~ReadSizeException() throw(); |
|
}; |
|
|
|
class Read { |
|
public: |
|
explicit Read(int fd) : file_(fd) {} |
|
void Run(const ChainPosition &position); |
|
private: |
|
int file_; |
|
}; |
|
|
|
|
|
class PRead { |
|
public: |
|
explicit PRead(int fd, bool take_own = false) : file_(fd), own_(take_own) {} |
|
void Run(const ChainPosition &position); |
|
private: |
|
int file_; |
|
bool own_; |
|
}; |
|
|
|
class Write { |
|
public: |
|
explicit Write(int fd) : file_(fd) {} |
|
void Run(const ChainPosition &position); |
|
private: |
|
int file_; |
|
}; |
|
|
|
|
|
|
|
class WriteAndRecycle { |
|
public: |
|
explicit WriteAndRecycle(int fd) : file_(fd) {} |
|
void Run(const ChainPosition &position); |
|
private: |
|
int file_; |
|
}; |
|
|
|
class PWrite { |
|
public: |
|
explicit PWrite(int fd) : file_(fd) {} |
|
void Run(const ChainPosition &position); |
|
private: |
|
int file_; |
|
}; |
|
|
|
|
|
|
|
class FileBuffer { |
|
public: |
|
explicit FileBuffer(int fd) : file_(fd) {} |
|
|
|
PWrite Sink() const { |
|
util::SeekOrThrow(file_.get(), 0); |
|
return PWrite(file_.get()); |
|
} |
|
|
|
PRead Source(bool discard = false) { |
|
return PRead(discard ? file_.release() : file_.get(), discard); |
|
} |
|
|
|
uint64_t Size() const { |
|
return SizeOrThrow(file_.get()); |
|
} |
|
|
|
private: |
|
scoped_fd file_; |
|
}; |
|
|
|
} |
|
} |
|
#endif |
|
|