casacore
Loading...
Searching...
No Matches
FilebufIO.h
Go to the documentation of this file.
1//# FilebufIO.h: Class for buffered IO on a file
2//# Copyright (C) 1996,1997,1999,2001,2002
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_FILEBUFIO_H
27#define CASA_FILEBUFIO_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/IO/ByteIO.h>
33#include <casacore/casa/BasicSL/String.h>
34
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38// <summary> Class for buffered IO on a file.</summary>
39
40// <use visibility=export>
41
42// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tByteIO" demos="">
43// </reviewed>
44
45// <prerequisite>
46// <li> <linkto class=ByteIO>ByteIO</linkto>
47// </prerequisite>
48
49// <synopsis>
50// This class is a specialization of class
51// <linkto class=ByteIO>ByteIO</linkto>.
52// This class is doing IO on a file in a buffered way to reduce the number
53// of file accesses as much as possible.
54// It is part of the entire IO framework. It can for
55// instance be used to store data in canonical format in a file
56// in an IO-efficient way
57// <br>
58// The buffer size is dynamic, so any time it can be set as needed.
59// <p>
60// It is also possible to construct a <src>FilebufIO</src> object
61// from a file descriptor (e.g. for a pipe or socket).
62// The constructor will determine automatically if the file is
63// readable, writable and seekable.
64// </synopsis>
65
66// <example>
67// This example shows how FilebufIO can be used with an fd.
68// It uses the fd for a regular file, which could be done in an easier
69// way using class <linkto class=RegularFileIO>RegularFileIO</linkto>.
70// However, when using pipes or sockets, this would be the only way.
71// <srcblock>
72// // Get a file descriptor for the file.
73// int fd = open ("file.name");
74// // Use that as the source of AipsIO (which will also use CanonicalIO).
75// FilebufIO fio (fd);
76// AipsIO stream (&fio);
77// // Read the data.
78// Int vali;
79// Bool valb;
80// stream >> vali >> valb;
81// </srcblock>
82// </example>
83
84// <motivation>
85// The stdio package was used, but it proved to be very slow on SOlaris.
86// After a seek the buffer was refreshed, which increased the number
87// of file accesses enormously.
88// Also the interaction between reads and writes in stdio was poor.
89// </motivation>
90
91
92class FilebufIO: public ByteIO
93{
94public:
95 // Default constructor.
96 // A stream can be attached using the attach function.
98
99 // Construct from the given file descriptor.
100 // Note that the destructor and the detach function implicitly close
101 // the file descriptor.
102 explicit FilebufIO (int fd, uInt bufferSize=16384);
103
104 // Attach to the given file descriptor.
105 // Note that the destructor and the detach function implicitly close
106 // the file descriptor.
107 void attach (int fd, uInt bufferSize=16384);
108
109 // The destructor closes the file when it was owned and opened and not
110 // closed yet.
111 virtual ~FilebufIO();
112
113 // Write the number of bytes.
114 virtual void write (Int64 size, const void* buf);
115
116 // Read <src>size</src> bytes from the File. Returns the number of bytes
117 // actually read. Will throw an exception (AipsError) if the requested
118 // number of bytes could not be read unless throwException is set to
119 // False. Will always throw an exception if the file is not readable or
120 // the system call returns an undocumented value.
121 virtual Int64 read (Int64 size, void* buf, Bool throwException=True);
122
123 // Flush the current buffer.
124 virtual void flush();
125
126 // Resync the file (i.e. empty the current buffer).
127 virtual void resync();
128
129 // Truncate the file to the given size.
130 virtual void truncate (Int64 size);
131
132 // Get the length of the byte stream.
133 virtual Int64 length();
134
135 // Is the IO stream readable?
136 virtual Bool isReadable() const;
137
138 // Is the IO stream writable?
139 virtual Bool isWritable() const;
140
141 // Is the IO stream seekable?
142 virtual Bool isSeekable() const;
143
144 // Get the file name of the file attached.
145 virtual String fileName() const;
146
147 // Get the buffer size.
149 { return itsBufSize; }
150
151protected:
152 // Detach the FILE. Close it when needed.
153 void detach (Bool closeFile=False);
154
155 // Determine if the file descriptor is readable and/or writable.
156 void fillRWFlags (int fd);
157
158 // Determine if the file is seekable.
160
161 // Reset the position pointer to the given value. It returns the
162 // new position.
164
165 // Set a new buffer size.
166 // If a buffer was already existing, flush and delete it.
167 void setBuffer (Int64 bufSize);
168
169 // Write a buffer of given length into the file at given offset.
170 void writeBuffer (Int64 offset, const char* buf, Int64 size);
171
172 // Read a buffer of given length from the file at given offset.
173 Int64 readBuffer (Int64 offset, char* buf, Int64 size,
174 Bool throwException);
175
176 // Write a block into the stream at the current offset.
177 // It is guaranteed that the block fits in a single buffer.
178 void writeBlock (Int64 size, const char* buf);
179
180 // Read a block from the stream at the current offset.
181 // It is guaranteed that the block fits in a single buffer.
182 Int64 readBlock (Int64 size, char* buf, Bool throwException);
183
184private:
189 Int64 itsBufSize; // the buffer size
190 Int64 itsBufLen; // the current buffer length used
192 Int64 itsBufOffset; // file offset of current buffer
193 Int64 itsOffset; // current file offset
194 Int64 itsSeekOffset; // offset last seeked
195 Bool itsDirty; // data written into current buffer?
196
197 // Copy constructor, should not be used.
198 FilebufIO (const FilebufIO& that);
199
200 // Assignment, should not be used.
202};
203
204
205} //# NAMESPACE CASACORE - END
206
207#endif
SeekOption
Define the possible seek options.
Definition ByteIO.h:80
virtual Bool isSeekable() const
Is the IO stream seekable?
uInt bufferSize() const
Get the buffer size.
Definition FilebufIO.h:148
Int64 readBlock(Int64 size, char *buf, Bool throwException)
Read a block from the stream at the current offset.
FilebufIO()
Default constructor.
void writeBuffer(Int64 offset, const char *buf, Int64 size)
Write a buffer of given length into the file at given offset.
void fillRWFlags(int fd)
Determine if the file descriptor is readable and/or writable.
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.
void setBuffer(Int64 bufSize)
Set a new buffer size.
virtual Bool isWritable() const
Is the IO stream writable?
virtual ~FilebufIO()
The destructor closes the file when it was owned and opened and not closed yet.
void attach(int fd, uInt bufferSize=16384)
Attach to the given file descriptor.
virtual Int64 length()
Get the length of the byte stream.
virtual String fileName() const
Get the file name of the file attached.
void detach(Bool closeFile=False)
Detach the FILE.
FilebufIO(const FilebufIO &that)
Copy constructor, should not be used.
virtual void resync()
Resync the file (i.e.
virtual Int64 read(Int64 size, void *buf, Bool throwException=True)
Read size bytes from the File.
virtual void write(Int64 size, const void *buf)
Write the number of bytes.
virtual void truncate(Int64 size)
Truncate the file to the given size.
FilebufIO & operator=(const FilebufIO &that)
Assignment, should not be used.
void fillSeekable()
Determine if the file is seekable.
virtual Bool isReadable() const
Is the IO stream readable?
virtual Int64 doSeek(Int64 offset, ByteIO::SeekOption)
Reset the position pointer to the given value.
FilebufIO(int fd, uInt bufferSize=16384)
Construct from the given file descriptor.
virtual void flush()
Flush the current buffer.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41