1550d2417SBruno G. Albuquerque /* 2739f15e1SIngo Weinhold * Copyright 2009-2014, Haiku, Inc. All Rights Reserved. 3550d2417SBruno G. Albuquerque * Distributed under the terms of the MIT License. 4550d2417SBruno G. Albuquerque * 5550d2417SBruno G. Albuquerque * Authors: 6550d2417SBruno G. Albuquerque * Bruno Albuquerque, bga@bug-br.org.br 7550d2417SBruno G. Albuquerque */ 8550d2417SBruno G. Albuquerque 9550d2417SBruno G. Albuquerque #ifndef _DYNAMIC_BUFFER_H 10550d2417SBruno G. Albuquerque #define _DYNAMIC_BUFFER_H 11550d2417SBruno G. Albuquerque 12739f15e1SIngo Weinhold #include <DataIO.h> 13550d2417SBruno G. Albuquerque #include <SupportDefs.h> 14550d2417SBruno G. Albuquerque 15550d2417SBruno G. Albuquerque 16739f15e1SIngo Weinhold class DynamicBuffer : public BDataIO { 17550d2417SBruno G. Albuquerque public: 18ef93221dSBruno G. Albuquerque DynamicBuffer(size_t initialSize = 0); 19550d2417SBruno G. Albuquerque ~DynamicBuffer(); 20550d2417SBruno G. Albuquerque 21a12096e5SBruno G. Albuquerque DynamicBuffer(const DynamicBuffer& buffer); 22a12096e5SBruno G. Albuquerque 23550d2417SBruno G. Albuquerque // InitCheck() should be called to guarantee the object initialization 24550d2417SBruno G. Albuquerque // didn't fail. If it does not return B_OK, the initialization failed. 25550d2417SBruno G. Albuquerque status_t InitCheck() const; 26550d2417SBruno G. Albuquerque 27550d2417SBruno G. Albuquerque // Insert data at the end of the buffer. The buffer will be increased to 28550d2417SBruno G. Albuquerque // accomodate the data if needed. 29*6926863eSIngo Weinhold virtual ssize_t Write(const void* data, size_t size); 30550d2417SBruno G. Albuquerque 31550d2417SBruno G. Albuquerque // Remove data from the start of the buffer. Move the buffer start 32550d2417SBruno G. Albuquerque // pointer to point to the data following it. 33*6926863eSIngo Weinhold virtual ssize_t Read(void* data, size_t size); 34550d2417SBruno G. Albuquerque 35550d2417SBruno G. Albuquerque // Return a pointer to the underlying buffer. Note this will not 36550d2417SBruno G. Albuquerque // necessarily be a pointer to the start of the allocated memory as the 37550d2417SBruno G. Albuquerque // initial data may be positioned at an offset inside the buffer. In other 38550d2417SBruno G. Albuquerque // words, this returns a pointer to the start of data inside the buffer. 39550d2417SBruno G. Albuquerque unsigned char* Data() const; 40550d2417SBruno G. Albuquerque 41550d2417SBruno G. Albuquerque // Returns the actual buffer size. This is the amount of memory allocated 42550d2417SBruno G. Albuquerque // for the buffer. 43550d2417SBruno G. Albuquerque size_t Size() const; 44550d2417SBruno G. Albuquerque 45550d2417SBruno G. Albuquerque // Number of bytes of data still present in the buffer that can be 46550d2417SBruno G. Albuquerque // extracted through calls to Remove(). 47550d2417SBruno G. Albuquerque size_t BytesRemaining() const; 48550d2417SBruno G. Albuquerque 49550d2417SBruno G. Albuquerque // Dump some buffer statistics to stdout. 50550d2417SBruno G. Albuquerque void PrintToStream(); 51550d2417SBruno G. Albuquerque 52550d2417SBruno G. Albuquerque private: 53a12096e5SBruno G. Albuquerque status_t _GrowToFit(size_t size, bool exact = false); 54550d2417SBruno G. Albuquerque 55550d2417SBruno G. Albuquerque unsigned char* fBuffer; 56550d2417SBruno G. Albuquerque size_t fBufferSize; 57550d2417SBruno G. Albuquerque size_t fDataStart; 58550d2417SBruno G. Albuquerque size_t fDataEnd; 59550d2417SBruno G. Albuquerque 60550d2417SBruno G. Albuquerque status_t fInit; 61550d2417SBruno G. Albuquerque }; 62550d2417SBruno G. Albuquerque 63550d2417SBruno G. Albuquerque #endif // _DYNAMIC_BUFFER_H 64