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 
23 namespace 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 
203 size_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 //==============================================================================
217 void 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
juce::InputStream::readShort
virtual short readShort()
Reads two bytes from the stream as a little-endian 16-bit value.
Definition: juce_InputStream.cpp:48
juce::ByteOrder::littleEndianShort
static JUCE_CONSTEXPR uint16 littleEndianShort(const void *bytes) noexcept
Turns 2 bytes into a little-endian integer.
Definition: juce_ByteOrder.h:202
juce::InputStream::readFloatBigEndian
virtual float readFloatBigEndian()
Reads four bytes as a 32-bit floating point value.
Definition: juce_InputStream.cpp:141
juce::InputStream::readInt
virtual int readInt()
Reads four bytes from the stream as a little-endian 32-bit value.
Definition: juce_InputStream.cpp:68
juce::OutputStream::writeByte
virtual bool writeByte(char byte)
Writes a single byte to the stream.
Definition: juce_OutputStream.cpp:72
juce::HeapBlock< char >
juce::MemoryOutputStream
Writes data to an internal memory buffer, which grows as required.
Definition: juce_MemoryOutputStream.h:39
juce::InputStream::readDoubleBigEndian
virtual double readDoubleBigEndian()
Reads eight bytes as a 64-bit floating point value.
Definition: juce_InputStream.cpp:155
juce::InputStream::read
virtual int read(void *destBuffer, int maxBytesToRead)=0
Reads some data from the stream into a memory buffer.
juce::InputStream::getNumBytesRemaining
int64 getNumBytesRemaining()
Returns the number of bytes available for reading, or a negative value if the remaining length is not...
Definition: juce_InputStream.cpp:26
juce::InputStream::readInt64
virtual int64 readInt64()
Reads eight bytes from the stream as a little-endian 64-bit value.
Definition: juce_InputStream.cpp:113
juce::InputStream::readCompressedInt
virtual int readCompressedInt()
Reads an encoded 32-bit number from the stream using a space-saving compressed format.
Definition: juce_InputStream.cpp:88
juce::InputStream::getPosition
virtual int64 getPosition()=0
Returns the offset of the next byte that will be read from the stream.
juce::InputStream::setPosition
virtual bool setPosition(int64 newPosition)=0
Tries to move the current read position of the stream.
juce::InputStream::readByte
virtual char readByte()
Reads a byte from the stream.
Definition: juce_InputStream.cpp:36
juce::InputStream::readFloat
virtual float readFloat()
Reads four bytes as a 32-bit floating point value.
Definition: juce_InputStream.cpp:133
juce::MemoryOutputStream::toUTF8
String toUTF8() const
Returns a String created from the (UTF8) data that has been written to the stream.
Definition: juce_MemoryOutputStream.cpp:189
juce::InputStream::readBool
virtual bool readBool()
Reads a boolean from the stream.
Definition: juce_InputStream.cpp:43
juce::ByteOrder::swapIfLittleEndian
static Type swapIfLittleEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is little-endian.
Definition: juce_ByteOrder.h:78
juce::MemoryOutputStream::writeFromInputStream
int64 writeFromInputStream(InputStream &, int64 maxNumBytesToWrite) override
Reads data from an input stream and writes it to this stream.
Definition: juce_MemoryOutputStream.cpp:172
juce::InputStream::isExhausted
virtual bool isExhausted()=0
Returns true if the stream has no more data to read.
juce::InputStream::readShortBigEndian
virtual short readShortBigEndian()
Reads two bytes from the stream as a little-endian 16-bit value.
Definition: juce_InputStream.cpp:58
juce::ByteOrder::swapIfBigEndian
static Type swapIfBigEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is big-endian.
Definition: juce_ByteOrder.h:67
juce::InputStream::readEntireStreamAsString
virtual String readEntireStreamAsString()
Tries to read the whole stream and turn it into a string.
Definition: juce_InputStream.cpp:209
juce::InputStream::readIntBigEndian
virtual int readIntBigEndian()
Reads four bytes from the stream as a big-endian 32-bit value.
Definition: juce_InputStream.cpp:78
juce::InputStream::readString
virtual String readString()
Reads a zero-terminated UTF-8 string from the stream.
Definition: juce_InputStream.cpp:162
juce::ByteOrder::bigEndianShort
static JUCE_CONSTEXPR uint16 bigEndianShort(const void *bytes) noexcept
Turns 2 bytes into a big-endian integer.
Definition: juce_ByteOrder.h:210
juce::MemoryOutputStream::toString
String toString() const
Attempts to detect the encoding of the data and convert it to a string.
Definition: juce_MemoryOutputStream.cpp:195
juce::InputStream::getTotalLength
virtual int64 getTotalLength()=0
Returns the total number of bytes available for reading in this stream.
juce::String
The JUCE String class!
Definition: juce_String.h:42
juce::InputStream::readInt64BigEndian
virtual int64 readInt64BigEndian()
Reads eight bytes from the stream as a big-endian 64-bit value.
Definition: juce_InputStream.cpp:123
juce::ByteOrder::bigEndianInt
static JUCE_CONSTEXPR uint32 bigEndianInt(const void *bytes) noexcept
Turns 4 bytes into a big-endian integer.
Definition: juce_ByteOrder.h:211
juce::InputStream::readNextLine
virtual String readNextLine()
Reads a UTF-8 string from the stream, up to the next linefeed or carriage return.
Definition: juce_InputStream.cpp:176
juce::InputStream::readDouble
virtual double readDouble()
Reads eight bytes as a 64-bit floating point value.
Definition: juce_InputStream.cpp:148
juce::InputStream::skipNextBytes
virtual void skipNextBytes(int64 numBytesToSkip)
Reads and discards a number of bytes from the stream.
Definition: juce_InputStream.cpp:217
juce::ByteOrder::littleEndianInt
static JUCE_CONSTEXPR uint32 littleEndianInt(const void *bytes) noexcept
Turns 4 bytes into a little-endian integer.
Definition: juce_ByteOrder.h:203
juce::MemoryBlock
A class to hold a resizable block of raw data.
Definition: juce_MemoryBlock.h:36
juce::InputStream::readIntoMemoryBlock
virtual size_t readIntoMemoryBlock(MemoryBlock &destBlock, ssize_t maxNumBytesToRead=-1)
Reads from the stream and appends the data to a MemoryBlock.
Definition: juce_InputStream.cpp:203