1 /* 2 ============================================================================== 3 4 This file is part of the Water library. 5 Copyright (c) 2016 ROLI Ltd. 6 Copyright (C) 2017 Filipe Coelho <falktx@falktx.com> 7 8 Permission is granted to use this software under the terms of the ISC license 9 http://www.isc.org/downloads/software-support-policy/isc-license/ 10 11 Permission to use, copy, modify, and/or distribute this software for any 12 purpose with or without fee is hereby granted, provided that the above 13 copyright notice and this permission notice appear in all copies. 14 15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD 16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, 18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF 19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 21 OF THIS SOFTWARE. 22 23 ============================================================================== 24 */ 25 26 #ifndef WATER_MEMORYOUTPUTSTREAM_H_INCLUDED 27 #define WATER_MEMORYOUTPUTSTREAM_H_INCLUDED 28 29 #include "OutputStream.h" 30 #include "../memory/MemoryBlock.h" 31 32 namespace water { 33 34 //============================================================================== 35 /** 36 Writes data to an internal memory buffer, which grows as required. 37 38 The data that was written into the stream can then be accessed later as 39 a contiguous block of memory. 40 */ 41 class MemoryOutputStream : public OutputStream 42 { 43 public: 44 //============================================================================== 45 /** Creates an empty memory stream, ready to be written into. 46 @param initialSize the intial amount of capacity to allocate for writing into 47 */ 48 MemoryOutputStream (size_t initialSize = 256); 49 50 /** Creates a memory stream for writing into into a pre-existing MemoryBlock object. 51 52 Note that the destination block will always be larger than the amount of data 53 that has been written to the stream, because the MemoryOutputStream keeps some 54 spare capactity at its end. To trim the block's size down to fit the actual 55 data, call flush(), or delete the MemoryOutputStream. 56 57 @param memoryBlockToWriteTo the block into which new data will be written. 58 @param appendToExistingBlockContent if this is true, the contents of the block will be 59 kept, and new data will be appended to it. If false, 60 the block will be cleared before use 61 */ 62 MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo, 63 bool appendToExistingBlockContent); 64 65 /** Destructor. 66 This will free any data that was written to it. 67 */ 68 ~MemoryOutputStream(); 69 70 //============================================================================== 71 /** Returns a pointer to the data that has been written to the stream. 72 @see getDataSize 73 */ 74 const void* getData() const noexcept; 75 76 /** Returns a pointer to the data that has been written to the stream and releases the buffer pointer. 77 @see getDataSize 78 */ 79 void* getDataAndRelease() noexcept; 80 81 /** Returns the number of bytes of data that have been written to the stream. 82 @see getData 83 */ getDataSize()84 size_t getDataSize() const noexcept { return size; } 85 86 /** Resets the stream, clearing any data that has been written to it so far. */ 87 void reset() noexcept; 88 89 /** Increases the internal storage capacity to be able to contain at least the specified 90 amount of data without needing to be resized. 91 */ 92 void preallocate (size_t bytesToPreallocate); 93 94 /** Appends the utf-8 bytes for a unicode character */ 95 bool appendUTF8Char (water_uchar character); 96 97 /** Returns a String created from the (UTF8) data that has been written to the stream. */ 98 String toUTF8() const; 99 100 /** Attempts to detect the encoding of the data and convert it to a string. 101 @see String::createStringFromData 102 */ 103 String toString() const; 104 105 /** Returns a copy of the stream's data as a memory block. */ 106 MemoryBlock getMemoryBlock() const; 107 108 //============================================================================== 109 /** If the stream is writing to a user-supplied MemoryBlock, this will trim any excess 110 capacity off the block, so that its length matches the amount of actual data that 111 has been written so far. 112 */ 113 void flush() override; 114 115 bool write (const void*, size_t) override; getPosition()116 int64 getPosition() override { return (int64) position; } 117 bool setPosition (int64) override; 118 int64 writeFromInputStream (InputStream&, int64 maxNumBytesToWrite) override; 119 bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override; 120 121 private: 122 //============================================================================== 123 MemoryBlock internalBlock; 124 MemoryBlock& blockToUse; 125 size_t position, size; 126 bool usingInternalBlock; 127 128 void trimExternalBlockSize(); 129 char* prepareToWrite (size_t); 130 131 CARLA_DECLARE_NON_COPY_CLASS (MemoryOutputStream) 132 }; 133 134 /** Copies all the data that has been written to a MemoryOutputStream into another stream. */ 135 OutputStream& operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead); 136 137 } 138 139 #endif // WATER_MEMORYOUTPUTSTREAM_H_INCLUDED 140