casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
LargeFilebufIO.h
Go to the documentation of this file.
00001 //# LargeFilebufIO.h: Class for buffered IO on a large file
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: LargeFilebufIO.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_LARGEFILEBUFIO_H
00029 #define CASA_LARGEFILEBUFIO_H
00030 
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <casa/IO/ByteIO.h>
00035 #include <casa/BasicSL/String.h>
00036 
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 // <summary> Class for buffered IO on a large file.</summary>
00041 
00042 // <use visibility=export>
00043 
00044 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tByteIO" demos="">
00045 // </reviewed>
00046 
00047 // <prerequisite> 
00048 //  <li> <linkto class=ByteIO>ByteIO</linkto>
00049 // </prerequisite>
00050 
00051 // <synopsis>
00052 // This class is a specialization of class
00053 // <linkto class=ByteIO>ByteIO</linkto>.
00054 // This class is doing IO on a file in a buffered way to reduce the number
00055 // of file accesses as much as possible.
00056 // It is part of the entire IO framework. It can for
00057 // instance be used to store data in canonical format in a file
00058 // in an IO-efficient way
00059 // <br>
00060 // The buffer size is dynamic, so any time it can be set as needed.
00061 // <p>
00062 // It is also possible to construct a <src>LargeFilebufIO</src> object
00063 // from a file descriptor (e.g. for a pipe or socket).
00064 // The constructor will determine automatically if the file is
00065 // readable, writable and seekable.
00066 // </synopsis>
00067 
00068 // <example>
00069 // This example shows how LargeFilebufIO can be used with an fd.
00070 // It uses the fd for a regular file, which could be done in an easier
00071 // way using class <linkto class=RegularFileIO>RegularFileIO</linkto>.
00072 // However, when using pipes or sockets, this would be the only way.
00073 // <srcblock>
00074 //    // Get a file descriptor for the file.
00075 //    int fd = open ("file.name");
00076 //    // Use that as the source of AipsIO (which will also use CanonicalIO).
00077 //    LargeFilebufIO fio (fd);
00078 //    AipsIO stream (&fio);
00079 //    // Read the data.
00080 //    Int vali;
00081 //    Bool valb;
00082 //    stream >> vali >> valb;
00083 // </srcblock>
00084 // </example>
00085 
00086 // <motivation> 
00087 // The stdio package was used, but it proved to be very slow on SOlaris.
00088 // After a seek the buffer was refreshed, which increased the number
00089 // of file accesses enormously.
00090 // Also the interaction between reads and writes in stdio was poor.
00091 // </motivation>
00092 
00093 
00094 class LargeFilebufIO: public ByteIO
00095 {
00096 public: 
00097     // Default constructor.
00098     // A stream can be attached using the attach function.
00099     LargeFilebufIO();
00100 
00101     // Construct from the given file descriptor.
00102     // Note that the destructor and the detach function implicitly close
00103     // the file descriptor.
00104     explicit LargeFilebufIO (int fd, uInt bufferSize=16384);
00105 
00106     // Attach to the given file descriptor.
00107     // Note that the destructor and the detach function implicitly 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     ~LargeFilebufIO();
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     LargeFilebufIO (const LargeFilebufIO& that);
00197 
00198     // Assignment, should not be used.
00199     LargeFilebufIO& operator= (const LargeFilebufIO& that);
00200 };
00201 
00202 
00203 inline uInt LargeFilebufIO::bufferSize() const
00204 {
00205     return itsBufSize;
00206 }
00207 
00208 
00209 
00210 } //# NAMESPACE CASA - END
00211 
00212 #endif