OpenShot Library | libopenshot-audio  0.1.9
juce_MemoryOutputStream.h
1 
2 /** @weakgroup juce_core-streams
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Writes data to an internal memory buffer, which grows as required.
33 
34  The data that was written into the stream can then be accessed later as
35  a contiguous block of memory.
36 
37  @tags{Core}
38 */
40 {
41 public:
42  //==============================================================================
43  /** Creates an empty memory stream, ready to be written into.
44  @param initialSize the initial amount of capacity to allocate for writing into
45  */
46  MemoryOutputStream (size_t initialSize = 256);
47 
48  /** Creates a memory stream for writing into into a pre-existing MemoryBlock object.
49 
50  Note that the destination block will always be larger than the amount of data
51  that has been written to the stream, because the MemoryOutputStream keeps some
52  spare capactity at its end. To trim the block's size down to fit the actual
53  data, call flush(), or delete the MemoryOutputStream.
54 
55  @param memoryBlockToWriteTo the block into which new data will be written.
56  @param appendToExistingBlockContent if this is true, the contents of the block will be
57  kept, and new data will be appended to it. If false,
58  the block will be cleared before use
59  */
60  MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo,
61  bool appendToExistingBlockContent);
62 
63  /** Creates a MemoryOutputStream that will write into a user-supplied, fixed-size
64  block of memory.
65  When using this mode, the stream will write directly into this memory area until
66  it's full, at which point write operations will fail.
67  */
68  MemoryOutputStream (void* destBuffer, size_t destBufferSize);
69 
70  /** Destructor.
71  This will free any data that was written to it.
72  */
73  ~MemoryOutputStream() override;
74 
75  //==============================================================================
76  /** Returns a pointer to the data that has been written to the stream.
77  @see getDataSize
78  */
79  const void* getData() const noexcept;
80 
81  /** Returns the number of bytes of data that have been written to the stream.
82  @see getData
83  */
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 (juce_wchar 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;
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* const blockToUse = nullptr;
124  MemoryBlock internalBlock;
125  void* externalData = nullptr;
126  size_t position = 0, size = 0, availableSize = 0;
127 
128  void trimExternalBlockSize();
129  char* prepareToWrite (size_t);
130 
131  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryOutputStream)
132 };
133 
134 /** Copies all the data that has been written to a MemoryOutputStream into another stream. */
135 OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead);
136 
137 } // namespace juce
138 
139 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
int64 getPosition() override
Returns the stream&#39;s current position.
The base class for streams that read data.
The JUCE String class!
Definition: juce_String.h:42
The base class for streams that write data to some kind of destination.
size_t getDataSize() const noexcept
Returns the number of bytes of data that have been written to the stream.
Writes data to an internal memory buffer, which grows as required.
A class to hold a resizable block of raw data.