2 * Copyright 2007-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Stefano Ceccherini, burton666@libero.it
7 * Niels Sascha Reedijk, niels.reedijk@gmail.com
8 * John Scipione, jscipione@gmail.com
11 * headers/os/support/DataIO.h hrev47418
12 * src/kits/support/DataIO.cpp hrev47418
20 \brief Defines abstract BDataIO and BPositionIO and the derived BMallocIO
21 and BMemoryIO classes.
23 Pure virtual BDataIO and BPositioIO classes provide the protocol for
24 Read(), Write(), and Seek().
26 BMallocIO and BMemoryIO classes implement the protocol, as does BFile in
38 \brief Abstract interface for objects that provide read and write access to
41 The interface provided by this class applies to objects or data that are
42 limited to reading and writing data. Classes derived from this class should
43 re-implement the Read() or the Write() method from this class or both.
45 Candidates of types of data or objects that should be derived from this class
46 are probably broadcasting media streams (which don't support reading at a
47 certain point in the data) or network streams that output data continuously.
48 Objects and data that support more advanced operations like seeking or
49 reading at writing at defined positions should derive their classes from
50 BPositionIO, which inherits this class.
57 \fn BDataIO::BDataIO()
58 \brief This constructor does nothing.
65 \fn BDataIO::~BDataIO()
66 \brief This destructor does nothing.
73 \fn virtual ssize_t BDataIO::Read(void* buffer, size_t size)
74 \brief Reads data from the object into a buffer.
76 Your implementation should copy data into \c buffer, with the maximum size
79 The default implementation is a no-op returning \c B_NOT_SUPPORTED.
81 \return You should return the amount of bytes actually read, or an error
82 code in case of failure.
89 \fn virtual ssize_t BDataIO::Write(const void* buffer, size_t size)
90 \brief Writes data from a buffer to the object.
92 Your implementation should copy data from \c buffer, with the maximum size
95 The default implementation is a no-op returning \c B_NOT_SUPPORTED.
97 \return You should return the amount of bytes actually written, or an error
98 code in case of failure.
105 \fn virtual status_t BDataIO::Flush()
106 \brief Writes pending data to underlying storage.
108 This method is relevant for BDataIO implementations that buffer data passed
109 to Write(). The Flush() implementation should make sure that all such data
110 are written to the underlying storage.
112 The default implementation is a no-op returning \c B_OK.
114 \return An error code indicating whether flushing the buffered data
122 \fn virtual status_t BDataIO::ReadExactly(void* buffer, size_t size, size_t* _bytesRead)
123 \brief Reads an exact amount of data from the object into a buffer.
125 This is a convenience wrapper method for Read() for code that expects the
126 exact number of bytes requested to be read. This method calls Read() in a
127 loop to read the data. It fails when Read() returns an error or fails to
128 read any more data (i.e. returns 0).
130 \param buffer Pointer to pre-allocated storage of at least \a size bytes
131 into which the data shall be read. Won't be dereferenced, when
133 \param size The number of bytes to be read.
134 \param _bytesRead Optional pointer to a pre-allocated size_t into which the
135 number of bytes actually read will be written. When the method
136 returns \c B_OK this will always be \a size. Can be \c NULL.
138 \return An error code indicating whether or not the method succeeded.
139 \retval B_OK All data have been read.
140 \retval B_PARTIAL_READ Read() didn't fail, but couldn't provide as many
148 \fn virtual status_t BDataIO::WriteExactly(const void* buffer, size_t size,
149 size_t* _bytesWritten)
150 \brief Writes an exact amount of data from a buffer to the object.
152 This is a convenience wrapper method for Write() for code that expects the
153 exact number of bytes given to be written. This method calls Write() in a
154 loop to write the data. It fails when Write() returns an error or fails to
155 write any more data (i.e. returns 0).
157 \param buffer Pointer to a buffer of at least \a size bytes containing the
158 data to be written. Won't be dereferenced, when \a size is 0.
159 \param size The number of bytes to be written.
160 \param _bytesWritten Optional pointer to a pre-allocated size_t into which
161 the number of bytes actually written will be written. When the
162 method returns \c B_OK this will always be \a size. Can be \c NULL.
164 \return An error code indicated whether the method succeeded.
165 \retval B_OK All data have been written.
166 \retval B_PARTIAL_READ Write() didn't fail, but couldn't write as many
173 //////////// BPositionIO
180 \brief Abstract interface that provides advanced read, write and seek access
183 The interface of this object applies to objects or data that allows
184 position-aware reading and writing of data. Classes that derive from this
185 class should at least re-implement ReadAt(), WriteAt(), Seek(),
186 Position(), SetSize() and GetSize() methods.
188 A good example of a form of data that can derive from this object, are
189 files. The BFile class derives from BPositionIO and provides this
190 interface to files. If your object or data only supports linear reading
191 and writing, consider deriving from the base-class BDataIO.
193 A final note, from BDataIO this class inherits Read() and Write(). The
194 default implementation is to read or write the data at the current
195 position indicated by Position(). Re-implement the methods if you require
196 a different behavior.
203 \fn BPositionIO::BPositionIO()
204 \brief This constructor does nothing.
211 \fn virtual BPositionIO::~BPositionIO()
212 \brief This destructor does nothing.
219 \fn virtual ssize_t BPositionIO::Read(void* buffer, size_t size)
220 \brief Read data from current position.
222 This method is derived from BDataIO. The default implementation reads data
223 from the current position of the cursor, pointed at by Position(). If you
224 require different behaviour, please look at BDataIO::Read() for what is
225 expected of this method.
232 \fn virtual ssize_t BPositionIO::Write(const void *buffer, size_t size)
233 \brief Write data to the current position.
235 This method is derived from BDataIO. The default implementation writes data
236 to the current position of the cursor, pointed at by Position(). If you
237 require different behaviour, please look at BDataIO::Write() for what is
238 expected of this method.
245 \fn virtual ssize_t BPositionIO::ReadAt(off_t position, void* buffer,
247 \brief Pure virtual to read data from a certain position.
249 Your implementation should copy data from the position indicated by
250 \a position into the \a buffer with the maximum size of \a size.
252 \return The amount of bytes actually read, or an error code.
259 \fn virtual ssize_t BPositionIO::WriteAt(off_t position, const void *buffer,
261 \brief Pure virtual to write data to a certain position.
263 Your implementation should copy data from \a buffer to the position indicated
264 by \a buffer with the maximum size of \a size.
266 \return The amount of bytes actually written, or an error code.
273 \fn virtual status_t BPositionIO::ReadAtExactly(off_t position, void* buffer, size_t size, size_t* _bytesRead)
274 \brief Reads an exact amount of data from the object at the specified
275 position into a buffer.
277 This is a convenience wrapper method for ReadAt() for code that expects the
278 exact number of bytes requested to be read. This method calls ReadAt() in a
279 loop to read the data. It fails when ReadAt() returns an error or fails to
280 read any more data (i.e. returns 0).
282 \param position The object position at which to read the data.
283 \param buffer Pointer to pre-allocated storage of at least \a size bytes
284 into which the data shall be read. Won't be dereferenced, when
286 \param size The number of bytes to be read.
287 \param _bytesRead Optional pointer to a pre-allocated size_t into which the
288 number of bytes actually read will be written. When the method
289 returns \c B_OK this will always be \a size. Can be \c NULL.
291 \return An error code indicating whether or not the method succeeded.
292 \retval B_OK All data have been read.
293 \retval B_PARTIAL_READ ReadAt() didn't fail, but couldn't provide as many
301 \fn virtual status_t BPositionIO::WriteAtExactly(off_t position, const void* buffer, size_t size,
302 size_t* _bytesWritten)
303 \brief Writes an exact amount of data from a buffer to the object at the
306 This is a convenience wrapper method for WriteAt() for code that expects the
307 exact number of bytes given to be written. This method calls WriteAt() in a
308 loop to write the data. It fails when WriteAt() returns an error or fails to
309 write any more data (i.e. returns 0).
311 \param position The object position at which to write the data.
312 \param buffer Pointer to a buffer of at least \a size bytes containing the
313 data to be written. Won't be dereferenced, when \a size is 0.
314 \param size The number of bytes to be written.
315 \param _bytesWritten Optional pointer to a pre-allocated size_t into which
316 the number of bytes actually written will be written. When the
317 method returns \c B_OK this will always be \a size. Can be \c NULL.
319 \return An error code indicated whether the method succeeded.
320 \retval B_OK All data have been written.
321 \retval B_PARTIAL_READ WriteAt() didn't fail, but couldn't write as many
329 \fn virtual off_t BPositionIO::Seek(off_t position, uint32 seekMode) = 0
330 \brief Pure virtual to move the cursor to a certain position.
332 Your implementation should move the position of the cursor to the provided
333 point. What this actually means, depends on your object or data.
335 \param position An integer that defines a position.
336 \param seekMode You will get one of the following values:
337 - \c SEEK_SET Set the cursor to the position indicated by
339 - \c SEEK_END Set the cursor to the end of the buffer, and go
340 \c position beyond that.
341 - \c SEEK_CUR Set the cursor the the current position plus
343 \return The new position.
350 \fn virtual off_t BPositionIO::Position() const = 0
351 \brief Pure virtual to return the current position of the cursor.
353 \return Your implementation should return the current position of the cursor.
360 \fn virtual status_t BPositionIO::SetSize(off_t size)
361 \brief Set the size of the object or data.
363 The default implementation returns \c B_ERROR. If your object or data allows
364 the size to be changed, reimplement this method.
366 \return Return \c B_OK if everything succeeded, else return the appropriate
374 \fn virtual status_t BPositionIO::GetSize(off_t* size) const
375 \brief Get the size of the object or data.
377 The default implementation uses Seek() with the \c SEEK_END flag to
378 determine the size of the buffer. If your data or object has a different way
379 of determining size, reimplement this method.
381 Please check that NULL is not passed into \c size if you reimplement it in
384 \param[out] size The size of the object is put into this parameter.
386 \return This method returns \c B_OK on success or an error code on error.
395 //////////// BMemoryIO
402 \brief A BPositionIO derived class that works on memory buffers.
404 This class is used if you require access that confirms to the BPositionIO
405 interface on memory buffers that you created. If you would like to use that
406 interface on new buffers, have a look at BMallocIO.
408 This class is particularly useful if you would like to use a class or method
409 that are written to make use of the BPositionIO interface. It might also
410 be used for 'secure' reading and writing from buffers, since this class
411 automatically checks the bounds of anything you might want to do.
413 This class reimplements the Read(), Write(), ReadAt(), Writeat(), Seek() and
414 Position() interface from BPositionIO.
421 \fn BMemoryIO::BMemoryIO(void *data, size_t length)
422 \brief Create a read/write object.
424 \param data A pointer to the buffer to adopt.
425 \param length The size of the buffer.
427 \see BMemoryIO(const void *buffer, size_t length) for a read-only
435 \fn BMemoryIO::BMemoryIO(const void *buffer, size_t length)
436 \brief Create a read-only object.
438 \param buffer A pointer to the \c const (read-only) buffer to adopt.
439 \param length The size of the buffer.
441 \see BMemoryIO(void *buffer, size_t length) for a read-write implementation.
448 \fn BMemoryIO::~BMemoryIO()
449 \brief The destructor does nothing.
456 \fn ssize_t BMemoryIO::ReadAt(off_t pos, void *buffer, size_t size)
457 \brief Read from a given position.
459 \param[in] pos The offset where to start reading data.
460 \param[out] buffer The buffer to copy the read bytes into.
461 \param[in] size The size of the \a buffer.
463 \return The amount of read bytes or an error code.
464 \retval B_BAD_VALUE The position is less than zero or the buffer given on
465 construction is invalid.
472 \fn ssize_t BMemoryIO::WriteAt(off_t pos, const void *buffer, size_t size)
473 \brief Write at a given position.
475 \param pos The offset to write to.
476 \param buffer The buffer to copy the bytes from.
477 \param size The number of bytes to write.
479 \return The amount of bytes written or an error code.
480 \retval B_NOT_ALLOWED The object is constructed as a read-only object.
481 \retval B_BAD_VALUE The position is less than zero or the buffer given on
482 construction is invalid.
489 \fn off_t BMemoryIO::Seek(off_t position, uint32 seek_mode)
490 \brief Move the cursor to a given position.
492 \param position The position to move the cursor to.
493 \param seek_mode The mode determines where the cursor is placed.
494 Possibilities include:
495 - \c SEEK_SET The cursor is set to \a position.
496 - \c SEEK_CUR The \a position is added to the current position of
498 - \c SEEK_END The cursor is put at the end of the data, plus
499 \a position added to it.
501 \return The new position.
508 \fn off_t BMemoryIO::Position() const
509 \brief Return the current position.
511 \return The current position as an off_t.
518 \fn status_t BMemoryIO::SetSize(off_t size)
519 \brief Resize the buffer.
521 This method does not actually resize the buffer. If the new size is greater
522 than the size of the buffer, resizing will fail. It will only succeed if the
523 new size is less than the size of the buffer. The buffer itself will not be
526 This method might be useful in some cases. If the buffer is larger than the
527 data it holds, changing the size will enable you to use the Seek() method
528 with the flag \c SEEK_END and not get an error if you read or write from
529 that position, since you actually have a buffer at the end.
531 \retval B_OK The buffer is resized.
532 \retval B_NOT_ALLOWED The buffer is read-only.
533 \retval B_ERROR The \c size is larger than the size of the buffer.
539 //////////// BMallocIO
546 \brief A BPositionIO derived class that creates a memory buffer.
548 This class creates a memory buffer and provides a BPositionIO interface to
549 work on it. The memory buffer grows and shrinks automatically.
550 This is especially useful if you want to use a method or function that
551 works on an object derived from BPositionIO and you want to do something with
552 the resulting data, or it could be useful if you want to read and write to
553 memory in a safe way, since this class has boundary checking.
555 BMallocIO allocates a buffer based on a certain block size. This provides a
556 mechanism that will prevent it from needing to allocate new memory too often.
557 The default block size is 256 bytes, you can change it with SetBlockSize(). If
558 you are sure you are going to use a bigger buffer, change the block size so
559 that you won't have to allocate more memory too often, especially if you use
560 this class in performance-critical code.
562 If you require a BPositionIO derived object that works on buffers you
563 provide, have a look at BMemoryIO.
570 \fn BMallocIO::BMallocIO()
571 \brief Create a new memory buffer with block size 256.
580 \fn BMallocIO::~BMallocIO()
581 \brief Destroy the object and free the internal buffer.
588 \fn ssize_t BMallocIO::ReadAt(off_t pos, void *buffer, size_t size)
589 \brief Read data at a certain position.
591 \param[in] pos Offset into the data where to read from.
592 \param[out] buffer The buffer to copy the read bytes in.
593 \param [in] size Size of the buffer.
595 \return The number of read bytes, or \c B_BAD_VALUE if
596 the provided \a buffer is invalid.
603 \fn ssize_t BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size)
604 \brief Write data to a certain position.
606 \param pos Offset into the data where to write to.
607 \param buffer The buffer to copy from.
608 \param size The size of the buffer.
610 \return The number of bytes written or \c B_BAD_VALUE if the provided.
611 \a buffer is invalid.
618 \fn off_t BMallocIO::Seek(off_t position, uint32 seekMode)
619 \brief Move the cursor to a given position.
621 \param position The position to move the cursor to.
622 \param seekMode The mode determines where the cursor is placed. Possibilities:
623 - \c SEEK_SET The cursor is set to \a position.
624 - \c SEEK_CUR The \c position is added to the current position of the
626 - \c SEEK_END The cursor is put at the end of the data, plus
627 \a position added to it.
629 \return The new position as an off_t.
636 \fn off_t BMallocIO::Position() const
637 \brief Return the position of the cursor.
644 \fn status_t BMallocIO::SetSize(off_t size)
645 \brief Change the size of the buffer.
647 This method changes the size of the current buffer. If \a size is smaller
648 than the current size, the data will be cleared.
650 \param size The new size of the buffer.
652 \return A status code.
653 \retval B_OK Resizing the data succeeded.
654 \retval B_NO_MEMORY Failed to allocate the necessary memory.
661 \fn void BMallocIO::SetBlockSize(size_t blockSize)
662 \brief Change the block size to a certain value.
664 This class allocates memory in blocks. If you are in performance-critical
665 code you might want to tweak this setting to create a better performance in
666 case you know you are going to allocate more than the default block size of
669 \param blockSize The new block size.
676 \fn const void* BMallocIO::Buffer() const
677 \brief Return a pointer to the internal buffer.
679 As with any pointer to internal buffers the Haiku API exposes,
680 make sure you don't change anything since it doesn't belong to you.
687 \fn size_t BMallocIO::BufferLength() const
688 \brief Return the number of bytes in the buffer.
690 This number doesn't have to be the same size as the buffer is. Because memory
691 is allocated in blocks the actual size of the buffer may be greater, but this
692 method only returns the number of bytes that are actually used.