OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_InputStream.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23 namespace juce
24{
25
26int64 InputStream::getNumBytesRemaining()
27{
28 auto len = getTotalLength();
29
30 if (len >= 0)
31 len -= getPosition();
32
33 return len;
34}
35
36ssize_t InputStream::read (void* destBuffer, size_t size)
37{
38 ssize_t totalRead = 0;
39
40 while (size > 0)
41 {
42 auto numToRead = (int) std::min (size, (size_t) 0x70000000);
43 auto numRead = read (juce::addBytesToPointer (destBuffer, totalRead), numToRead);
44 jassert (numRead <= numToRead);
45
46 if (numRead < 0) return (ssize_t) numRead;
47 if (numRead == 0) break;
48
49 size -= (size_t) numRead;
50 totalRead += numRead;
51 }
52
53 return totalRead;
54}
55
56 char InputStream::readByte()
57{
58 char temp = 0;
59 read (&temp, 1);
60 return temp;
61}
62
63 bool InputStream::readBool()
64{
65 return readByte() != 0;
66}
67
68 short InputStream::readShort()
69{
70 char temp[2];
71
72 if (read (temp, 2) == 2)
73 return (short) ByteOrder::littleEndianShort (temp);
74
75 return 0;
76}
77
78 short InputStream::readShortBigEndian()
79{
80 char temp[2];
81
82 if (read (temp, 2) == 2)
83 return (short) ByteOrder::bigEndianShort (temp);
84
85 return 0;
86}
87
88 int InputStream::readInt()
89{
90 char temp[4];
91
92 if (read (temp, 4) == 4)
93 return (int) ByteOrder::littleEndianInt (temp);
94
95 return 0;
96}
97
98 int InputStream::readIntBigEndian()
99{
100 char temp[4];
101
102 if (read (temp, 4) == 4)
103 return (int) ByteOrder::bigEndianInt (temp);
104
105 return 0;
106}
107
108 int InputStream::readCompressedInt()
109{
110 auto sizeByte = (uint8) readByte();
111
112 if (sizeByte == 0)
113 return 0;
114
115 const int numBytes = (sizeByte & 0x7f);
116
117 if (numBytes > 4)
118 {
119 jassertfalse; // trying to read corrupt data - this method must only be used
120 // to read data that was written by OutputStream::writeCompressedInt()
121 return 0;
122 }
123
124 char bytes[4] = {};
125
126 if (read (bytes, numBytes) != numBytes)
127 return 0;
128
129 auto num = (int) ByteOrder::littleEndianInt (bytes);
130 return (sizeByte >> 7) ? -num : num;
131}
132
133int64 InputStream::readInt64()
134{
135 union { uint8 asBytes[8]; uint64 asInt64; } n;
136
137 if (read (n.asBytes, 8) == 8)
138 return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
139
140 return 0;
141}
142
143int64 InputStream::readInt64BigEndian()
144{
145 union { uint8 asBytes[8]; uint64 asInt64; } n;
146
147 if (read (n.asBytes, 8) == 8)
148 return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
149
150 return 0;
151}
152
153 float InputStream::readFloat()
154{
155 static_assert (sizeof (int32) == sizeof (float), "Union assumes float has the same size as an int32");
156 union { int32 asInt; float asFloat; } n;
157 n.asInt = (int32) readInt();
158 return n.asFloat;
159}
160
161 float InputStream::readFloatBigEndian()
162{
163 union { int32 asInt; float asFloat; } n;
164 n.asInt = (int32) readIntBigEndian();
165 return n.asFloat;
166}
167
168 double InputStream::readDouble()
169{
170 union { int64 asInt; double asDouble; } n;
171 n.asInt = readInt64();
172 return n.asDouble;
173}
174
175 double InputStream::readDoubleBigEndian()
176{
177 union { int64 asInt; double asDouble; } n;
178 n.asInt = readInt64BigEndian();
179 return n.asDouble;
180}
181
182 String InputStream::readString()
183{
184 MemoryOutputStream buffer;
185
186 for (;;)
187 {
188 auto c = readByte();
189 buffer.writeByte (c);
190
191 if (c == 0)
192 return buffer.toUTF8();
193 }
194}
195
196 String InputStream::readNextLine()
197{
198 MemoryOutputStream buffer;
199
200 for (;;)
201 {
202 auto c = readByte();
203
204 if (c == 0 || c == '\n')
205 break;
206
207 if (c == '\r')
208 {
209 auto lastPos = getPosition();
210
211 if (readByte() != '\n')
212 setPosition (lastPos);
213
214 break;
215 }
216
217 buffer.writeByte (c);
218 }
219
220 return buffer.toUTF8();
221}
222
223 size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
224{
225 MemoryOutputStream mo (block, true);
226 return (size_t) mo.writeFromInputStream (*this, numBytes);
227}
228
229 String InputStream::readEntireStreamAsString()
230{
231 MemoryOutputStream mo;
232 mo << *this;
233 return mo.toString();
234}
235
236//==============================================================================
237 void InputStream::skipNextBytes (int64 numBytesToSkip)
238{
239 if (numBytesToSkip > 0)
240 {
241 auto skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
242 HeapBlock<char> temp (skipBufferSize);
243
244 while (numBytesToSkip > 0 && ! isExhausted())
245 numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
246 }
247}
248
249} // namespace juce
juce::ByteOrder::bigEndianInt
static constexpr uint32 bigEndianInt(const void *bytes) noexcept
Definition juce_ByteOrder.h:207
juce::ByteOrder::littleEndianInt
static constexpr uint32 littleEndianInt(const void *bytes) noexcept
Definition juce_ByteOrder.h:199
juce::ByteOrder::swapIfLittleEndian
static Type swapIfLittleEndian(Type value) noexcept
Definition juce_ByteOrder.h:74
juce::ByteOrder::bigEndianShort
static constexpr uint16 bigEndianShort(const void *bytes) noexcept
Definition juce_ByteOrder.h:206
juce::ByteOrder::littleEndianShort
static constexpr uint16 littleEndianShort(const void *bytes) noexcept
Definition juce_ByteOrder.h:198
juce::ByteOrder::swapIfBigEndian
static Type swapIfBigEndian(Type value) noexcept
Definition juce_ByteOrder.h:63
juce::HeapBlock
Definition juce_HeapBlock.h:87
juce::InputStream::getPosition
virtual int64 getPosition()=0
juce::InputStream::readInt64
virtual int64 readInt64()
Definition juce_InputStream.cpp:133
juce::InputStream::readIntBigEndian
virtual int readIntBigEndian()
Definition juce_InputStream.cpp:98
juce::InputStream::readFloatBigEndian
virtual float readFloatBigEndian()
Definition juce_InputStream.cpp:161
juce::InputStream::getNumBytesRemaining
int64 getNumBytesRemaining()
Definition juce_InputStream.cpp:26
juce::InputStream::readFloat
virtual float readFloat()
Definition juce_InputStream.cpp:153
juce::InputStream::readCompressedInt
virtual int readCompressedInt()
Definition juce_InputStream.cpp:108
juce::InputStream::setPosition
virtual bool setPosition(int64 newPosition)=0
juce::InputStream::isExhausted
virtual bool isExhausted()=0
juce::InputStream::getTotalLength
virtual int64 getTotalLength()=0
juce::InputStream::readDoubleBigEndian
virtual double readDoubleBigEndian()
Definition juce_InputStream.cpp:175
juce::InputStream::readShort
virtual short readShort()
Definition juce_InputStream.cpp:68
juce::InputStream::readInt64BigEndian
virtual int64 readInt64BigEndian()
Definition juce_InputStream.cpp:143
juce::InputStream::skipNextBytes
virtual void skipNextBytes(int64 numBytesToSkip)
Definition juce_InputStream.cpp:237
juce::InputStream::readBool
virtual bool readBool()
Definition juce_InputStream.cpp:63
juce::InputStream::readShortBigEndian
virtual short readShortBigEndian()
Definition juce_InputStream.cpp:78
juce::InputStream::readIntoMemoryBlock
virtual size_t readIntoMemoryBlock(MemoryBlock &destBlock, ssize_t maxNumBytesToRead=-1)
Definition juce_InputStream.cpp:223
juce::InputStream::read
virtual int read(void *destBuffer, int maxBytesToRead)=0
juce::InputStream::readNextLine
virtual String readNextLine()
Definition juce_InputStream.cpp:196
juce::InputStream::readEntireStreamAsString
virtual String readEntireStreamAsString()
Definition juce_InputStream.cpp:229
juce::InputStream::readInt
virtual int readInt()
Definition juce_InputStream.cpp:88
juce::InputStream::readDouble
virtual double readDouble()
Definition juce_InputStream.cpp:168
juce::InputStream::readString
virtual String readString()
Definition juce_InputStream.cpp:182
juce::InputStream::readByte
virtual char readByte()
Definition juce_InputStream.cpp:56
juce::MemoryBlock
Definition juce_MemoryBlock.h:33
juce::MemoryOutputStream
Definition juce_MemoryOutputStream.h:36
juce::MemoryOutputStream::toUTF8
String toUTF8() const
Definition juce_MemoryOutputStream.cpp:189
juce::MemoryOutputStream::toString
String toString() const
Definition juce_MemoryOutputStream.cpp:195
juce::MemoryOutputStream::writeFromInputStream
int64 writeFromInputStream(InputStream &, int64 maxNumBytesToWrite) override
Definition juce_MemoryOutputStream.cpp:172
juce::OutputStream::writeByte
virtual bool writeByte(char byte)
Definition juce_OutputStream.cpp:83
juce::String
Definition juce_String.h:53