LMMS
Loading...
Searching...
No Matches
juce_InputStream.cpp
Go to the documentation of this file.
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
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//==============================================================================
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 uint32 littleEndianInt(const void *bytes) noexcept
Definition ByteOrder.h:236
static uint16 swapIfLittleEndian(uint16 value) noexcept
Definition ByteOrder.h:227
static uint16 bigEndianShort(const void *bytes) noexcept
Definition ByteOrder.h:241
static uint32 bigEndianInt(const void *bytes) noexcept
Definition ByteOrder.h:239
static uint16 littleEndianShort(const void *bytes) noexcept
Definition ByteOrder.h:238
static uint16 swapIfBigEndian(uint16 value) noexcept
Definition ByteOrder.h:218
static constexpr uint32 littleEndianInt(const void *bytes) noexcept
Definition juce_ByteOrder.h:203
Definition juce_HeapBlock.h:87
virtual int64 getPosition()=0
virtual int64 readInt64()
Definition juce_InputStream.cpp:133
virtual int readIntBigEndian()
Definition juce_InputStream.cpp:98
virtual float readFloatBigEndian()
Definition juce_InputStream.cpp:161
int64 getNumBytesRemaining()
Definition juce_InputStream.cpp:26
virtual float readFloat()
Definition juce_InputStream.cpp:153
virtual int readCompressedInt()
Definition juce_InputStream.cpp:108
virtual bool setPosition(int64 newPosition)=0
virtual bool isExhausted()=0
virtual int64 getTotalLength()=0
virtual double readDoubleBigEndian()
Definition juce_InputStream.cpp:175
virtual short readShort()
Definition juce_InputStream.cpp:68
virtual int64 readInt64BigEndian()
Definition juce_InputStream.cpp:143
virtual void skipNextBytes(int64 numBytesToSkip)
Definition juce_InputStream.cpp:237
virtual bool readBool()
Definition juce_InputStream.cpp:63
virtual short readShortBigEndian()
Definition juce_InputStream.cpp:78
virtual size_t readIntoMemoryBlock(MemoryBlock &destBlock, ssize_t maxNumBytesToRead=-1)
Definition juce_InputStream.cpp:223
virtual int read(void *destBuffer, int maxBytesToRead)=0
virtual String readNextLine()
Definition juce_InputStream.cpp:196
virtual String readEntireStreamAsString()
Definition juce_InputStream.cpp:229
virtual int readInt()
Definition juce_InputStream.cpp:88
virtual double readDouble()
Definition juce_InputStream.cpp:168
virtual String readString()
Definition juce_InputStream.cpp:182
virtual char readByte()
Definition juce_InputStream.cpp:56
Definition juce_MemoryBlock.h:33
Definition juce_MemoryOutputStream.h:36
Definition juce_String.h:53
#define jassert(expression)
#define jassertfalse
Definition carla_juce.cpp:31
unsigned long long uint64
Definition juce_MathsFunctions.h:56
constexpr Type jmin(Type a, Type b)
Definition juce_MathsFunctions.h:106
long long int64
Definition juce_MathsFunctions.h:54
Type * addBytesToPointer(Type *basePointer, IntegerType bytes) noexcept
Definition juce_Memory.h:111
signed int int32
Definition juce_MathsFunctions.h:43
unsigned char uint8
Definition juce_MathsFunctions.h:37
int n
Definition crypt.c:458
return c
Definition crypt.c:175
ulg size
Definition extract.c:2350
read(f, &c, 1)
typedef int(UZ_EXP MsgFn)()
mo
Definition zipinfo.c:2287