2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../core/juce_StandardHeader.h"
30 #include "juce_InputStream.h"
31 #include "juce_MemoryOutputStream.h"
34 //==============================================================================
35 char InputStream::readByte()
42 bool InputStream::readBool()
44 return readByte() != 0;
47 short InputStream::readShort()
51 if (read (temp
, 2) == 2)
52 return (short) ByteOrder::littleEndianShort (temp
);
57 short InputStream::readShortBigEndian()
61 if (read (temp
, 2) == 2)
62 return (short) ByteOrder::bigEndianShort (temp
);
67 int InputStream::readInt()
71 if (read (temp
, 4) == 4)
72 return (int) ByteOrder::littleEndianInt (temp
);
77 int InputStream::readIntBigEndian()
81 if (read (temp
, 4) == 4)
82 return (int) ByteOrder::bigEndianInt (temp
);
87 int InputStream::readCompressedInt()
89 const unsigned char sizeByte
= readByte();
93 const int numBytes
= (sizeByte
& 0x7f);
96 jassertfalse
; // trying to read corrupt data - this method must only be used
97 // to read data that was written by OutputStream::writeCompressedInt()
101 char bytes
[4] = { 0, 0, 0, 0 };
102 if (read (bytes
, numBytes
) != numBytes
)
105 const int num
= (int) ByteOrder::littleEndianInt (bytes
);
106 return (sizeByte
>> 7) ? -num
: num
;
109 int64
InputStream::readInt64()
111 union { uint8 asBytes
[8]; uint64 asInt64
; } n
;
113 if (read (n
.asBytes
, 8) == 8)
114 return (int64
) ByteOrder::swapIfBigEndian (n
.asInt64
);
119 int64
InputStream::readInt64BigEndian()
121 union { uint8 asBytes
[8]; uint64 asInt64
; } n
;
123 if (read (n
.asBytes
, 8) == 8)
124 return (int64
) ByteOrder::swapIfLittleEndian (n
.asInt64
);
129 float InputStream::readFloat()
131 // the union below relies on these types being the same size...
132 static_jassert (sizeof (int32
) == sizeof (float));
133 union { int32 asInt
; float asFloat
; } n
;
134 n
.asInt
= (int32
) readInt();
138 float InputStream::readFloatBigEndian()
140 union { int32 asInt
; float asFloat
; } n
;
141 n
.asInt
= (int32
) readIntBigEndian();
145 double InputStream::readDouble()
147 union { int64 asInt
; double asDouble
; } n
;
148 n
.asInt
= readInt64();
152 double InputStream::readDoubleBigEndian()
154 union { int64 asInt
; double asDouble
; } n
;
155 n
.asInt
= readInt64BigEndian();
159 String
InputStream::readString()
161 MemoryBlock
buffer (256);
162 char* data
= static_cast<char*> (buffer
.getData());
165 while ((data
[i
] = readByte()) != 0)
167 if (++i
>= buffer
.getSize())
169 buffer
.setSize (buffer
.getSize() + 512);
170 data
= static_cast<char*> (buffer
.getData());
174 return String::fromUTF8 (data
, (int) i
);
177 String
InputStream::readNextLine()
179 MemoryBlock
buffer (256);
180 char* data
= static_cast<char*> (buffer
.getData());
183 while ((data
[i
] = readByte()) != 0)
190 const int64 lastPos
= getPosition();
192 if (readByte() != '\n')
193 setPosition (lastPos
);
198 if (++i
>= buffer
.getSize())
200 buffer
.setSize (buffer
.getSize() + 512);
201 data
= static_cast<char*> (buffer
.getData());
205 return String::fromUTF8 (data
, (int) i
);
208 int InputStream::readIntoMemoryBlock (MemoryBlock
& block
, int numBytes
)
210 MemoryOutputStream
mo (block
, true);
211 return mo
.writeFromInputStream (*this, numBytes
);
214 String
InputStream::readEntireStreamAsString()
216 MemoryOutputStream mo
;
217 mo
.writeFromInputStream (*this, -1);
218 return mo
.toString();
221 //==============================================================================
222 void InputStream::skipNextBytes (int64 numBytesToSkip
)
224 if (numBytesToSkip
> 0)
226 const int skipBufferSize
= (int) jmin (numBytesToSkip
, (int64
) 16384);
227 HeapBlock
<char> temp (skipBufferSize
);
229 while (numBytesToSkip
> 0 && ! isExhausted())
230 numBytesToSkip
-= read (temp
, (int) jmin (numBytesToSkip
, (int64
) skipBufferSize
));