28 dataSize (sourceDataSize)
32 internalCopy =
MemoryBlock (sourceData, sourceDataSize);
39 dataSize (sourceData.getSize())
43 internalCopy = sourceData;
49 : internalCopy (
std::move (source))
60 return (int64) dataSize;
65 jassert (buffer !=
nullptr && howMany >= 0);
67 if (howMany <= 0 || position >= dataSize)
70 auto num = jmin ((
size_t) howMany, dataSize - position);
74 memcpy (buffer, addBytesToPointer (data, position), num);
83 return position >= dataSize;
88 position = (size_t) jlimit ((int64) 0, (int64) dataSize, pos);
94 return (int64) position;
99 if (numBytesToSkip > 0)
108 class MemoryStreamTests :
public UnitTest 112 :
UnitTest (
"MemoryInputStream & MemoryOutputStream", UnitTestCategories::streams)
115 void runTest()
override 117 beginTest (
"Basics");
123 String randomString (createRandomWideCharString (r));
136 expect (mi.readInt() == randomInt);
137 expect (mi.readIntBigEndian() == randomInt);
138 expect (mi.readCompressedInt() == randomInt);
139 expectEquals (mi.readString(), randomString);
140 expect (mi.readInt64() == randomInt64);
141 expect (mi.readInt64BigEndian() == randomInt64);
142 expect (mi.readDouble() == randomDouble);
143 expect (mi.readDoubleBigEndian() == randomDouble);
145 const MemoryBlock data (
"abcdefghijklmnopqrstuvwxyz", 26);
155 size_t numBytesRead = 0;
158 while (numBytesRead < data.getSize())
160 numBytesRead += (size_t) stream.
read (&readBuffer[numBytesRead], 3);
162 expectEquals (stream.
getPosition(), (int64) numBytesRead);
164 expect (stream.
isExhausted() == (numBytesRead == data.getSize()));
167 expectEquals (stream.
getPosition(), (int64) data.getSize());
171 expect (readBuffer == data);
182 const int numBytesToSkip = 5;
184 while (numBytesRead < data.getSize())
187 numBytesRead += numBytesToSkip;
188 numBytesRead = std::min (numBytesRead, data.getSize());
190 expectEquals (stream.
getPosition(), (int64) numBytesRead);
192 expect (stream.
isExhausted() == (numBytesRead == data.getSize()));
195 expectEquals (stream.
getPosition(), (int64) data.getSize());
202 juce_wchar buffer [50] = { 0 };
204 for (
int i = 0; i < numElementsInArray (buffer) - 1; ++i)
210 buffer[i] = (juce_wchar) (1 + r.
nextInt (0x10ffff - 1));
215 buffer[i] = (juce_wchar) (1 + r.
nextInt (0xff));
222 static MemoryStreamTests memoryInputStreamUnitTests;
virtual bool writeDoubleBigEndian(double value)
Writes a 64-bit floating point value to the stream in a binary format.
int nextInt() noexcept
Returns the next random 32 bit integer.
bool nextBool() noexcept
Returns the next random boolean value.
static bool canRepresent(juce_wchar character) noexcept
Returns true if the given unicode character can be represented in this encoding.
int64 nextInt64() noexcept
Returns the next 64-bit random number.
This is a base class for classes that perform a unit test.
virtual bool writeInt64BigEndian(int64 value)
Writes a 64-bit integer to the stream in a big-endian byte order.
Wraps a pointer to a null-terminated UTF-32 character string, and provides various methods to operate...
const void * getData() const noexcept
Returns a pointer to the data that has been written to the stream.
void * getData() noexcept
Returns a void pointer to the data.
virtual bool writeIntBigEndian(int value)
Writes a 32-bit integer to the stream in a big-endian byte order.
double nextDouble() noexcept
Returns the next random floating-point number.
virtual bool writeInt64(int64 value)
Writes a 64-bit integer to the stream in a little-endian byte order.
size_t getDataSize() const noexcept
Returns the number of bytes of data that have been written to the stream.
virtual bool writeDouble(double value)
Writes a 64-bit floating point value to the stream in a binary format.
virtual bool writeInt(int value)
Writes a 32-bit integer to the stream in a little-endian byte order.
A random number generator.
virtual bool writeCompressedInt(int value)
Writes a condensed binary encoding of a 32-bit integer.
Writes data to an internal memory buffer, which grows as required.
virtual bool writeString(const String &text)
Stores a string in the stream in a binary format.
A class to hold a resizable block of raw data.