LMMS
Loading...
Searching...
No Matches
MemoryInputStream.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the Water library.
5 Copyright (c) 2016 - ROLI Ltd.
6 Copyright (C) 2021 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#include "MemoryInputStream.h"
27
28namespace water {
29
30MemoryInputStream::MemoryInputStream (const void* const sourceData,
31 const size_t sourceDataSize,
32 const bool keepInternalCopy)
33 : data (sourceData),
34 dataSize (sourceDataSize),
35 position (0)
36{
37 if (keepInternalCopy)
39}
40
42 const bool keepInternalCopy)
43 : data (sourceData.getData()),
44 dataSize (sourceData.getSize()),
45 position (0)
46{
47 if (keepInternalCopy)
49}
50
57
61
66
67int MemoryInputStream::read (void* const buffer, const int howMany)
68{
69 wassert (buffer != nullptr && howMany >= 0);
70
71 if (howMany <= 0 || position >= dataSize)
72 return 0;
73
74 const size_t num = jmin ((size_t) howMany, dataSize - position);
75
76 if (num > 0)
77 {
78 memcpy (buffer, addBytesToPointer (data, position), num);
79 position += num;
80 }
81
82 return (int) num;
83}
84
89
91{
92 position = (size_t) jlimit ((int64) 0, (int64) dataSize, pos);
93 return true;
94}
95
100
101
102//==============================================================================
103#if WATER_UNIT_TESTS
104
105class MemoryStreamTests : public UnitTest
106{
107public:
108 MemoryStreamTests() : UnitTest ("MemoryInputStream & MemoryOutputStream") {}
109
110 void runTest() override
111 {
112 beginTest ("Basics");
113 Random r = getRandom();
114
115 int randomInt = r.nextInt();
116 int64 randomInt64 = r.nextInt64();
117 double randomDouble = r.nextDouble();
118 String randomString (createRandomWideCharString (r));
119
121 mo.writeInt (randomInt);
122 mo.writeIntBigEndian (randomInt);
123 mo.writeCompressedInt (randomInt);
124 mo.writeString (randomString);
125 mo.writeInt64 (randomInt64);
126 mo.writeInt64BigEndian (randomInt64);
127 mo.writeDouble (randomDouble);
128 mo.writeDoubleBigEndian (randomDouble);
129
130 MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
131 expect (mi.readInt() == randomInt);
132 expect (mi.readIntBigEndian() == randomInt);
133 expect (mi.readCompressedInt() == randomInt);
134 expectEquals (mi.readString(), randomString);
135 expect (mi.readInt64() == randomInt64);
136 expect (mi.readInt64BigEndian() == randomInt64);
137 expect (mi.readDouble() == randomDouble);
138 expect (mi.readDoubleBigEndian() == randomDouble);
139 }
140
141 static String createRandomWideCharString (Random& r)
142 {
143 water_wchar buffer [50] = { 0 };
144
145 for (int i = 0; i < numElementsInArray (buffer) - 1; ++i)
146 {
147 if (r.nextBool())
148 {
149 do
150 {
151 buffer[i] = (water_wchar) (1 + r.nextInt (0x10ffff - 1));
152 }
153 while (! CharPointer_UTF16::canRepresent (buffer[i]));
154 }
155 else
156 buffer[i] = (water_wchar) (1 + r.nextInt (0xff));
157 }
158
159 return CharPointer_UTF32 (buffer);
160 }
161};
162
163static MemoryStreamTests memoryInputStreamUnitTests;
164
165#endif
166
167}
int64_t int64
Definition basics.h:91
Definition MemoryOutputStream.h:42
Definition String.h:48
Definition MemoryBlock.h:39
size_t position
Definition MemoryInputStream.h:91
const void * getData() const noexcept
Definition MemoryInputStream.h:76
int64 getPosition() override
Definition MemoryInputStream.cpp:96
void createInternalCopy()
Definition MemoryInputStream.cpp:51
bool isExhausted() override
Definition MemoryInputStream.cpp:85
int read(void *destBuffer, int maxBytesToRead) override
Definition MemoryInputStream.cpp:67
~MemoryInputStream()
Definition MemoryInputStream.cpp:58
size_t dataSize
Definition MemoryInputStream.h:91
MemoryInputStream(const void *sourceData, size_t sourceDataSize, bool keepInternalCopyOfData)
Definition MemoryInputStream.cpp:30
int64 getTotalLength() override
Definition MemoryInputStream.cpp:62
HeapBlock< char > internalCopy
Definition MemoryInputStream.h:92
const void * data
Definition MemoryInputStream.h:90
bool setPosition(int64 pos) override
Definition MemoryInputStream.cpp:90
register unsigned i
Definition inflate.c:1575
#define wassert(expression)
JOCTET * buffer
Definition juce_JPEGLoader.cpp:302
constexpr int numElementsInArray(Type(&)[N]) noexcept
Definition juce_MathsFunctions.h:344
Definition AudioSampleBuffer.h:33
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
long long int64
Definition water.h:100
Type jlimit(const Type lowerLimit, const Type upperLimit, const Type valueToConstrain) noexcept
Definition MathsFunctions.h:169
Type * addBytesToPointer(Type *basePointer, IntegerType bytes) noexcept
Definition Memory.h:56
memcpy(hh, h, RAND_HEAD_LEN)
int r
Definition crypt.c:458
mo
Definition zipinfo.c:2287