casacore
Loading...
Searching...
No Matches
ByteIO.h
Go to the documentation of this file.
1// # ByteIO.h: Abstract base class for IO on a byte stream
2// # Copyright (C) 1996,1999,2001
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_BYTEIO_H
27#define CASA_BYTEIO_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/BasicSL/String.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// <summary>Abstract base class for IO on a byte stream.</summary>
36
37// <use visibility=export>
38
39// <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tByteIO" demos="">
40// </reviewed>
41
42// <synopsis>
43// ByteIO is the abstract base class for all classes doing IO on
44// byte streams. Examples of derived classes are
45// <linkto class=RegularFileIO>RegularFileIO</linkto> and
46// <linkto class=MemoryIO>MemoryIO</linkto>.
47// <p>
48// ByteIO contains two enumerations, which define the possible
49// open and seek options on byte streams. These enumerations
50// are used throughout the IO framework.
51// </synopsis>
52
53// <motivation>
54// Make polymorphic operations on byte streams possible.
55// </motivation>
56
57class ByteIO {
58 public:
59 // Define the possible ByteIO open options.
61 Old = 1,
62 // read/write; file must exist.
64 // read/write; create file if not exist.
66 // read/write; create file if not exist.
68 // read/write; file may not exist yet.
70 // read/write; delete file at close.
72 // read/write; file must exist; delete at close.
74 };
75
76 // Define the possible seek options.
78 // Seek from beginning of file.
79 Begin = 1,
80 // Seek from current position.
82 // Seek from the end of the file.
84 };
85
86 // The constructor does nothing.
87 ByteIO();
88
89 virtual ~ByteIO();
90
91 // Write <src>size</src> bytes to the byte stream.
92 virtual void write(Int64 size, const void* buf) = 0;
93
94 // Write <src>size</src> bytes to the byte stream at <src>offset</src>.
95 // The file offset is not changed
96 virtual void pwrite(Int64 size, Int64 offset, const void* buf);
97
98 // Read <src>size</src> bytes from the byte stream. Returns the number of
99 // bytes actually read, or a negative number if an error occurred. Will also
100 // throw an Exception (AipsError) if the requested number of bytes could
101 // not be read unless throwException is set to False.
102 virtual Int64 read(Int64 size, void* buf, Bool throwException = True) = 0;
103
104 // Like read but reads from offset of start of the file
105 // The file offset is not changed
106 virtual Int64 pread(Int64 size, Int64 offset, void* buf, Bool throwException = True);
107
108 // Reopen the underlying IO stream for read/write access.
109 // Nothing will be done if the stream is writable already.
110 // Otherwise it will be reopened and an exception will be thrown
111 // if it is not possible to reopen it for read/write access.
112 // The default implementation in this base class throws a "not possible"
113 // exception if a reopen has to be done.
114 virtual void reopenRW();
115
116 // This function sets the position on the given offset.
117 // The seek option defines from which file position the seek is done.
118 // -1 is returned if not seekable.
119 // <group>
122 // </group>
123
124 // Flush the data to the file.
125 // The default implementation does nothing.
126 virtual void flush();
127
128 // Fsync the file (i.e. force the data to be physically written).
129 // The default implementation does nothing.
130 virtual void fsync();
131
132 // Resync the file (i.e. empty the current buffer).
133 // The default implementation does nothing.
134 virtual void resync();
135
136 // Truncate the file to the given size.
137 // The default implementation does nothing.
138 virtual void truncate(Int64 size);
139
140 // Get the file name of the file attached.
141 // The default implementation returns an empty string.
142 virtual String fileName() const;
143
144 // Get the length of the byte stream.
145 virtual Int64 length() = 0;
146
147 // Is the byte stream readable?
148 virtual Bool isReadable() const = 0;
149
150 // Is the byte stream writable?
151 virtual Bool isWritable() const = 0;
152
153 // Is the byte stream seekable?
154 virtual Bool isSeekable() const = 0;
155
156 protected:
157 // Make copy constructor and assignment protected, so a user cannot
158 // use them (but a derived class can).
159 // <group>
160 ByteIO(const ByteIO& byteIO);
161 ByteIO& operator=(const ByteIO& byteIO);
162 // </group>
163
165};
166
167inline ByteIO::ByteIO() {}
168
169inline ByteIO::ByteIO(const ByteIO&) {}
170
171inline ByteIO& ByteIO::operator=(const ByteIO&) { return *this; }
172
174 return doSeek(offset, option);
175}
177 return doSeek(Int64(offset), option);
178}
179
180} // namespace casacore
181
182#endif
virtual Int64 doSeek(Int64 offset, ByteIO::SeekOption)=0
virtual void reopenRW()
Reopen the underlying IO stream for read/write access.
virtual Int64 length()=0
Get the length of the byte stream.
virtual Bool isWritable() const =0
Is the byte stream writable?
SeekOption
Define the possible seek options.
Definition ByteIO.h:77
@ Begin
Seek from beginning of file.
Definition ByteIO.h:79
@ Current
Seek from current position.
Definition ByteIO.h:81
@ End
Seek from the end of the file.
Definition ByteIO.h:83
virtual Bool isReadable() const =0
Is the byte stream readable?
ByteIO & operator=(const ByteIO &byteIO)
Definition ByteIO.h:171
virtual String fileName() const
Get the file name of the file attached.
virtual void flush()
Flush the data to the file.
virtual Int64 read(Int64 size, void *buf, Bool throwException=True)=0
Read size bytes from the byte stream.
virtual void resync()
Resync the file (i.e.
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.
Int64 seek(Int offset, ByteIO::SeekOption=ByteIO::Begin)
This function sets the position on the given offset.
Definition ByteIO.h:176
virtual void truncate(Int64 size)
Truncate the file to the given size.
virtual void write(Int64 size, const void *buf)=0
Write size bytes to the byte stream.
virtual void fsync()
Fsync the file (i.e.
virtual Bool isSeekable() const =0
Is the byte stream seekable?
ByteIO()
The constructor does nothing.
Definition ByteIO.h:167
virtual void pwrite(Int64 size, Int64 offset, const void *buf)
Write size bytes to the byte stream at offset.
virtual ~ByteIO()
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:60
@ Scratch
read/write; delete file at close.
Definition ByteIO.h:71
@ Delete
read/write; file must exist; delete at close.
Definition ByteIO.h:73
@ Append
read/write; create file if not exist.
Definition ByteIO.h:65
@ NewNoReplace
read/write; file may not exist yet.
Definition ByteIO.h:69
@ New
read/write; create file if not exist.
Definition ByteIO.h:67
@ Update
read/write; file must exist.
Definition ByteIO.h:63
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
int offset(int, int) const
compute a linear offset from array indicies
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
int Int
Definition aipstype.h:48
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