2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 ==============================================================================
26 #include "InputStream.h"
27 #include "../memory/ByteOrder.h"
28 #include "../streams/MemoryOutputStream.h"
32 int64
InputStream::getNumBytesRemaining()
34 int64 len
= getTotalLength();
42 char InputStream::readByte()
49 bool InputStream::readBool()
51 return readByte() != 0;
54 short InputStream::readShort()
58 if (read (temp
, 2) == 2)
59 return (short) ByteOrder::littleEndianShort (temp
);
64 short InputStream::readShortBigEndian()
68 if (read (temp
, 2) == 2)
69 return (short) ByteOrder::bigEndianShort (temp
);
74 int InputStream::readInt()
78 if (read (temp
, 4) == 4)
79 return (int) ByteOrder::littleEndianInt (temp
);
84 int InputStream::readIntBigEndian()
88 if (read (temp
, 4) == 4)
89 return (int) ByteOrder::bigEndianInt (temp
);
94 int InputStream::readCompressedInt()
96 const uint8 sizeByte
= (uint8
) readByte();
100 const int numBytes
= (sizeByte
& 0x7f);
103 wassertfalse
; // trying to read corrupt data - this method must only be used
104 // to read data that was written by OutputStream::writeCompressedInt()
108 char bytes
[4] = { 0, 0, 0, 0 };
109 if (read (bytes
, numBytes
) != numBytes
)
112 const int num
= (int) ByteOrder::littleEndianInt (bytes
);
113 return (sizeByte
>> 7) ? -num
: num
;
116 int64
InputStream::readInt64()
118 union { uint8 asBytes
[8]; uint64 asInt64
; } n
;
120 if (read (n
.asBytes
, 8) == 8)
121 return (int64
) ByteOrder::swapIfBigEndian (n
.asInt64
);
126 int64
InputStream::readInt64BigEndian()
128 union { uint8 asBytes
[8]; uint64 asInt64
; } n
;
130 if (read (n
.asBytes
, 8) == 8)
131 return (int64
) ByteOrder::swapIfLittleEndian (n
.asInt64
);
136 float InputStream::readFloat()
138 // the union below relies on these types being the same size...
139 #ifdef CARLA_PROPER_CPP11_SUPPORT
140 static_wassert (sizeof (int32
) == sizeof (float));
142 union { int32 asInt
; float asFloat
; } n
;
143 n
.asInt
= (int32
) readInt();
147 float InputStream::readFloatBigEndian()
149 union { int32 asInt
; float asFloat
; } n
;
150 n
.asInt
= (int32
) readIntBigEndian();
154 double InputStream::readDouble()
156 union { int64 asInt
; double asDouble
; } n
;
157 n
.asInt
= readInt64();
161 double InputStream::readDoubleBigEndian()
163 union { int64 asInt
; double asDouble
; } n
;
164 n
.asInt
= readInt64BigEndian();
168 String
InputStream::readString()
170 MemoryBlock
buffer (256);
171 char* data
= static_cast<char*> (buffer
.getData());
174 while ((data
[i
] = readByte()) != 0)
176 if (++i
>= buffer
.getSize())
178 buffer
.setSize (buffer
.getSize() + 512);
179 data
= static_cast<char*> (buffer
.getData());
183 return String::fromUTF8 (data
, (int) i
);
186 String
InputStream::readNextLine()
188 MemoryBlock
buffer (256);
189 char* data
= static_cast<char*> (buffer
.getData());
192 while ((data
[i
] = readByte()) != 0)
199 const int64 lastPos
= getPosition();
201 if (readByte() != '\n')
202 setPosition (lastPos
);
207 if (++i
>= buffer
.getSize())
209 buffer
.setSize (buffer
.getSize() + 512);
210 data
= static_cast<char*> (buffer
.getData());
214 return String::fromUTF8 (data
, (int) i
);
217 size_t InputStream::readIntoMemoryBlock (MemoryBlock
& block
, ssize_t numBytes
)
219 MemoryOutputStream
mo (block
, true);
220 return (size_t) mo
.writeFromInputStream (*this, numBytes
);
223 String
InputStream::readEntireStreamAsString()
225 MemoryOutputStream mo
;
227 return mo
.toString();
230 //==============================================================================
231 void InputStream::skipNextBytes (int64 numBytesToSkip
)
233 if (numBytesToSkip
> 0)
235 const int skipBufferSize
= (int) jmin (numBytesToSkip
, (int64
) 16384);
236 HeapBlock
<char> temp
;
237 temp
.malloc((size_t) skipBufferSize
); // another FIXME
239 while (numBytesToSkip
> 0 && ! isExhausted())
240 numBytesToSkip
-= read (temp
, (int) jmin (numBytesToSkip
, (int64
) skipBufferSize
));