OpenShot Library | OpenShotAudio 0.2.2
juce_InputStream.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
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
23namespace juce
24{
25
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
57{
58 char temp = 0;
59 read (&temp, 1);
60 return temp;
61}
62
64{
65 return readByte() != 0;
66}
67
69{
70 char temp[2];
71
72 if (read (temp, 2) == 2)
73 return (short) ByteOrder::littleEndianShort (temp);
74
75 return 0;
76}
77
79{
80 char temp[2];
81
82 if (read (temp, 2) == 2)
83 return (short) ByteOrder::bigEndianShort (temp);
84
85 return 0;
86}
87
89{
90 char temp[4];
91
92 if (read (temp, 4) == 4)
93 return (int) ByteOrder::littleEndianInt (temp);
94
95 return 0;
96}
97
99{
100 char temp[4];
101
102 if (read (temp, 4) == 4)
103 return (int) ByteOrder::bigEndianInt (temp);
104
105 return 0;
106}
107
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
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
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
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
162{
163 union { int32 asInt; float asFloat; } n;
164 n.asInt = (int32) readIntBigEndian();
165 return n.asFloat;
166}
167
169{
170 union { int64 asInt; double asDouble; } n;
171 n.asInt = readInt64();
172 return n.asDouble;
173}
174
176{
177 union { int64 asInt; double asDouble; } n;
178 n.asInt = readInt64BigEndian();
179 return n.asDouble;
180}
181
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
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
223size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
224{
225 MemoryOutputStream mo (block, true);
226 return (size_t) mo.writeFromInputStream (*this, numBytes);
227}
228
230{
232 mo << *this;
233 return mo.toString();
234}
235
236//==============================================================================
237void 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
static JUCE_CONSTEXPR uint16 littleEndianShort(const void *bytes) noexcept
Turns 2 bytes into a little-endian integer.
static JUCE_CONSTEXPR uint16 bigEndianShort(const void *bytes) noexcept
Turns 2 bytes into a big-endian integer.
static JUCE_CONSTEXPR uint32 bigEndianInt(const void *bytes) noexcept
Turns 4 bytes into a big-endian integer.
static Type swapIfLittleEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is little-endian.
static Type swapIfBigEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is big-endian.
static JUCE_CONSTEXPR uint32 littleEndianInt(const void *bytes) noexcept
Turns 4 bytes into a little-endian integer.
virtual int64 getPosition()=0
Returns the offset of the next byte that will be read from the stream.
virtual int64 readInt64()
Reads eight bytes from the stream as a little-endian 64-bit value.
virtual int readIntBigEndian()
Reads four bytes from the stream as a big-endian 32-bit value.
virtual float readFloatBigEndian()
Reads four bytes as a 32-bit floating point value.
int64 getNumBytesRemaining()
Returns the number of bytes available for reading, or a negative value if the remaining length is not...
virtual float readFloat()
Reads four bytes as a 32-bit floating point value.
virtual int readCompressedInt()
Reads an encoded 32-bit number from the stream using a space-saving compressed format.
virtual bool setPosition(int64 newPosition)=0
Tries to move the current read position of the stream.
virtual bool isExhausted()=0
Returns true if the stream has no more data to read.
virtual int64 getTotalLength()=0
Returns the total number of bytes available for reading in this stream.
virtual double readDoubleBigEndian()
Reads eight bytes as a 64-bit floating point value.
virtual short readShort()
Reads two bytes from the stream as a little-endian 16-bit value.
virtual int64 readInt64BigEndian()
Reads eight bytes from the stream as a big-endian 64-bit value.
virtual void skipNextBytes(int64 numBytesToSkip)
Reads and discards a number of bytes from the stream.
virtual bool readBool()
Reads a boolean from the stream.
virtual short readShortBigEndian()
Reads two bytes from the stream as a little-endian 16-bit value.
virtual size_t readIntoMemoryBlock(MemoryBlock &destBlock, ssize_t maxNumBytesToRead=-1)
Reads from the stream and appends the data to a MemoryBlock.
virtual int read(void *destBuffer, int maxBytesToRead)=0
Reads some data from the stream into a memory buffer.
virtual String readNextLine()
Reads a UTF-8 string from the stream, up to the next linefeed or carriage return.
virtual String readEntireStreamAsString()
Tries to read the whole stream and turn it into a string.
virtual int readInt()
Reads four bytes from the stream as a little-endian 32-bit value.
virtual double readDouble()
Reads eight bytes as a 64-bit floating point value.
virtual String readString()
Reads a zero-terminated UTF-8 string from the stream.
virtual char readByte()
Reads a byte from the stream.
A class to hold a resizable block of raw data.
Writes data to an internal memory buffer, which grows as required.
String toUTF8() const
Returns a String created from the (UTF8) data that has been written to the stream.
String toString() const
Attempts to detect the encoding of the data and convert it to a string.
int64 writeFromInputStream(InputStream &, int64 maxNumBytesToWrite) override
Reads data from an input stream and writes it to this stream.
virtual bool writeByte(char byte)
Writes a single byte to the stream.
The JUCE String class!
Definition: juce_String.h:43