casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
FilebufIO.h
Go to the documentation of this file.
00001 //# FilebufIO.h: Class for buffered file IO
00002 //# Copyright (C) 1996,1997,1999,2001,2002
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: FilebufIO.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_FILEBUFIO_H
00029 #define CASA_FILEBUFIO_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/IO/ByteIO.h>
00034 #include <casa/BasicSL/String.h>
00035 
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 
00039 // <summary>Class for buffered file IO.</summary>
00040 
00041 // <use visibility=export>
00042 
00043 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tByteIO" demos="">
00044 // </reviewed>
00045 
00046 // <prerequisite> 
00047 //  <li> <linkto class=ByteIO>ByteIO</linkto>
00048 // </prerequisite>
00049 
00050 // <synopsis>
00051 // This class is a specialization of class
00052 // <linkto class=ByteIO>ByteIO</linkto>.
00053 // This class is doing IO on a file in a buffered way to reduce the number
00054 // of file accesses as much as possible.
00055 // It is part of the entire IO framework. It can for
00056 // instance be used to store data in canonical format in a file
00057 // in an IO-efficient way
00058 // <br>
00059 // The buffer size is dynamic, so any time it can be set as needed.
00060 // <p>
00061 // It is also possible to construct a <src>FilebufIO</src> object
00062 // from a file descriptor (e.g. for a pipe or socket).
00063 // The constructor will determine automatically if the file is
00064 // readable, writable and seekable.
00065 // </synopsis>
00066 
00067 // <example>
00068 // This example shows how FilebufIO can be used with an fd.
00069 // It uses the fd for a regular file, which could be done in an easier
00070 // way using class <linkto class=RegularFileIO>RegularFileIO</linkto>.
00071 // However, when using pipes or sockets, this would be the only way.
00072 // <srcblock>
00073 //    // Get a file descriptor for the file.
00074 //    int fd = open ("file.name");
00075 //    // Use that as the source of AipsIO (which will also use CanonicalIO).
00076 //    FilebufIO fio (fd);
00077 //    AipsIO stream (&fio);
00078 //    // Read the data.
00079 //    Int vali;
00080 //    Bool valb;
00081 //    stream >> vali >> valb;
00082 // </srcblock>
00083 // </example>
00084 
00085 // <motivation> 
00086 // The stdio package was used, but it proved to be very slow on Solaris.
00087 // After a seek the buffer was refreshed, which increased the number
00088 // of file accesses enormously.
00089 // Also the interaction between reads and writes in stdio required
00090 // speial care.
00091 // </motivation>
00092 
00093 
00094 class FilebufIO: public ByteIO
00095 {
00096 public: 
00097     // Default constructor.
00098     // A stream can be attached using the attach function.
00099     FilebufIO();
00100 
00101     // Construct from the given file descriptor.
00102     // Note that the detach function can be told to close
00103     // the file descriptor.
00104     explicit FilebufIO (int fd, uInt bufferSize=16384);
00105 
00106     // Attach to the given file descriptor.
00107     // Note that the detach function can be told to close
00108     // the file descriptor.
00109     void attach (int fd, uInt bufferSize=16384);
00110 
00111     // The destructor closes the file when it was owned and opened and not
00112     // closed yet.
00113     ~FilebufIO();
00114     
00115     // Write the number of bytes.
00116     virtual void write (uInt size, const void* buf);
00117 
00118     // Read <src>size</src> bytes from the File. Returns the number of bytes
00119     // actually read. Will throw an exception (AipsError) if the requested
00120     // number of bytes could not be read unless throwException is set to
00121     // False. Will always throw an exception if the file is not readable or
00122     // the system call returns an undocumented value.
00123     virtual Int read (uInt size, void* buf, Bool throwException=True);    
00124 
00125     // Flush the current buffer.
00126     void flush();
00127 
00128     // Resync the file (i.e. empty the current buffer).
00129     void resync();
00130   
00131     // Get the length of the byte stream.
00132     virtual Int64 length();
00133        
00134     // Is the IO stream readable?
00135     virtual Bool isReadable() const;
00136 
00137     // Is the IO stream writable?
00138     virtual Bool isWritable() const;
00139 
00140     // Is the IO stream seekable?
00141     virtual Bool isSeekable() const;
00142 
00143     // Get the file name of the file attached.
00144     virtual String fileName() const;
00145 
00146     // Get the buffer size.
00147     uInt bufferSize() const;
00148 
00149 protected:
00150     // Detach the FILE. Close it when needed.
00151     void detach (Bool closeFile=False);
00152 
00153     // Determine if the file descriptor is readable and/or writable.
00154     void fillRWFlags (int fd);
00155 
00156     // Determine if the file is seekable.
00157     void fillSeekable();
00158 
00159     // Reset the position pointer to the given value. It returns the
00160     // new position.
00161     virtual Int64 doSeek (Int64 offset, ByteIO::SeekOption);
00162 
00163     // Set a new buffer size.
00164     // If a buffer was already existing, flush and delete it.
00165     void setBuffer (uInt bufSize);
00166 
00167     // Write a buffer of given length into the file at given offset.
00168     void writeBuffer (Int64 offset, const char* buf, Int size);
00169 
00170     // Read a buffer of given length from the file at given offset.
00171     uInt readBuffer (Int64 offset, char* buf, uInt size,
00172                      Bool throwException);
00173 
00174     // Write a block into the stream at the current offset.
00175     // It is guaranteed that the block fits in a single buffer.
00176     void writeBlock (uInt size, const char* buf);
00177 
00178     // Read a block from the stream at the current offset.
00179     // It is guaranteed that the block fits in a single buffer.
00180     uInt readBlock (uInt size, char* buf, Bool throwException);
00181 
00182 private:
00183     Bool        itsSeekable;
00184     Bool        itsReadable;
00185     Bool        itsWritable;
00186     int         itsFile;
00187     uInt        itsBufSize;          // the buffer size
00188     uInt        itsBufLen;           // the current buffer length used
00189     char*       itsBuffer;
00190     Int64       itsBufOffset;        // file offset of current buffer
00191     Int64       itsOffset;           // current file offset
00192     Int64       itsSeekOffset;       // offset last seeked
00193     Bool        itsDirty;            // data written into current buffer?
00194 
00195     // Copy constructor, should not be used.
00196     FilebufIO (const FilebufIO& that);
00197 
00198     // Assignment, should not be used.
00199     FilebufIO& operator= (const FilebufIO& that);
00200 };
00201 
00202 
00203 inline uInt FilebufIO::bufferSize() const
00204 {
00205     return itsBufSize;
00206 }
00207 
00208 
00209 
00210 } //# NAMESPACE CASA - END
00211 
00212 #endif