Loading...
Searching...
No Matches
casacore::FilebufIO Class Reference
casa package (libcasa_casa) » IO_module

More...

`#include <FilebufIO.h>`

## Public Member Functions

| FilebufIO ()
| Default constructor.
| FilebufIO (int fd, uInt bufferSize=16384)
| Construct from the given file descriptor.
void | attach (int fd, uInt bufferSize=16384)
| Attach to the given file descriptor.
virtual | ~FilebufIO ()
| The destructor closes the file when it was owned and opened and not closed yet.
virtual void | write (Int64 size, const void *buf)
| Write the number of bytes.
virtual Int64 | read (Int64 size, void *buf, Bool throwException=True)
| Read size bytes from the File.
virtual void | flush ()
| Flush the current buffer.
virtual void | resync ()
| Resync the file (i.e.
virtual void | truncate (Int64 size)
| Truncate the file to the given size.
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.
Public Member Functions inherited from casacore::ByteIO
| ByteIO ()
| The constructor does nothing.
virtual | ~ByteIO ()
virtual void | pwrite (Int64 size, Int64 offset, const void *buf)
| Write size bytes to the byte stream at offset.
virtual Int64 | pread (Int64 size, Int64 offset, void *buf, Bool throwException=True)
| Like read but reads from offset of start of the file The file offset is not changed.
virtual void | reopenRW ()
| Reopen the underlying IO stream for read/write access.
Int64 | seek (Int offset, ByteIO::SeekOption=ByteIO::Begin)
| This function sets the position on the given offset.
Int64 | seek (Int64 offset, ByteIO::SeekOption=ByteIO::Begin)
virtual void | fsync ()
| Fsync the file (i.e.

## 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 (Int64 bufSize)
| Set a new buffer size.
void | writeBuffer (Int64 offset, const char *buf, Int64 size)
| Write a buffer of given length into the file at given offset.
Int64 | readBuffer (Int64 offset, char *buf, Int64 size, Bool throwException)
| Read a buffer of given length from the file at given offset.
void | writeBlock (Int64 size, const char *buf)
| Write a block into the stream at the current offset.
Int64 | readBlock (Int64 size, char *buf, Bool throwException)
| Read a block from the stream at the current offset.
Protected Member Functions inherited from casacore::ByteIO
| ByteIO (const ByteIO &byteIO)
| Make copy constructor and assignment protected, so a user cannot use them (but a derived class can).
ByteIO & | operator= (const ByteIO &byteIO)

## Private Member Functions

| FilebufIO (const FilebufIO &that)
| Copy constructor, should not be used.
FilebufIO & | operator= (const FilebufIO &that)
| Assignment, should not be used.

## Private Attributes

Bool | itsSeekable
Bool | itsReadable
Bool | itsWritable
int | itsFile
Int64 | itsBufSize
Int64 | itsBufLen
char * | itsBuffer
Int64 | itsBufOffset
Int64 | itsOffset
Int64 | itsSeekOffset
Bool | itsDirty

## Additional Inherited Members

Public Types inherited from casacore::ByteIO
enum | OpenOption { Old , Update , Append , New , NewNoReplace , Scratch , Delete }
| Define the possible ByteIO open options. More...
enum | SeekOption { Begin , Current , End }
| Define the possible seek options. More...

## Detailed Description

Class for buffered IO on a file.

### Intended use:

Public interface

### Review Status

Reviewed By:
  UNKNOWN
Date Reviewed:
  before2004/08/25
Test programs:
  tByteIO

### Prerequisite

- ByteIO

### Synopsis

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 FilebufIO 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.

### Example

This example shows how FilebufIO 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).
FilebufIO fio (fd);
AipsIO stream (&fio);
// Read the data.
Int vali;
Bool valb;
stream >> vali >> valb;
casacore::AipsIO
Definition AipsIO.h:167
casacore::FilebufIO::FilebufIO
FilebufIO()
Default constructor.
casacore::Int
int Int
Definition aipstype.h:48
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40

### Motivation

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 92 of file FilebufIO.h.

## Constructor & Destructor Documentation

## FilebufIO() [1/3]

casacore::FilebufIO::FilebufIO | ( |  | ) |

Default constructor.

A stream can be attached using the attach function.

Referenced by FilebufIO(), and operator=().

## FilebufIO() [2/3]

casacore::FilebufIO::FilebufIO | ( | 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.

References bufferSize().

## ~FilebufIO()

virtual casacore::FilebufIO::~FilebufIO | ( |  | ) |

virtual

The destructor closes the file when it was owned and opened and not closed yet.

## FilebufIO() [3/3]

casacore::FilebufIO::FilebufIO | ( | const FilebufIO & | that | ) |

private

Copy constructor, should not be used.

References FilebufIO().

## Member Function Documentation

## attach()

void casacore::FilebufIO::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.

References bufferSize().

## bufferSize()

uInt casacore::FilebufIO::bufferSize | ( |  | ) | const

inline

Get the buffer size.

Definition at line 148 of file FilebufIO.h.

References itsBufSize.

Referenced by attach(), and FilebufIO().

## detach()

void casacore::FilebufIO::detach | ( | Bool | closeFile = False | ) |

protected

Detach the FILE.

Close it when needed.

References casacore::False.

## doSeek()

virtual Int64 casacore::FilebufIO::doSeek | ( | Int64 | offset,
|  | ByteIO::SeekOption | )

protected virtual

Reset the position pointer to the given value.

It returns the new position.

Implements casacore::ByteIO.

References casacore::offset().

## fileName()

virtual String casacore::FilebufIO::fileName | ( |  | ) | const

virtual

Get the file name of the file attached.

Reimplemented from casacore::ByteIO.

Reimplemented in casacore::RegularFileIO.

## fillRWFlags()

void casacore::FilebufIO::fillRWFlags | ( | int | fd | ) |

protected

Determine if the file descriptor is readable and/or writable.

## fillSeekable()

void casacore::FilebufIO::fillSeekable | ( |  | ) |

protected

Determine if the file is seekable.

## flush()

virtual void casacore::FilebufIO::flush | ( |  | ) |

virtual

Flush the current buffer.

Reimplemented from casacore::ByteIO.

## isReadable()

virtual Bool casacore::FilebufIO::isReadable | ( |  | ) | const

virtual

Is the IO stream readable?

Implements casacore::ByteIO.

## isSeekable()

virtual Bool casacore::FilebufIO::isSeekable | ( |  | ) | const

virtual

Is the IO stream seekable?

Implements casacore::ByteIO.

## isWritable()

virtual Bool casacore::FilebufIO::isWritable | ( |  | ) | const

virtual

Is the IO stream writable?

Implements casacore::ByteIO.

## length()

virtual Int64 casacore::FilebufIO::length | ( |  | ) |

virtual

Get the length of the byte stream.

Implements casacore::ByteIO.

## operator=()

FilebufIO & casacore::FilebufIO::operator= | ( | const FilebufIO & | that | ) |

private

Assignment, should not be used.

References FilebufIO().

## read()

virtual Int64 casacore::FilebufIO::read | ( | Int64 | 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 casacore::ByteIO.

References casacore::size(), and casacore::True.

## readBlock()

Int64 casacore::FilebufIO::readBlock | ( | Int64 | 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.

References casacore::size().

## readBuffer()

Int64 casacore::FilebufIO::readBuffer | ( | Int64 | offset,
|  | char * | buf,
|  | Int64 | size,
|  | Bool | throwException )

protected

Read a buffer of given length from the file at given offset.

References casacore::offset(), and casacore::size().

## resync()

virtual void casacore::FilebufIO::resync | ( |  | ) |

virtual

Resync the file (i.e.

empty the current buffer).

Reimplemented from casacore::ByteIO.

## setBuffer()

void casacore::FilebufIO::setBuffer | ( | Int64 | bufSize | ) |

protected

Set a new buffer size.

If a buffer was already existing, flush and delete it.

## truncate()

virtual void casacore::FilebufIO::truncate | ( | Int64 | size | ) |

virtual

Truncate the file to the given size.

Reimplemented from casacore::ByteIO.

References casacore::size().

## write()

virtual void casacore::FilebufIO::write | ( | Int64 | size,
|  | const void * | buf )

virtual

Write the number of bytes.

Implements casacore::ByteIO.

References casacore::size().

## writeBlock()

void casacore::FilebufIO::writeBlock | ( | Int64 | 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.

References casacore::size().

## writeBuffer()

void casacore::FilebufIO::writeBuffer | ( | Int64 | offset,
|  | const char * | buf,
|  | Int64 | size )

protected

Write a buffer of given length into the file at given offset.

References casacore::offset(), and casacore::size().

## Member Data Documentation

## itsBuffer

char* casacore::FilebufIO::itsBuffer

private

Definition at line 191 of file FilebufIO.h.

## itsBufLen

Int64 casacore::FilebufIO::itsBufLen

private

Definition at line 190 of file FilebufIO.h.

## itsBufOffset

Int64 casacore::FilebufIO::itsBufOffset

private

Definition at line 192 of file FilebufIO.h.

## itsBufSize

Int64 casacore::FilebufIO::itsBufSize

private

Definition at line 189 of file FilebufIO.h.

Referenced by bufferSize().

## itsDirty

Bool casacore::FilebufIO::itsDirty

private

Definition at line 195 of file FilebufIO.h.

## itsFile

int casacore::FilebufIO::itsFile

private

Definition at line 188 of file FilebufIO.h.

## itsOffset

Int64 casacore::FilebufIO::itsOffset

private

Definition at line 193 of file FilebufIO.h.

## itsReadable

Bool casacore::FilebufIO::itsReadable

private

Definition at line 186 of file FilebufIO.h.

## itsSeekable

Bool casacore::FilebufIO::itsSeekable

private

Definition at line 185 of file FilebufIO.h.

## itsSeekOffset

Int64 casacore::FilebufIO::itsSeekOffset

private

Definition at line 194 of file FilebufIO.h.

## itsWritable

Bool casacore::FilebufIO::itsWritable

private

Definition at line 187 of file FilebufIO.h.

The documentation for this class was generated from the following file:

- casa/IO/FilebufIO.h