casa
$Rev:20696$
|
Class for buffered IO on a large file. More...
#include <LargeFilebufIO.h>
Public Member Functions | |
LargeFilebufIO () | |
Default constructor. | |
LargeFilebufIO (int fd, uInt bufferSize=16384) | |
Construct from the given file descriptor. | |
void | attach (int fd, uInt bufferSize=16384) |
Attach to the given file descriptor. | |
~LargeFilebufIO () | |
The destructor closes the file when it was owned and opened and not closed yet. | |
virtual void | write (uInt size, const void *buf) |
Write the number of bytes. | |
virtual Int | read (uInt size, void *buf, Bool throwException=True) |
Read size bytes from the File. | |
void | flush () |
Flush the current buffer. | |
void | resync () |
Resync the file (i.e. | |
virtual Int64 | length () |
Get the length of the byte stream. | |
virtual Bool | isReadable () const |
Is the IO stream readable? | |
virtual Bool | isWritable () const |
Is the IO stream writable? | |
virtual Bool | isSeekable () const |
Is the IO stream seekable? | |
virtual String | fileName () const |
Get the file name of the file attached. | |
uInt | bufferSize () const |
Get the buffer size. | |
Protected Member Functions | |
void | detach (Bool closeFile=False) |
Detach the FILE. | |
void | fillRWFlags (int fd) |
Determine if the file descriptor is readable and/or writable. | |
void | fillSeekable () |
Determine if the file is seekable. | |
virtual Int64 | doSeek (Int64 offset, ByteIO::SeekOption) |
Reset the position pointer to the given value. | |
void | setBuffer (uInt bufSize) |
Set a new buffer size. | |
void | writeBuffer (Int64 offset, const char *buf, Int size) |
Write a buffer of given length into the file at given offset. | |
uInt | readBuffer (Int64 offset, char *buf, uInt size, Bool throwException) |
Read a buffer of given length from the file at given offset. | |
void | writeBlock (uInt size, const char *buf) |
Write a block into the stream at the current offset. | |
uInt | readBlock (uInt size, char *buf, Bool throwException) |
Read a block from the stream at the current offset. | |
Private Member Functions | |
LargeFilebufIO (const LargeFilebufIO &that) | |
Copy constructor, should not be used. | |
LargeFilebufIO & | operator= (const LargeFilebufIO &that) |
Assignment, should not be used. | |
Private Attributes | |
Bool | itsSeekable |
Bool | itsReadable |
Bool | itsWritable |
int | itsFile |
uInt | itsBufSize |
uInt | itsBufLen |
char * | itsBuffer |
Int64 | itsBufOffset |
Int64 | itsOffset |
Int64 | itsSeekOffset |
Bool | itsDirty |
Class for buffered IO on a large file.
Public interface
<h3>Review Status</h3><dl><dt>Reviewed By:<dd>UNKNOWN<dt>Date Reviewed:<dd>before2004/08/25<dt>Test programs:<dd>tByteIO</dl>
This class is a specialization of class ByteIO . This class is doing IO on a file in a buffered way to reduce the number of file accesses as much as possible. It is part of the entire IO framework. It can for instance be used to store data in canonical format in a file in an IO-efficient way
The buffer size is dynamic, so any time it can be set as needed.
It is also possible to construct a LargeFilebufIO
object from a file descriptor (e.g. for a pipe or socket). The constructor will determine automatically if the file is readable, writable and seekable.
This example shows how LargeFilebufIO can be used with an fd. It uses the fd for a regular file, which could be done in an easier way using class RegularFileIO . However, when using pipes or sockets, this would be the only way.
// Get a file descriptor for the file. int fd = open ("file.name"); // Use that as the source of AipsIO (which will also use CanonicalIO). LargeFilebufIO fio (fd); AipsIO stream (&fio); // Read the data. Int vali; Bool valb; stream >> vali >> valb;
The stdio package was used, but it proved to be very slow on SOlaris. After a seek the buffer was refreshed, which increased the number of file accesses enormously. Also the interaction between reads and writes in stdio was poor.
Definition at line 94 of file LargeFilebufIO.h.
Default constructor.
A stream can be attached using the attach function.
casa::LargeFilebufIO::LargeFilebufIO | ( | int | fd, |
uInt | bufferSize = 16384 |
||
) | [explicit] |
Construct from the given file descriptor.
Note that the destructor and the detach function implicitly close the file descriptor.
The destructor closes the file when it was owned and opened and not closed yet.
casa::LargeFilebufIO::LargeFilebufIO | ( | const LargeFilebufIO & | that | ) | [private] |
Copy constructor, should not be used.
void casa::LargeFilebufIO::attach | ( | int | fd, |
uInt | bufferSize = 16384 |
||
) |
Attach to the given file descriptor.
Note that the destructor and the detach function implicitly close the file descriptor.
uInt casa::LargeFilebufIO::bufferSize | ( | ) | const [inline] |
void casa::LargeFilebufIO::detach | ( | Bool | closeFile = False | ) | [protected] |
Detach the FILE.
Close it when needed.
virtual Int64 casa::LargeFilebufIO::doSeek | ( | Int64 | offset, |
ByteIO::SeekOption | |||
) | [protected, virtual] |
Reset the position pointer to the given value.
It returns the new position.
Implements casa::ByteIO.
virtual String casa::LargeFilebufIO::fileName | ( | ) | const [virtual] |
Get the file name of the file attached.
Reimplemented in casa::LargeRegularFileIO.
void casa::LargeFilebufIO::fillRWFlags | ( | int | fd | ) | [protected] |
Determine if the file descriptor is readable and/or writable.
void casa::LargeFilebufIO::fillSeekable | ( | ) | [protected] |
Determine if the file is seekable.
void casa::LargeFilebufIO::flush | ( | ) |
Flush the current buffer.
virtual Bool casa::LargeFilebufIO::isReadable | ( | ) | const [virtual] |
Is the IO stream readable?
Implements casa::ByteIO.
virtual Bool casa::LargeFilebufIO::isSeekable | ( | ) | const [virtual] |
Is the IO stream seekable?
Implements casa::ByteIO.
virtual Bool casa::LargeFilebufIO::isWritable | ( | ) | const [virtual] |
Is the IO stream writable?
Implements casa::ByteIO.
virtual Int64 casa::LargeFilebufIO::length | ( | ) | [virtual] |
Get the length of the byte stream.
Implements casa::ByteIO.
LargeFilebufIO& casa::LargeFilebufIO::operator= | ( | const LargeFilebufIO & | that | ) | [private] |
Assignment, should not be used.
virtual Int casa::LargeFilebufIO::read | ( | uInt | size, |
void * | buf, | ||
Bool | throwException = True |
||
) | [virtual] |
Read size
bytes from the File.
Returns the number of bytes actually read. Will throw an exception (AipsError) if the requested number of bytes could not be read unless throwException is set to False. Will always throw an exception if the file is not readable or the system call returns an undocumented value.
Implements casa::ByteIO.
uInt casa::LargeFilebufIO::readBlock | ( | uInt | size, |
char * | buf, | ||
Bool | throwException | ||
) | [protected] |
Read a block from the stream at the current offset.
It is guaranteed that the block fits in a single buffer.
uInt casa::LargeFilebufIO::readBuffer | ( | Int64 | offset, |
char * | buf, | ||
uInt | size, | ||
Bool | throwException | ||
) | [protected] |
Read a buffer of given length from the file at given offset.
void casa::LargeFilebufIO::resync | ( | ) |
Resync the file (i.e.
empty the current buffer).
void casa::LargeFilebufIO::setBuffer | ( | uInt | bufSize | ) | [protected] |
Set a new buffer size.
If a buffer was already existing, flush and delete it.
virtual void casa::LargeFilebufIO::write | ( | uInt | size, |
const void * | buf | ||
) | [virtual] |
Write the number of bytes.
Implements casa::ByteIO.
void casa::LargeFilebufIO::writeBlock | ( | uInt | size, |
const char * | buf | ||
) | [protected] |
Write a block into the stream at the current offset.
It is guaranteed that the block fits in a single buffer.
void casa::LargeFilebufIO::writeBuffer | ( | Int64 | offset, |
const char * | buf, | ||
Int | size | ||
) | [protected] |
Write a buffer of given length into the file at given offset.
char* casa::LargeFilebufIO::itsBuffer [private] |
Definition at line 189 of file LargeFilebufIO.h.
uInt casa::LargeFilebufIO::itsBufLen [private] |
Definition at line 188 of file LargeFilebufIO.h.
Int64 casa::LargeFilebufIO::itsBufOffset [private] |
Definition at line 190 of file LargeFilebufIO.h.
uInt casa::LargeFilebufIO::itsBufSize [private] |
Definition at line 187 of file LargeFilebufIO.h.
Referenced by bufferSize().
Bool casa::LargeFilebufIO::itsDirty [private] |
Definition at line 193 of file LargeFilebufIO.h.
int casa::LargeFilebufIO::itsFile [private] |
Definition at line 186 of file LargeFilebufIO.h.
Int64 casa::LargeFilebufIO::itsOffset [private] |
Definition at line 191 of file LargeFilebufIO.h.
Bool casa::LargeFilebufIO::itsReadable [private] |
Definition at line 184 of file LargeFilebufIO.h.
Bool casa::LargeFilebufIO::itsSeekable [private] |
Definition at line 183 of file LargeFilebufIO.h.
Int64 casa::LargeFilebufIO::itsSeekOffset [private] |
Definition at line 192 of file LargeFilebufIO.h.
Bool casa::LargeFilebufIO::itsWritable [private] |
Definition at line 185 of file LargeFilebufIO.h.