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/DataIO.h rev 38226 11 * src/kits/support/DataIO.cpp rev 42177 12 */ 13 14 15/*! 16 \file DataIO.h 17 \ingroup support 18 \ingroup libbe 19 \brief Defines abstract BDataIO and BPositionIO and the derived BMallocIO and BMemoryIO classes. 20 21 Pure virtual BDataIO and BPositioIO classes provide 22 the protocol for Read()/Write()/Seek(). 23 24 BMallocIO and BMemoryIO classes implement the protocol, 25 as does BFile in the Storage Kit. 26*/ 27 28 29///// BDataIO ///// 30 31 32/*! 33 \class BDataIO 34 \ingroup support 35 \ingroup libbe 36 \brief Abstract interface for objects that provide read and write access to 37 data. 38 39 The interface provided by this class applies to objects or data that are 40 limited to reading and writing data. Classes derived from this class should 41 re-implement both the Read() and Write() method from this class. 42 43 Candidates of types of data or objects that should be derived from this class 44 are probably broadcasting media streams (which don't support reading at a 45 certain point in the data) or network streams that output data continuously. 46 Objects and data that support more advanced operations like seeking or 47 reading at writing at defined positions should derive their classes from 48 BPositionIO, which inherits this class. 49*/ 50 51 52/*! 53 \fn BDataIO::BDataIO() 54 \brief This constructor does nothing. 55*/ 56 57 58/*! 59 \fn BDataIO::~BDataIO() 60 \brief This destructor does nothing. 61*/ 62 63 64/*! 65 \fn virtual ssize_t BDataIO::Read(void *buffer, size_t size) = 0 66 \brief Pure virtual to read data. 67 68 Your implementation should copy data into \c buffer, with the maximum size 69 of \c size. 70 \return You should return the amount of bytes actually read, or an error code 71 in case of failure. 72*/ 73 74 75/*! 76 \fn virtual ssize_t BDataIO::Write(const void *buffer, size_t size) = 0 77 \brief Pure virtual to write data. 78 79 Your implementation should copy data from \c buffer, with the maximum size 80 of \c size. 81 \return You should return the amount of bytes actually written, or an error 82 code in case of failure. 83*/ 84 85 86//////////// BPositionIO 87 88 89/*! 90 \class BPositionIO 91 \ingroup support 92 \ingroup libbe 93 \brief Abstract interface that provides advanced read, write and seek access 94 to data. 95 96 The interface of this object applies to objects or data that allows 97 position-aware reading and writing of data. Classes that derive from this 98 class should at least re-implement ReadAt(), WriteAt(), Seek(), 99 Position(), SetSize() and GetSize() methods. 100 101 A good example of a form of data that can derive from this object, are 102 files. The BFile class derives from BPositionIO and provides this 103 interface to files. If your object or data only supports linear reading 104 and writing, consider deriving from the base-class BDataIO. 105 106 A final note, from BDataIO this class inherits Read() and Write(). The 107 default implementation is to read or write the data at the current 108 position indicated by Position(). Re-implement the methods if you require 109 a different behavior. 110*/ 111 112 113/*! 114 \fn BPositionIO::BPositionIO() 115 \brief This constructor does nothing. 116*/ 117 118 119/*! 120 \fn virtual BPositionIO::~BPositionIO() 121 \brief This destructor does nothing. 122*/ 123 124 125/*! 126 \fn virtual ssize_t BPositionIO::Read(void *buffer, size_t size) 127 \brief Read data from current position. 128 129 This method is derived from BDataIO. The default implementation reads data 130 from the current position of the cursor, pointed at by Position(). If you 131 require different behaviour, please look at BDataIO::Read() for what is 132 expected of this method. 133*/ 134 135 136/*! 137 \fn virtual ssize_t BPositionIO::Write(const void *buffer, size_t size) 138 \brief Write data to the current position. 139 140 This method is derived from BDataIO. The default implementation writes data 141 to the current position of the cursor, pointed at by Position(). If you 142 require different behaviour, please look at BDataIO::Write() for what is 143 expected of this method. 144*/ 145 146 147/*! 148 \fn virtual ssize_t BPositionIO::ReadAt(off_t position, void *buffer, size_t size) = 0 149 \brief Pure virtual to read data from a certain position. 150 151 Your implementation should copy data from the position indicated by 152 \a position into the \a buffer with the maximum size of \a size. 153 154 \return The amount of bytes actually read, or an error code. 155*/ 156 157 158/*! 159 \fn virtual ssize_t BPositionIO::WriteAt(off_t position, const void *buffer, size_t size) = 0 160 \brief Pure virtual to write data to a certain position. 161 162 Your implementation should copy data from \a buffer to the position indicated 163 by \a buffer with the maximum size of \a size. 164 165 \return The amount of bytes actually written, or an error code. 166*/ 167 168 169/*! 170 \fn virtual off_t BPositionIO::Seek(off_t position, uint32 seekMode) = 0 171 \brief Pure virtual to move the cursor to a certain position. 172 173 Your implementation should move the position of the cursor to the provided 174 point. What this actually means, depends on your object or data. 175 176 \param position An integer that defines a position. 177 \param seekMode You will get one of the following values: 178 - \c SEEK_SET Set the cursor to the position indicated by \c position. 179 - \c SEEK_END Set the cursor to the end of the buffer, and go 180 \c position beyond that. 181 - \c SEEK_CUR Set the cursor the the current position plus \c position. 182 \return The new position. 183*/ 184 185 186/*! 187 \fn virtual off_t BPositionIO::Position() const = 0 188 \brief Pure virtual to return the current position of the cursor. 189 190 \return Your implementation should return the current position of the cursor. 191*/ 192 193 194/*! 195 \fn virtual status_t BPositionIO::SetSize(off_t size) 196 \brief Set the size of the object or data. 197 198 The default implementation returns \c B_ERROR. If your object or data allows 199 the size to be changed, reimplement this method. 200 201 \return Return \c B_OK if everything succeeded, else return the appropriate 202 error code. 203*/ 204 205 206/*! 207 \fn virtual status_t BPositionIO::GetSize(off_t* size) const 208 \brief Get the size of the object or data. 209 210 The default implementation uses Seek() with the \c SEEK_END flag to 211 determine the size of the buffer. If your data or object has a different way 212 of determining size, reimplement this method. 213 214 Please check that NULL is not passed into \c size if you reimplement it in 215 your class. 216 217 \param[out] size The size of the object is put into this parameter. 218 \return This method returns \c B_OK on success or an error code on error. 219 \see Seek() 220*/ 221 222 223//////////// BMemoryIO 224 225 226/*! 227 \class BMemoryIO 228 \ingroup support 229 \ingroup libbe 230 \brief A BPositionIO derived class that works on memory buffers. 231 232 This class is used if you require access that confirms to the BPositionIO 233 interface on memory buffers that you created. If you would like to use that 234 interface on new buffers, have a look at BMallocIO. 235 236 This class is particularly useful if you would like to use a class or method 237 that are written to make use of the BPositionIO interface. It might also 238 be used for 'secure' reading and writing from buffers, since this class 239 automatically checks the bounds of anything you might want to do. 240 241 This class reimplements the Read(), Write(), ReadAt(), Writeat(), Seek() and 242 Position() interface from BPositionIO. 243*/ 244 245 246/*! 247 \fn BMemoryIO::BMemoryIO(void *data, size_t length) 248 \brief Create a read/write object. 249 250 \param data A pointer to the buffer to adopt. 251 \param length The size of the buffer. 252 \see BMemoryIO(const void *buffer, size_t length) for a read-only 253 implementation. 254*/ 255 256 257/*! 258 \fn BMemoryIO::BMemoryIO(const void *buffer, size_t length) 259 \brief Create a read-only object. 260 261 \param buffer A pointer to the \c const (read-only) buffer to adopt. 262 \param length The size of the buffer. 263 \see BMemoryIO(void *buffer, size_t length) for a read-write implementation. 264*/ 265 266 267/*! 268 \fn BMemoryIO::~BMemoryIO() 269 \brief The destructor does nothing. 270*/ 271 272 273/*! 274 \fn ssize_t BMemoryIO::ReadAt(off_t pos, void *buffer, size_t size) 275 \brief Read from a given position. 276 277 \param[in] pos The offset where to start reading data. 278 \param[out] buffer The buffer to copy the read bytes into. 279 \param[in] size The size of the \a buffer. 280 \return The amount of read bytes or an error code. 281 \retval B_BAD_VALUE The position is less than zero or the buffer given on 282 construction is invalid. 283*/ 284 285 286/*! 287 \fn ssize_t BMemoryIO::WriteAt(off_t pos, const void *buffer, size_t size) 288 \brief Write at a given position. 289 290 \param pos The offset to write to. 291 \param buffer The buffer to copy the bytes from. 292 \param size The number of bytes to write. 293 \return The amount of bytes written or an error code. 294 \retval B_NOT_ALLOWED The object is constructed as a read-only object. 295 \retval B_BAD_VALUE The position is less than zero or the buffer given on 296 construction is invalid. 297*/ 298 299 300/*! 301 \fn off_t BMemoryIO::Seek(off_t position, uint32 seek_mode) 302 \brief Move the cursor to a given position. 303 304 \param position The position to move the cursor to. 305 \param seek_mode The mode determines where the cursor is placed. 306 Possibilities: 307 - \c SEEK_SET The cursor is set to \a position. 308 - \c SEEK_CUR The \a position is added to the current position of the 309 cursor. 310 - \c SEEK_END The cursor is put at the end of the data, plus 311 \a position added to it. 312 \return The new position. 313*/ 314 315 316/*! 317 \fn off_t BMemoryIO::Position() const 318 \brief Return the current position. 319*/ 320 321 322/*! 323 \fn status_t BMemoryIO::SetSize(off_t size) 324 \brief Resize the buffer. 325 326 This method does not actually resize the buffer. If the new size is greater 327 than the size of the buffer, resizing will fail. It will only succeed if the 328 new size is less than the size of the buffer. The buffer itself will not be 329 resized though. 330 331 This method might be useful in some cases. If the buffer is larger than the 332 data it holds, changing the size will enable you to use the Seek() method 333 with the flag \c SEEK_END and not get an error if you read or write from 334 that position, since you actually have a buffer at the end. 335 336 \retval B_OK The buffer is resized. 337 \retval B_NOT_ALLOWED The buffer is read-only. 338 \retval B_ERROR The \c size is larger than the size of the buffer. 339*/ 340 341 342//////////// BMallocIO 343 344 345/*! 346 \class BMallocIO 347 \ingroup support 348 \ingroup libbe 349 \brief A BPositionIO derived class that creates a memory buffer. 350 351 This class creates a memory buffer and provides a BPositionIO interface to 352 work on it. The memory buffer grows and shrinks automatically. 353 This is especially useful if you want to use a method or function that 354 works on an object derived from BPositionIO and you want to do something with 355 the resulting data, or it could be useful if you want to read and write to 356 memory in a safe way, since this class has boundary checking. 357 358 BMallocIO allocates a buffer based on a certain block size. This provides a 359 mechanism that will prevent it from needing to allocate new memory too often. 360 The default block size is 256 bytes, you can change it with SetBlockSize(). If 361 you are sure you are going to use a bigger buffer, change the block size so 362 that you won't have to allocate more memory too often, especially if you use 363 this class in performance-critical code. 364 365 If you require a BPositionIO derived object that works on buffers you 366 provide, have a look at BMemoryIO. 367*/ 368 369 370/*! 371 \fn BMallocIO::BMallocIO() 372 \brief Create a new memory buffer with block size 256. 373 \see SetBlockSize() 374*/ 375 376 377/*! 378 \fn BMallocIO::~BMallocIO() 379 \brief Destroy the object and free the internal buffer. 380*/ 381 382 383/*! 384 \fn ssize_t BMallocIO::ReadAt(off_t pos, void *buffer, size_t size) 385 \brief Read data at a certain position. 386 387 \param[in] pos Offset into the data where to read from. 388 \param[out] buffer The buffer to copy the read bytes in. 389 \param [in] size Size of the buffer. 390 \return The number of read bytes, or \c B_BAD_VALUE if 391 the provided \a buffer is invalid. 392*/ 393 394 395/*! 396 \fn ssize_t BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size) 397 \brief Write data to a certain position. 398 399 \param pos Offset into the data where to write to. 400 \param buffer The buffer to copy from. 401 \param size The size of the buffer. 402 \return The number of bytes written or \c B_BAD_VALUE if the provided. 403 \a buffer is invalid. 404*/ 405 406 407/*! 408 \fn off_t BMallocIO::Seek(off_t position, uint32 seekMode) 409 \brief Move the cursor to a given position. 410 411 \param position The position to move the cursor to. 412 \param seekMode The mode determines where the cursor is placed. Possibilities: 413 - \c SEEK_SET The cursor is set to \a position. 414 - \c SEEK_CUR The \c position is added to the current position of the 415 cursor. 416 - \c SEEK_END The cursor is put at the end of the data, plus 417 \a position added to it. 418 \return The new position. 419*/ 420 421 422/*! 423 \fn off_t BMallocIO::Position() const 424 \brief Return the position of the cursor. 425*/ 426 427 428/*! 429 \fn status_t BMallocIO::SetSize(off_t size) 430 \brief Change the size of the buffer. 431 432 This method changes the size of the current buffer. If \a size is smaller 433 than the current size, the data will be cleared. 434 435 \param size The new size of the buffer. 436 \retval B_OK Resizing the data succeeded. 437 \retval B_NO_MEMORY Failed to allocate the necessary memory. 438*/ 439 440 441/*! 442 \fn void BMallocIO::SetBlockSize(size_t blockSize) 443 \brief Change the block size to a certain value. 444 445 This class allocates memory in blocks. If you are in performance-critical 446 code you might want to tweak this setting to create a better performance in 447 case you know you are going to allocate more than the default blocksize of 448 256. 449 450 \param blockSize The new block size. 451*/ 452 453 454/*! 455 \fn const void *BMallocIO::Buffer() const 456 \brief Return a pointer to the internal buffer. 457 458 As with any pointer to internal buffers the Haiku API exposes, 459 make sure you don't change anything since it doesn't belong to you. 460*/ 461 462 463/*! 464 \fn size_t BMallocIO::BufferLength() const 465 \brief Return the number of bytes in the buffer. 466 467 This number doesn't have to be the same size as the buffer is. Because memory 468 is allocated in blocks the actual size of the buffer may be greater, but this 469 method only returns the number of bytes that are actually used. 470*/ 471