OpenShot Audio Library | OpenShotAudio  0.3.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 
36 ssize_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 
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 
230 {
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
virtual short readShort()
static Type swapIfBigEndian(Type value) noexcept
virtual int64 getTotalLength()=0
virtual bool readBool()
virtual void skipNextBytes(int64 numBytesToSkip)
virtual int readIntBigEndian()
virtual int64 readInt64BigEndian()
virtual bool writeByte(char byte)
virtual int64 readInt64()
int64 writeFromInputStream(InputStream &, int64 maxNumBytesToWrite) override
static JUCE_CONSTEXPR uint32 bigEndianInt(const void *bytes) noexcept
virtual String readString()
virtual int readCompressedInt()
virtual short readShortBigEndian()
virtual int64 getPosition()=0
virtual bool isExhausted()=0
virtual float readFloatBigEndian()
virtual int readInt()
static Type swapIfLittleEndian(Type value) noexcept
static JUCE_CONSTEXPR uint32 littleEndianInt(const void *bytes) noexcept
static JUCE_CONSTEXPR uint16 littleEndianShort(const void *bytes) noexcept
static JUCE_CONSTEXPR uint16 bigEndianShort(const void *bytes) noexcept
virtual size_t readIntoMemoryBlock(MemoryBlock &destBlock, ssize_t maxNumBytesToRead=-1)
virtual double readDoubleBigEndian()
virtual char readByte()
virtual String readNextLine()
virtual int read(void *destBuffer, int maxBytesToRead)=0
virtual double readDouble()
virtual float readFloat()
virtual bool setPosition(int64 newPosition)=0
virtual String readEntireStreamAsString()