OpenShot Library | libopenshot-audio 0.2.0
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
37{
38 char temp = 0;
39 read (&temp, 1);
40 return temp;
41}
42
44{
45 return readByte() != 0;
46}
47
49{
50 char temp[2];
51
52 if (read (temp, 2) == 2)
53 return (short) ByteOrder::littleEndianShort (temp);
54
55 return 0;
56}
57
59{
60 char temp[2];
61
62 if (read (temp, 2) == 2)
63 return (short) ByteOrder::bigEndianShort (temp);
64
65 return 0;
66}
67
69{
70 char temp[4];
71
72 if (read (temp, 4) == 4)
73 return (int) ByteOrder::littleEndianInt (temp);
74
75 return 0;
76}
77
79{
80 char temp[4];
81
82 if (read (temp, 4) == 4)
83 return (int) ByteOrder::bigEndianInt (temp);
84
85 return 0;
86}
87
89{
90 auto sizeByte = (uint8) readByte();
91
92 if (sizeByte == 0)
93 return 0;
94
95 const int numBytes = (sizeByte & 0x7f);
96
97 if (numBytes > 4)
98 {
99 jassertfalse; // trying to read corrupt data - this method must only be used
100 // to read data that was written by OutputStream::writeCompressedInt()
101 return 0;
102 }
103
104 char bytes[4] = {};
105
106 if (read (bytes, numBytes) != numBytes)
107 return 0;
108
109 auto num = (int) ByteOrder::littleEndianInt (bytes);
110 return (sizeByte >> 7) ? -num : num;
111}
112
114{
115 union { uint8 asBytes[8]; uint64 asInt64; } n;
116
117 if (read (n.asBytes, 8) == 8)
118 return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
119
120 return 0;
121}
122
124{
125 union { uint8 asBytes[8]; uint64 asInt64; } n;
126
127 if (read (n.asBytes, 8) == 8)
128 return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
129
130 return 0;
131}
132
134{
135 static_assert (sizeof (int32) == sizeof (float), "Union assumes float has the same size as an int32");
136 union { int32 asInt; float asFloat; } n;
137 n.asInt = (int32) readInt();
138 return n.asFloat;
139}
140
142{
143 union { int32 asInt; float asFloat; } n;
144 n.asInt = (int32) readIntBigEndian();
145 return n.asFloat;
146}
147
149{
150 union { int64 asInt; double asDouble; } n;
151 n.asInt = readInt64();
152 return n.asDouble;
153}
154
156{
157 union { int64 asInt; double asDouble; } n;
158 n.asInt = readInt64BigEndian();
159 return n.asDouble;
160}
161
163{
164 MemoryOutputStream buffer;
165
166 for (;;)
167 {
168 auto c = readByte();
169 buffer.writeByte (c);
170
171 if (c == 0)
172 return buffer.toUTF8();
173 }
174}
175
177{
178 MemoryOutputStream buffer;
179
180 for (;;)
181 {
182 auto c = readByte();
183
184 if (c == 0 || c == '\n')
185 break;
186
187 if (c == '\r')
188 {
189 auto lastPos = getPosition();
190
191 if (readByte() != '\n')
192 setPosition (lastPos);
193
194 break;
195 }
196
197 buffer.writeByte (c);
198 }
199
200 return buffer.toUTF8();
201}
202
203size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
204{
205 MemoryOutputStream mo (block, true);
206 return (size_t) mo.writeFromInputStream (*this, numBytes);
207}
208
210{
212 mo << *this;
213 return mo.toString();
214}
215
216//==============================================================================
217void InputStream::skipNextBytes (int64 numBytesToSkip)
218{
219 if (numBytesToSkip > 0)
220 {
221 auto skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
222 HeapBlock<char> temp (skipBufferSize);
223
224 while (numBytesToSkip > 0 && ! isExhausted())
225 numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
226 }
227}
228
229} // 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.
Very simple container class to hold a pointer to some data on the heap.
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