1/* 2 * Copyright 2007,Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stefano Ceccherini, burton666@libero.it 7 * Niels Sascha Reedijk, niels.reedijk@gmail.com 8 * 9 * Corresponds to: 10 * headers/os/support/BufferIO.h rev 38225 11 * src/kits/support/BufferIO.cpp rev 44862 12 */ 13 14 15/*! 16 \file BufferIO.h 17 \ingroup support 18 \ingroup libbe 19 \brief Provides the BBufferIO class. 20*/ 21 22 23/*! 24 \class BBufferIO 25 \ingroup support 26 \ingroup libbe 27 \brief A buffered adapter for BPositionIO objects. 28 29 This class differs from other classes derived from BPositionIO in a sense 30 that it does not actually provide an actual entity to be read or written 31 to, but rather acts like a "frontend" to a stream. This class especially 32 comes in handy when working with files that are constantly written and 33 rewritten and where you want do this writing buffered so that the hard 34 disk or the network will not have to be accessed so frequently. 35 36 This class works as follows. After constructing a BBufferIO object that 37 you want to be buffered, you can create this object. The constructor 38 takes a \a stream parameter that points to the object to be buffered. 39 40 You then use this object as a proxy to the resource you want to read 41 of or write to. As soon as you use ReadAt(), the buffer will be 42 initialized to the contents of the original stream, and subsequent calls 43 to the positions within the buffer will not be routed to the original 44 stream. In the same way WriteAt() will change the data in the buffer, 45 but not in the actual stream. In order to flush the changes to the 46 original stream, use the Flush() method. Deleting the object when you are 47 done with it will also flush the stream and update the original stream. 48 49 \note This class is not meant to be used in cases where the 50 original stream requires to be in a consistent state. Neither should 51 this class be used as a way to perform 'atomic' writes, because the 52 object might need to do partial writes if it needs to 'move' the 53 buffer. This happens for instance if the original stream is bigger 54 than the buffer. 55*/ 56 57/*! 58 \fn BBufferIO::BBufferIO(BPositionIO *stream, size_t bufferSize, 59 bool ownsStream) 60 \brief Initialize a BBufferIO object. 61 62 The constructor will create a buffer of the given size 63 and associate the object with the given BPositionIO stream. 64 65 \param stream A pointer to a BPositionIO object. 66 \param bufferSize The size of the buffer that the object will allocate and 67 use. 68 \param ownsStream Specifies if the object will delete the stream on 69 destruction. 70*/ 71 72/*! 73 \fn BBufferIO::~BBufferIO() 74 \brief Free the resources allocated by the object 75 76 Flush pending changes to the stream and free the allocated memory. 77 If the \c owns_stream property is \c true, the destructor also 78 deletes the stream associated with the BBufferIO object. 79*/ 80 81 82/*! 83 \fn ssize_t BBufferIO::ReadAt(off_t pos, void *buffer, size_t size) 84 \brief Read the specified amount of bytes at the given position. 85 86 \param pos The offset into the stream where to read. 87 \param buffer A pointer to a buffer where to copy the read data. 88 \param size The amount of bytes to read. 89 90 \return The amount of bytes actually read, or an error code. 91 \retval B_NO_INIT The object is not associated with a valid BPositionIO 92 stream. 93 \retval B_BAD_VALUE The \c buffer parameter is not valid. 94*/ 95 96/*! 97 \fn ssize_t BBufferIO::WriteAt(off_t pos, const void *buffer, size_t size) 98 \brief Write the specified amount of bytes at the given position. 99 100 \param pos The offset into the stream where to write. 101 \param buffer A pointer to a buffer which contains the data to write. 102 \param size The amount of bytes to write. 103 104 \return The amount of bytes actually written, or an error code. 105 \retval B_NO_INIT The object is not associated with a valid BPositionIO 106 stream. 107 \retval B_BAD_VALUE The \c buffer parameter is not valid. 108*/ 109 110/*! 111 \fn off_t BBufferIO::Seek(off_t position, uint32 seekMode) 112 \brief Set the position in the stream. 113 114 Set the position in the stream where the Read() and Write() functions 115 (inherited from BPositionIO) begin reading and writing. 116 How the position argument is understood depends on the seek_mode flag. 117 118 \param position The position where you want to seek. 119 \param seekMode Can have three values: 120 - \c SEEK_SET The position passed is an offset from the beginning of 121 the stream; in other words, the current position is set to 122 position. For this mode, position should be a positive value. 123 - \c SEEK_CUR The position argument is an offset from the current 124 position; the value of the argument is added to the current 125 position. 126 - \c SEEK_END. The position argument is an offset from the end of the 127 stream. In this mode the position argument should be negative 128 (or zero). 129 130 \return The current position as an offset in bytes from the beginning of 131 the stream. 132 \retval B_NO_INIT The object is not associated with a valid BPositionIO 133 stream. 134*/ 135 136/*! 137 \fn off_t BBufferIO::Position() const 138 \brief Return the current position in the stream. 139 140 \return The current position as an offset in bytes 141 from the beginning of the stream. 142 \retval B_NO_INIT The object is not associated with a valid BPositionIO 143 stream. 144*/ 145 146/*! 147 \fn status_t BBufferIO::SetSize(off_t size) 148 \brief Call the SetSize() function of the assigned BPositionIO stream. 149 150 \param size The new size of the BPositionIO object. 151 152 \returns A status code. 153 \retval B_OK The stream is resized. 154 \retval B_NO_INIT The object is not associated with a valid BPositionIO 155 stream. 156*/ 157 158/*! 159 \fn status_t BBufferIO::Flush() 160 \brief Write pending modifications to the stream. 161 162 \return The amount of bytes written, or if it failed it will return an error 163 code. 164*/ 165 166 167/*! 168 \fn BPositionIO *BBufferIO::Stream() const 169 \brief Return a pointer to the stream specified on construction. 170 171 \return A pointer to the BPositionIO stream specified on construction. 172*/ 173 174 175/*! 176 \fn size_t BBufferIO::BufferSize() const 177 \brief Return the size of the internal buffer. 178 179 \return The size of the buffer allocated by the object. 180*/ 181 182 183/*! 184 \fn bool BBufferIO::OwnsStream() const 185 \brief Tell if the BBufferIO object "owns" the specified stream. 186 187 \retval true The object "owns" the stream and will destroy it upon 188 destruction. 189 \retval false The object does not own the stream. 190 191 \see SetOwnsStream() 192*/ 193 194 195/*! 196 \fn void BBufferIO::SetOwnsStream(bool owns_stream) 197 \brief Set the \c owns_stream property of the object. 198 199 \param owns_stream If you pass \c true, the object will delete the stream 200 upon destruction, if you pass \c false it will not. 201*/ 202 203 204/*! 205 \fn void BBufferIO::PrintToStream() const 206 \brief Print the object to stdout. 207*/ 208