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// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/IO/ByteIO.h>
32#include <casacore/casa/BasicSL/String.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// <summary> Class for buffered IO on a file.</summary>
37
38// <use visibility=export>
39
40// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tByteIO" demos="">
41// </reviewed>
42
43// <prerequisite>
44// <li> <linkto class=ByteIO>ByteIO</linkto>
45// </prerequisite>
46
47// <synopsis>
48// This class is a specialization of class
49// <linkto class=ByteIO>ByteIO</linkto>.
50// This class is doing IO on a file in a buffered way to reduce the number
51// of file accesses as much as possible.
52// It is part of the entire IO framework. It can for
53// instance be used to store data in canonical format in a file
54// in an IO-efficient way
55// <br>
56// The buffer size is dynamic, so any time it can be set as needed.
57// <p>
58// It is also possible to construct a <src>FilebufIO</src> object
59// from a file descriptor (e.g. for a pipe or socket).
60// The constructor will determine automatically if the file is
61// readable, writable and seekable.
62// </synopsis>
63
64// <example>
65// This example shows how FilebufIO can be used with an fd.
66// It uses the fd for a regular file, which could be done in an easier
67// way using class <linkto class=RegularFileIO>RegularFileIO</linkto>.
68// However, when using pipes or sockets, this would be the only way.
69// <srcblock>
70// // Get a file descriptor for the file.
71// int fd = open ("file.name");
72// // Use that as the source of AipsIO (which will also use CanonicalIO).
73// FilebufIO fio (fd);
74// AipsIO stream (&fio);
75// // Read the data.
76// Int vali;
77// Bool valb;
78// stream >> vali >> valb;
79// </srcblock>
80// </example>
81
82// <motivation>
83// The stdio package was used, but it proved to be very slow on SOlaris.
84// After a seek the buffer was refreshed, which increased the number
85// of file accesses enormously.
86// Also the interaction between reads and writes in stdio was poor.
87// </motivation>
88
89class FilebufIO : public ByteIO {
90 public:
91 // Default constructor.
92 // A stream can be attached using the attach function.
94
95 // Construct from the given file descriptor.
96 // Note that the destructor and the detach function implicitly close
97 // the file descriptor.
98 explicit FilebufIO(int fd, uInt bufferSize = 16384);
99
100 // Attach to the given file descriptor.
101 // Note that the destructor and the detach function implicitly close
102 // the file descriptor.
103 void attach(int fd, uInt bufferSize = 16384);
104
105 // The destructor closes the file when it was owned and opened and not
106 // closed yet.
107 virtual ~FilebufIO();
108
109 // Write the number of bytes.
110 virtual void write(Int64 size, const void* buf);
111
112 // Read <src>size</src> bytes from the File. Returns the number of bytes
113 // actually read. Will throw an exception (AipsError) if the requested
114 // number of bytes could not be read unless throwException is set to
115 // False. Will always throw an exception if the file is not readable or
116 // the system call returns an undocumented value.
117 virtual Int64 read(Int64 size, void* buf, Bool throwException = True);
118
119 // Flush the current buffer.
120 virtual void flush();
121
122 // Resync the file (i.e. empty the current buffer).
123 virtual void resync();
124
125 // Truncate the file to the given size.
126 virtual void truncate(Int64 size);
127
128 // Get the length of the byte stream.
129 virtual Int64 length();
130
131 // Is the IO stream readable?
132 virtual Bool isReadable() const;
133
134 // Is the IO stream writable?
135 virtual Bool isWritable() const;
136
137 // Is the IO stream seekable?
138 virtual Bool isSeekable() const;
139
140 // Get the file name of the file attached.
141 virtual String fileName() const;
142
143 // Get the buffer size.
144 uInt bufferSize() const { return itsBufSize; }
145
146 protected:
147 // Detach the FILE. Close it when needed.
148 void detach(Bool closeFile = False);
149
150 // Determine if the file descriptor is readable and/or writable.
151 void fillRWFlags(int fd);
152
153 // Determine if the file is seekable.
155
156 // Reset the position pointer to the given value. It returns the
157 // new position.
159
160 // Set a new buffer size.
161 // If a buffer was already existing, flush and delete it.
162 void setBuffer(Int64 bufSize);
163
164 // Write a buffer of given length into the file at given offset.
165 void writeBuffer(Int64 offset, const char* buf, Int64 size);
166
167 // Read a buffer of given length from the file at given offset.
168 Int64 readBuffer(Int64 offset, char* buf, Int64 size, Bool throwException);
169
170 // Write a block into the stream at the current offset.
171 // It is guaranteed that the block fits in a single buffer.
172 void writeBlock(Int64 size, const char* buf);
173
174 // Read a block from the stream at the current offset.
175 // It is guaranteed that the block fits in a single buffer.
176 Int64 readBlock(Int64 size, char* buf, Bool throwException);
177
178 private:
183 Int64 itsBufSize; // the buffer size
184 Int64 itsBufLen; // the current buffer length used
186 Int64 itsBufOffset; // file offset of current buffer
187 Int64 itsOffset; // current file offset
188 Int64 itsSeekOffset; // offset last seeked
189 Bool itsDirty; // data written into current buffer?
190
191 // Copy constructor, should not be used.
192 FilebufIO(const FilebufIO& that);
193
194 // Assignment, should not be used.
196};
197
198} // namespace casacore
199
200#endif
SeekOption
Define the possible seek options.
Definition ByteIO.h:77
ByteIO()
The constructor does nothing.
Definition ByteIO.h:167
virtual Bool isSeekable() const
Is the IO stream seekable?
uInt bufferSize() const
Get the buffer size.
Definition FilebufIO.h:144
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:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
int offset(int, int) const
compute a linear offset from array indicies
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
size_t size() const
Definition Block.h:566
const Bool True
Definition aipstype.h:41