Add remaining files
[juce-lv2.git] / juce / source / src / io / streams / juce_InputStream.cpp
blob079f057575c8c300dc8cf78a1a805d501822482d
1 /*
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"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_InputStream.h"
31 #include "juce_MemoryOutputStream.h"
34 //==============================================================================
35 char InputStream::readByte()
37 char temp = 0;
38 read (&temp, 1);
39 return temp;
42 bool InputStream::readBool()
44 return readByte() != 0;
47 short InputStream::readShort()
49 char temp[2];
51 if (read (temp, 2) == 2)
52 return (short) ByteOrder::littleEndianShort (temp);
54 return 0;
57 short InputStream::readShortBigEndian()
59 char temp[2];
61 if (read (temp, 2) == 2)
62 return (short) ByteOrder::bigEndianShort (temp);
64 return 0;
67 int InputStream::readInt()
69 char temp[4];
71 if (read (temp, 4) == 4)
72 return (int) ByteOrder::littleEndianInt (temp);
74 return 0;
77 int InputStream::readIntBigEndian()
79 char temp[4];
81 if (read (temp, 4) == 4)
82 return (int) ByteOrder::bigEndianInt (temp);
84 return 0;
87 int InputStream::readCompressedInt()
89 const unsigned char sizeByte = readByte();
90 if (sizeByte == 0)
91 return 0;
93 const int numBytes = (sizeByte & 0x7f);
94 if (numBytes > 4)
96 jassertfalse; // trying to read corrupt data - this method must only be used
97 // to read data that was written by OutputStream::writeCompressedInt()
98 return 0;
101 char bytes[4] = { 0, 0, 0, 0 };
102 if (read (bytes, numBytes) != numBytes)
103 return 0;
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);
116 return 0;
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);
126 return 0;
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();
135 return n.asFloat;
138 float InputStream::readFloatBigEndian()
140 union { int32 asInt; float asFloat; } n;
141 n.asInt = (int32) readIntBigEndian();
142 return n.asFloat;
145 double InputStream::readDouble()
147 union { int64 asInt; double asDouble; } n;
148 n.asInt = readInt64();
149 return n.asDouble;
152 double InputStream::readDoubleBigEndian()
154 union { int64 asInt; double asDouble; } n;
155 n.asInt = readInt64BigEndian();
156 return n.asDouble;
159 String InputStream::readString()
161 MemoryBlock buffer (256);
162 char* data = static_cast<char*> (buffer.getData());
163 size_t i = 0;
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());
181 size_t i = 0;
183 while ((data[i] = readByte()) != 0)
185 if (data[i] == '\n')
186 break;
188 if (data[i] == '\r')
190 const int64 lastPos = getPosition();
192 if (readByte() != '\n')
193 setPosition (lastPos);
195 break;
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));
234 END_JUCE_NAMESPACE