2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 - ROLI Ltd.
6 Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
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/
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.
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
23 ==============================================================================
26 #include "MemoryInputStream.h"
30 MemoryInputStream::MemoryInputStream (const void* const sourceData
,
31 const size_t sourceDataSize
,
32 const bool keepInternalCopy
)
34 dataSize (sourceDataSize
),
41 MemoryInputStream::MemoryInputStream (const MemoryBlock
& sourceData
,
42 const bool keepInternalCopy
)
43 : data (sourceData
.getData()),
44 dataSize (sourceData
.getSize()),
51 void MemoryInputStream::createInternalCopy()
53 internalCopy
.malloc (dataSize
);
54 memcpy (internalCopy
, data
, dataSize
);
58 MemoryInputStream::~MemoryInputStream()
62 int64
MemoryInputStream::getTotalLength()
64 return (int64
) dataSize
;
67 int MemoryInputStream::read (void* const buffer
, const int howMany
)
69 wassert (buffer
!= nullptr && howMany
>= 0);
71 if (howMany
<= 0 || position
>= dataSize
)
74 const size_t num
= jmin ((size_t) howMany
, dataSize
- position
);
78 memcpy (buffer
, addBytesToPointer (data
, position
), num
);
85 bool MemoryInputStream::isExhausted()
87 return position
>= dataSize
;
90 bool MemoryInputStream::setPosition (const int64 pos
)
92 position
= (size_t) jlimit ((int64
) 0, (int64
) dataSize
, pos
);
96 int64
MemoryInputStream::getPosition()
98 return (int64
) position
;
102 //==============================================================================
105 class MemoryStreamTests
: public UnitTest
108 MemoryStreamTests() : UnitTest ("MemoryInputStream & MemoryOutputStream") {}
110 void runTest() override
112 beginTest ("Basics");
113 Random r
= getRandom();
115 int randomInt
= r
.nextInt();
116 int64 randomInt64
= r
.nextInt64();
117 double randomDouble
= r
.nextDouble();
118 String
randomString (createRandomWideCharString (r
));
120 MemoryOutputStream mo
;
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
);
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
);
141 static String
createRandomWideCharString (Random
& r
)
143 water_wchar buffer
[50] = { 0 };
145 for (int i
= 0; i
< numElementsInArray (buffer
) - 1; ++i
)
151 buffer
[i
] = (water_wchar
) (1 + r
.nextInt (0x10ffff - 1));
153 while (! CharPointer_UTF16::canRepresent (buffer
[i
]));
156 buffer
[i
] = (water_wchar
) (1 + r
.nextInt (0xff));
159 return CharPointer_UTF32 (buffer
);
163 static MemoryStreamTests memoryInputStreamUnitTests
;