28 dataSize (sourceDataSize)
36 dataSize (sourceData.getSize())
42 void MemoryInputStream::createInternalCopy()
44 internalCopy.
malloc (dataSize);
45 memcpy (internalCopy, data, dataSize);
55 return (int64) dataSize;
60 jassert (buffer !=
nullptr && howMany >= 0);
62 if (howMany <= 0 || position >= dataSize)
65 auto num = jmin ((
size_t) howMany, dataSize - position);
69 memcpy (buffer, addBytesToPointer (data, position), num);
78 return position >= dataSize;
83 position = (size_t) jlimit ((int64) 0, (int64) dataSize, pos);
89 return (int64) position;
94 if (numBytesToSkip > 0)
101 class MemoryStreamTests :
public UnitTest 105 :
UnitTest (
"MemoryInputStream & MemoryOutputStream",
"Streams")
108 void runTest()
override 110 beginTest (
"Basics");
116 String randomString (createRandomWideCharString (r));
129 expect (mi.readInt() == randomInt);
130 expect (mi.readIntBigEndian() == randomInt);
131 expect (mi.readCompressedInt() == randomInt);
132 expectEquals (mi.readString(), randomString);
133 expect (mi.readInt64() == randomInt64);
134 expect (mi.readInt64BigEndian() == randomInt64);
135 expect (mi.readDouble() == randomDouble);
136 expect (mi.readDoubleBigEndian() == randomDouble);
138 const MemoryBlock data (
"abcdefghijklmnopqrstuvwxyz", 26);
148 size_t numBytesRead = 0;
151 while (numBytesRead < data.getSize())
153 numBytesRead += (size_t) stream.
read (&readBuffer[numBytesRead], 3);
155 expectEquals (stream.
getPosition(), (int64) numBytesRead);
157 expect (stream.
isExhausted() == (numBytesRead == data.getSize()));
160 expectEquals (stream.
getPosition(), (int64) data.getSize());
164 expect (readBuffer == data);
175 const int numBytesToSkip = 5;
177 while (numBytesRead < data.getSize())
180 numBytesRead += numBytesToSkip;
181 numBytesRead = std::min (numBytesRead, data.getSize());
183 expectEquals (stream.
getPosition(), (int64) numBytesRead);
185 expect (stream.
isExhausted() == (numBytesRead == data.getSize()));
188 expectEquals (stream.
getPosition(), (int64) data.getSize());
195 juce_wchar buffer [50] = { 0 };
197 for (
int i = 0; i < numElementsInArray (buffer) - 1; ++i)
203 buffer[i] = (juce_wchar) (1 + r.
nextInt (0x10ffff - 1));
208 buffer[i] = (juce_wchar) (1 + r.
nextInt (0xff));
215 static MemoryStreamTests memoryInputStreamUnitTests;
virtual bool writeDoubleBigEndian(double value)
Writes a 64-bit floating point value to the stream in a binary format.
void malloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Allocates a specified amount of memory.
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.
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.